Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views12 pages

Ajpprogram

The document contains a series of Java programs demonstrating the use of AWT and Swing components for various applications, including creating forms, calculators, menus, trees, tables, and progress bars. Each practical example showcases different layouts and components such as Frame, Label, TextField, Button, Choice, List, JTree, JTable, and JProgressBar. The programs are designed to illustrate fundamental concepts in GUI programming using Java.

Uploaded by

Sahil Tarle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Ajpprogram

The document contains a series of Java programs demonstrating the use of AWT and Swing components for various applications, including creating forms, calculators, menus, trees, tables, and progress bars. Each practical example showcases different layouts and components such as Frame, Label, TextField, Button, Choice, List, JTree, JTable, and JProgressBar. The programs are designed to illustrate fundamental concepts in GUI programming using Java.

Uploaded by

Sahil Tarle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Pratical:-1

1. Write a program to demonstrate the use of AWT components .


import java.awt.*;
public class BasicAWT extends Frame{
public static void main(String args[])
{
Frame f=new Frame();
f.setSize(400,400);
f.setVisible(true);
f.setLayout(new FlowLayout());
f.setBackground(Color.cyan);

Label l1=new Label();


l1.setText("Enter your name");
//l1.setBounds(20,50,30,30);

TextField tf= new TextField("Shreya");


//tf.setBounds(20,50,20,50);

Label l2=new Label ("Address");


TextArea ta = new TextArea ("",3,40);
Button b = new Button ("Submit");
Label l4 = new Label("Select subject");
//l4.setBounds(100,100,100,110);
Checkbox cb1 = new Checkbox("english");
Checkbox cb2 = new Checkbox("marathi");
Checkbox cb3 = new Checkbox("hindi");
Checkbox cb4 = new Checkbox("history");
Label l5= new Label ("Select gender");
CheckboxGroup cg = new CheckboxGroup ();
Checkbox c1 = new Checkbox("Male",cg,true);
Checkbox c2 = new Checkbox("Female",cg,true);

f.add(l1);
f.add(tf);
f.add(l4);
f.add(cb1);
f.add(cb2);
f.add(cb3);
f.add(cb4);
f.add(l5);
f.add(c1);
f.add(c2);
f.add(l2);
f.add(ta);
f.add(b);
}
}
Pratical:-2
1. Write a program to design a form using the component list and choice.

import java.awt.*;
public class ChoiceDemo
{
public static void main(String args[])
{
Frame f = new Frame();
f.setSize(400,400);
f.setVisible(true);
f.setLayout(new FlowLayout());

Choice c = new Choice();


c.add("Maths");
c.add("Physics");
c.add("Chemistry");
Label la=new Label("LIST Component");
List l = new List();
//l.setMultipleSelections(true);
l.add("The Times of India");
l.add("Lokmat");
l.add("Divya Marathi");
l.add("Navbharat Times");

f.add(la);

f.add(l);
f.add(c);
}
}
Pratical:-3
1. Write a program to design simple calculator with the use of Grid layout.

