You are on page 1of 11

1.

Write a program to demonstrate textbox radio button & checkbox

import java.applet.*;

import java.awt.*;

/*<APPLET CODE="Demo.class" WIDTH=400 HEIGHT=200>

</APPLET>

*/

public class Demo extends Applet {

CheckboxGroup radioGroup;

public void init() {

radioGroup= new CheckboxGroup();

Checkbox c1 = new Checkbox("Marathi");

Checkbox c2 = new Checkbox("English");

Checkbox radioButton1 =new Checkbox("Hindi",radioGroup,false);

Checkbox radioButton2 =new Checkbox("Maths",radioGroup,false);

TextField tf = new TextField(30);

add(c1);

add(c2);

add(radioButton1);

add(radioButton2);

add(new Label("Text Field"));

add(tf);

}
2. Write a program to demonstrate choice and list various cities in India

import java.awt.*;

public class ListShow {

public static void main(String args[]) {

Frame F1 = new Frame("Cities");

F1.setSize(500, 500);

F1.setLayout(new FlowLayout());

F1.setVisible(true);

Label L1 = new Label("To Different cities");

List Cities = new List(10);

Cities.add("Jaipur");

Cities.add("Lucknow");

Cities.add("Patna");

Choice cityChoice = new Choice();

cityChoice.add("Select City");

cityChoice.add("Mumbai");

cityChoice.add("Pune");

cityChoice.add("Hyderabad");

F1.add(L1);

F1.add(Cities);

F1.add(cityChoice);

}
3. Write a program to demonstrate card layout border layout gird layout

import java.awt.*;

import java.applet.*;

public class Prac extends Applet {

public void init() {

setLayout(new GridLayout(3, 3));

add(new Button("0"));

add(new Button("1"));

add(new Button("2"));

add(new Button("3"));

add(new Button("4"));

add(new Button("5"));

add(new Button("6"));

add(new Button("7"));

add(new Button("8"));

Frame F= new Frame("Direction");

F.setLayout(new BorderLayout());

F.setVisible(true);

F.setSize(300,300);

Button b1=new Button("North");

Button b2=new Button("South");

Button b3=new Button("West");

Button b4=new Button("East");

F.add(b1,BorderLayout.NORTH);

F.add(b2,BorderLayout.SOUTH);

F.add(b3,BorderLayout.WEST);

F.add(b4,BorderLayout.EAST);

/*<applet code="Prac.class" width="300" height="300"></applet>*/


4. Write program to demonstrate Menu bar and menu items

import java.awt.*;

public class Exp {

public Exp() {

Frame f = new Frame("Menu & MenuItem Exp");

MenuBar mB = new MenuBar();

Menu me = new Menu("Red");

MenuItem i1 = new MenuItem("Pink");

MenuItem i2 = new MenuItem("Yellow");

MenuItem i3 = new MenuItem("Queen");

CheckboxMenuItem checkboxItem = new CheckboxMenuItem("Blue");

me.add(i1);

me.add(i2);

me.add(i3);

me.add(checkboxItem);

mB.add(me);

f.setMenuBar(mB);

f.setSize(300, 300);

f.setVisible(true);

public static void main(String args[]) {

new Exp();

}
5. Write a program to demonstrate card layout
import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CardLayoutExample extends JFrame implements ActionListener {

CardLayout cardA;

JButton b1, b2, b3;

Container c;

CardLayoutExample() {

c = getContentPane();

card = new CardLayout(40, 30);

c.setLayout(card);

b1 = new JButton("Apple");

b2 = new JButton("Boy");

b3 = new JButton("Cat");

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

c.add("a", b1);

c.add("b", b2);

c.add("c", b3);

public void actionPerformed(ActionEvent e) {

card.next(c);

public static void main(String[] args) {

CardLayoutExample cl = new CardLayoutExample();

cl.setSize(400, 400);

cl.setVisible(true);

cl.setDefaultCloseOperation(EXIT_ON_CLOSE);

}
6. Write a program to demonstrate Grid layout

import java.awt.*;

import java.applet.*;

public class Prac extends Applet {

public void init() {

setLayout(new GridLayout(3, 3));

add(new Button("0"));

add(new Button("1"));

add(new Button("2"));

add(new Button("3"));

add(new Button("4"));

add(new Button("5"));

add(new Button("6"));

add(new Button("7"));

add(new Button("8"));

/*<applet code="Prac.class" width="300" height="300"></applet>*/


7. Write a programme to demonstrate combo box for various cities in India and display the selected
city

import java.awt.*;

import javax.swing.*;

public class JComboBoxDemo extends JFrame {

public JComboBoxDemo() {

Container ct = getContentPane();

ct.setLayout(new FlowLayout());

JLabel label = new JLabel("Select the different states of India");

JComboBox<String>jc = new JComboBox<>();

jc.addItem("Mumbai");

jc.addItem("Patna");

jc.addItem("Gujrat");

jc.setBounds(30, 50, 100, 30);

jc.setBounds(150, 50, 100, 30);

ct.add(label);

ct.add(jc);

JLabel selectedCityLabel = new JLabel("Selected City:");

ct.add(label);

ct.add(jc);

ct.add(selectedCityLabel);

jc.addActionListener(e -> {

String selectedCity = (String) jc.getSelectedItem();

selectedCityLabel.setText("Selected City: " + selectedCity);

});

public static void main(String[] args) {

JComboBoxDemo f1 = new JComboBoxDemo();

f1.setTitle("Demonstrating JComboBox");

f1.setSize(300, 400);

f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);

8. Write a programme to demonstrate jtree & its nodes

import java.awt.*;

import javax.swing.*;

import javax.swing.tree.*;

/*<applet code="TreeApplet.class" width="300" height="300"></applet>*/

public class TreeApplet extends JApplet {

public void init() {

Container container = getContentPane();

container.setLayout(new BorderLayout());

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");

DefaultMutableTreeNode ai = new DefaultMutableTreeNode("AI");

DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");

DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");

DefaultMutableTreeNode bi = new DefaultMutableTreeNode("BI");

DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");

root.add(a);

a.add(ai);

a.add(a2);

root.add(b);

b.add(bi);

b.add(b2);

JTree tree = new JTree(root);

JScrollPane scrollPane = new JScrollPane(tree);

container.add(scrollPane, BorderLayout.CENTER);

}
9. Write a program to demonstrate Jtable for various employees in a company

import javax.swing.*;

public class JTableDemo {

JFrame frame;

JTableDemo() {

frame = new JFrame();

String[][] rows = {

{"101", "Amit", "67000"},

{"102", "Jai", "780000"},

{"103", "Sachin", "700000"}

};

String[] columns = { "ID", "Name", "Salary" };

JTable jTable = new JTable(rows, columns);

jTable.setBounds(30, 40, 200, 300);

JScrollPane scrollPane = new JScrollPane(jTable);

frame.add(scrollPane);

frame.setSize(300, 400);

frame.setVisible(true);

public static void main(String args[]) {

new JTableDemo();

}
10. Write a program to demonstrate password field and display username and password on screen

import javax.swing.*;

public class PasswordFieldExample {

public static void main(String args[]) {

JFrame frame = new JFrame();

JPasswordField value = new JPasswordField();

value.setBounds(100, 75, 100, 30);

JLabel label1 = new JLabel("Username");

label1.setBounds(20, 20, 80, 80);

JLabel label2 = new JLabel("Password");

label2.setBounds(20, 75, 80, 30);

JButton button = new JButton("Login");

button.setBounds(100, 120, 80, 30);

final JTextField text = new JTextField();

text.setBounds(100, 20, 100, 30);

frame.add(value);

frame.add(label1);

frame.add(label2);

frame.add(button);

frame.add(text);

frame.setSize(300, 200);

frame.setLayout(null);

frame.setVisible(true);

button.addActionListener(e -> {

String username = text.getText();

String password = new String(value.getPassword());

JOptionPane.showMessageDialog(frame, "Username: " + username + "\nPassword: " +


password);

});

}
11. Write a program to demonstrate all Inet address methods in Java

import java.net.*;

public class InetDemo {

public static void main(String args[]) {

try {

InetAddress ip = InetAddress.getByName("www.youtube.com");

System.out.println("Host name: " + ip.getHostName());

System.out.println("Host IP: " + ip.getHostAddress());

} catch (Exception e) {

System.out.println(e);

12. Write a program to demonstrate all URL methods in Java

import java.net.*;

public class URLDemo {

public static void main(String args[]) {

try {

URL url = new URL("http://www.example.com/path?query=value");

System.out.println("URL: " + url.toString());

System.out.println("Protocol: " + url.getProtocol());

System.out.println("Host: " + url.getHost());

System.out.println("Port: " + url.getPort());

System.out.println("Path: " + url.getPath());

System.out.println("Query: " + url.getQuery());

System.out.println("File: " + url.getFile());

} catch (Exception e) {

System.out.println(e);

You might also like