import java.awt.*;
import javax.swing.*;
public class MyGridLayout{
JFrame f;
MyGridLayout(){
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
// adding buttons to the frame
f.add(b1); f.add(b2); f.add(b3);
f.add(b4); f.add(b5); f.add(b6);
f.add(b7); f.add(b8); f.add(b9);

// setting grid layout of 3 rows and 3 columns


f.setLayout(new GridLayout(3,3));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyGridLayout();
}
}
Pratical:-4
Using of CardLayout to write a program to create a two-level card deck that
allows the user to select an operating system.

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.*;
public class CardLayoutDemo extends JFrame implements ActionListener {
CardLayout card;
JButton b1, b2, b3;
Container c;
CardLayoutDemo()
{
c = getContentPane();
card = new CardLayout(40, 30);
c.setLayout(card);

b1 = new JButton("First Level");


b2 = new JButton("Second Level");

b1.addActionListener(this);
b2.addActionListener(this);

c.add("a", b1);
c.add("b", b2);
}

public void actionPerformed(ActionEvent e)


{
card.next(c);
}

public static void main(String[] args)


{
CardLayoutDemo cl = new CardLayoutDemo();

cl.setSize(400, 400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Pratical:-5
1. Write a program using AWT to create a menu bar where menu bar
contains menu items .

import java.awt.*;
class MenuExample
{
MenuExample(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("Sub Menu");
MenuItem i1=new MenuItem("Item 1");

MenuItem i2=new MenuItem("Item 2");


MenuItem i3=new MenuItem("Item 3");
MenuItem i4=new MenuItem("Item 4");
MenuItem i5=new MenuItem("Item 5");
menu.add(i1);
menu.add(i2);
menu.add(i3);
submenu.add(i4);
submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}
}
}
Pratical:-6
1. Write a program using swig to display a Scrollpane and jcomboBox in an
japple.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class JComboBoxDemo extends JApplet implements ItemListener


{
JLabel JLabelObj ;

public void init()


{
setLayout(new FlowLayout());
setSize(400, 400);
setVisible(true);
JComboBox JComboBoxObj = new JComboBox();

JComboBoxObj.addItem("Solapur");
JComboBoxObj.addItem("Pune");
JComboBoxObj.addItem("Banglore");
JComboBoxObj.addItem("Mumbai");
JComboBoxObj.addItemListener(this);

JLabelObj = new JLabel();

add(JComboBoxObj);
add(JLabelObj);
}

public void itemStateChanged(ItemEvent ie)


{
String stateName = (String) ie.getItem();

JLabelObj.setText("You are in "+stateName);


}

}
/* <applet code="JComboBoxDemo" height="400" width="400"> </applet> */
Pratical:7
1. Write a program to create a JTree.

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;

public class JTreeDemo


{
public static void main(String[] args) {

JFrame JFrameMain = new JFrame();


JFrameMain.setVisible(true);
JFrameMain.setSize(400,400);

DefaultMutableTreeNode rootNode = new


DefaultMutableTreeNode("India");

DefaultMutableTreeNode maharashtraNode = new


DefaultMutableTreeNode("Maharashtra");
DefaultMutableTreeNode gujrathNode = new
DefaultMutableTreeNode("Gujrath");
rootNode.add(maharashtraNode);
rootNode.add(gujrathNode);

DefaultMutableTreeNode mumbaiSubNode = new


DefaultMutableTreeNode("Mumbai");
DefaultMutableTreeNode puneSubNode = new
DefaultMutableTreeNode("Pune");
DefaultMutableTreeNode nashikSubNode = new
DefaultMutableTreeNode("Nashik");
DefaultMutableTreeNode nagpurSubNode = new
DefaultMutableTreeNode("Nagpur");

maharashtraNode.add(mumbaiSubNode);
maharashtraNode.add(puneSubNode);
maharashtraNode.add(nashikSubNode);
maharashtraNode.add(nagpurSubNode);

JTree tree = new JTree(rootNode);

JFrameMain.add(tree);
}

}
Pratical:-8
1. Write a program to create a JTable.

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
public class JTableDemo
{
public static void main(String[] args)
{
JFrame JFrameMain = new JFrame();
JFrameMain.setVisible(true);
JFrameMain.setSize(400,400);

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

Object data[][] = {
{101,"Amit",670000},
{102,"Jai",780000},
{101,"Sachin",700000}
};
JTable JTableObj = new JTable(data,colHeads);

int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(JTableObj,v,h);

JFrameMain.add(jsp,BorderLayout.CENTER);

//JFrameMain.add(JTableObj);
}
}
Pratical:-9
1. Write a program to launch a JProgressBar

import javax.swing.*;
import java.awt.*;

public class JProgresBarDemo


{
JProgressBar JProgressBarObj;
int i=0,num=0;

JProgresBarDemo()
{
JFrame JFrameMain = new JFrame();

JFrameMain.setVisible(true);
JFrameMain.setSize(400,400);
JFrameMain.setLayout(new FlowLayout());

JProgressBarObj = new JProgressBar(0,2000);


JProgressBarObj.setValue(0);
JProgressBarObj.setStringPainted(true);

JFrameMain.add(JProgressBarObj);
}

public static void main(String[] args)


{
JProgresBarDemo jpd = new JProgresBarDemo();
jpd.iterate();
}

public void iterate()


{
while(i<=2000){
JProgressBarObj.setValue(i);
i =i+20;
try
{
Thread.sleep(150);
}
catch(Exception e)
{

}
}
}
}

You might also like