Vous êtes sur la page 1sur 85

AWT - abstract window tool kit

package: java.awt
classes:
Frame
TextField
TextArea
Button
Checkbox
CheckboxGroup
List
Choice
Label

class : Frame
One can create a frame by extending the class
Frame.
Following components can be added to a frame:
TextField
TextArea
Button
Checkbox
List
Choice
Label

LayoutManager : speaks about, how the


components are arranged on Frame.

Following are some of the classes wheich


specifies different types of layouts:
FlowLayout
GridLayout
BorderLayout
GridBagLayout

One can specify the layout for a frame, by


invoking the method :
setLayout(LayoutManager object)
One can specify the dimensions of a frame by
invoking the method:
setSize(int,int)
By default the frame is invisible, one can make
it visible by calling the setVisible(boolean) with
boolean value true

// Program to demonstrate AWT Frame and its


components

import java.awt.*;
class AWTFrame extends Frame
{
Label l;
TextField tf;
TextArea ta;
Button b;
Checkbox cbx1;
Checkbox cbx2;
Checkbox cbx3;
Checkbox cbx4;
CheckboxGroup cbg; // not a component
List li;
Choice c;
AWTFrame(String s)
{
super(s); // invoking parent class constructor
to set title of the frame
//setTitle(s);->instead of invoking parent
constructor to set title, call the
setTitle(String)
l=new Label("It's me"); // creation of
component Label
tf= new TextField(15); // creation of
component text field with width 15
ta= new TextArea(5,5); // creation of
component text area with height 5 and width 5
b = new Button("Press Here"); // creation of
component Button with message 'Press Here'
cbx1= new Checkbox("red"); // creation of
component checkbox with label 'red'
cbx2= new Checkbox("blue");// creation of
component checkbox with label 'blue'
// from above two checkboxes, we can also
select both and deselect both (multiple
selection)
cbg= new CheckboxGroup(); // used with
checkbox creation, for single selection
cbx3= new Checkbox("Male",cbg,false); //
check box group is used , now only single
selection
cbx4= new Checkbox("Female",cbg,false);
li= new List(); // all items of list are
physically visible
li.add("arun"); // items of list
li.add("kiran");// items of list
li.add("kalyan"); // items of list
c=new Choice(); // all items are physically
visible, when u drop the menu down
c.add("arun"); // items of choice
c.add("kiran"); // items of choice
c.add("kalyan"); // items of choice
setSize(300,300); // width and height of frame
is 300 and 300
setVisible(true); // makes the frame visible
setLayout(new FlowLayout()); // arrange the
components on first come first serve basis
add(l); // adding components to frame
add(tf);
add(ta);
add(b);
add(cbx1);
add(cbx2);
add(cbx3);
add(cbx4);
add(li);
add(c);
}
public static void main(String args[])
{
new AWTFrame("Am Title For Frame");//
creating object for class and constructor
invokes
}
}
Press Ctrl+c on command prompt to close
frame

In the above generated frame , when we click


on the cross button of it, the frame is not
getting closed because it cannot listen to the
event performed on it.
Even the button component inside frame do not
react, bcoz it cannot listen to event.
We can force them to listen by adding
Listeners to them using the following methods:
addXXXListener()
and u can remove by calling
removeXXXListener()

WindowListener is an interface which an awt


frame needs to implement to perform window
events
Following are the methods of interface:
WindowListener:
1) public void windowOpened(WindowEvent we)
2) public void windowClosed(WindowEvent we)
3) public void windowClosing(WindowEvent we)
4) public void windowActivated(WindowEvent
we)
5) public void windowDeactivated(WindowEvent
we)
6) public void windowIconified(WindowEvent we)
7) public void windowDeiconified(WindowEvent
we)
ActionListener is an interface which needs to
implemented to perform any activity on button
click.
Following are the methods of interface:
ActionListener
1)public void actionPerformed(ActionEvent ae)
Following are the methods of interface:
ItemListener
1)public void itemStateChanged(ItemEvent ie)
The above three listeners are available in
java.awt.event

// Program to demonstrate awt frame and its


components with listeners
import java.awt.*;
import java.awt.event.*; // listeners
class AWTFrame extends Frame implements
ActionListener,WindowListener,ItemListener
{
Label l;
TextField tf;
TextArea ta;
Button b;
Checkbox cbx1;
Checkbox cbx2;
Checkbox cbx3;
Checkbox cbx4;
CheckboxGroup cbg;
List li;
Choice c;
AWTFrame(String s)
{
super(s); // to set Title of the frame
//setTitle(s);
l=new Label("It's me");
tf= new TextField(15);
ta= new TextArea(5,5);
b = new Button("Press Here");
cbx1= new Checkbox("red");
cbx2= new Checkbox("blue");
cbg= new CheckboxGroup();
cbx3= new Checkbox("Male",cbg,true);
cbx4= new Checkbox("Feamle",cbg,true);
li= new List();
li.add("arun");
li.add("kiran");
li.add("kalyan");
c=new Choice();
c.add("arun");
c.add("kiran");
c.add("kalyan");
setSize(300,300);
setVisible(true);
setLayout(new FlowLayout());
add(l);
add(tf);
add(ta);
add(b);
add(cbx1);
add(cbx2);
add(cbx3);
add(cbx4);
add(li);
add(c);
c.addItemListener(this); // can be applied for
checkbox and choice
li.addItemListener(this);
addWindowListener(this);// adding listener to
Frame
b.addActionListener(this);// adding listener to
Button
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c)
tf.setText(c.getSelectedItem().toString());
if(ie.getSource()==li)
tf.setText(li.getSelectedItem().toString());
}
public void actionPerformed(ActionEvent ae)
{
ta.setText("Hello button was pressed");
}
public void windowOpened(WindowEvent we)
{
System.out.println("Window Opened");
}
public void windowClosed(WindowEvent we)
{
System.out.println("Window Closed");
}
public void windowClosing(WindowEvent we)
{
System.out.println("Window Closing");
System.exit(0);
}
public void windowActivated(WindowEvent we)
{
System.out.println("Window Activated");
}
public void windowDeactivated(WindowEvent we)
{
System.out.println("Window Deactivated");
}
public void windowIconified(WindowEvent we)
{
System.out.println("Window Iconified");
}
public void windowDeiconified(WindowEvent we)
{
System.out.println("Window Deiconified");
}
public static void main(String args[])
{
new AWTFrame("Am Title For Frame");
}
}

Note: It is
inconvenient and a
burden on the
programmer to
override the
abstract methods
of an interface
which he is not
interested
Adapters are class
which got empty
definition for
abstract methods
of interface .
Example:
WindowAdapter
// program to demonstrate WindowAdapter
import java.awt.*;
import java.awt.event.*;
class UserMadeFrame extends Frame {
String s="Hello";
public UserMadeFrame(String str)
{
//setTitle("UserMadeFrame");
super(str);

addWindowListener(new WindowAdapter()
{
public void
windowClosing(WindowEvent we)
{
System.exit(0);
}
});
setSize(400,400);
setVisible(true);
}
public void paint(Graphics g)
{
g.drawString(s,100,100);
}
public static void main(String args[])
{
new UserMadeFrame("UserMadeFrame");
}
}

Swings are not the replacement


of awt, but they are the
extension of AWT
AWT components are not
developed in java, whereas swing
components are purely developed
in java. AWT components does
not maintain same look, whenever
platform changes. whereas swing
maintains consistent look across
various platforms. One can add
images on labels, buttons of
swings but not on awt buttons
and labels. Even we have a
separate class for radio buttons.
// program to demonstrate swings
rimport javax.swing.*;
import java.awt.*;
class UserJFrame extends JFrame
{
Container cp; // belongs to java.awt
JLabel lb;
JTextField tf;
JTextArea ta;
JComboBox c;
JList l;
JCheckBox cb;
JRadioButton rb;
JButton b;
public UserJFrame(String s)
{
super(s); // invokes parent constructor and
that sets the title for the frame
//setTitle("UseMadeFrame");
cp=getContentPane();
lb= new JLabel("Name");
tf=new JTextField(10);
ta=new JTextArea(5,5);
String s1[]={"arun","kiran"};
c=new JComboBox(s1); // s1, array of type
String contains the items of JComboBox
l=new JList(s1);// s1, array of type String
contains the items of JList
cb=new JCheckBox("CB");
rb=new JRadioButton("X");
b=new JButton("OK");
cp.setLayout(new FlowLayout());
cp.add(lb);
cp.add(tf);
cp.add(ta);
cp.add(c);
cp.add(l);
cp.add(cb);
cp.add(rb);
cp.add(b);
pack();// deprecated method and replacement
for it is below. width and height equals sum of
sizes of components
//setSize(50,300);
show(); // deprecated method
//setVisible(true);
//setDefaultCloseOperation(JFrame.EXIT_ON_
CLOSE);
}
public static void main(String args[])
{
new UserJFrame("UserJFrame");
}
}

Note: JFrame is divided into 4 layers:


1)content pane
2) glass pane
3) root pane
4) layered pane

One cannot add components to JFrame by


calling add, but components can be added to
one of the layer of JFrame.
Note: though the WindowListener is not
implemented and abstract methods are not
overridden, still the frame which extended
JFrame gets closed, but it is not coming out of
the command prompt. Press Ctrl+C to come out
to next command prompt or invoke the
following the method in the program:
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);

// program to display date in text field when


we open a application
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;// WindowListener
belongs to event
import java.util.*;// class : Date belongs to it
class TimeDisp extends JFrame implements
WindowListener
{
JLabel jlb;
JTextField jtf;
String str;
Container cp;
Calendar cl =Calendar.getInstance();
public TimeDisp()
{
cp = getContentPane();
jlb = new JLabel("Time");
jtf=new JTextField(10);
str=""+cl.get(Calendar.DATE)+"-"+
(cl.get(Calendar.MONTH)
+1)+"-"+cl.get(Calendar.YEAR);
// month count begins with 0(zero)
cp.setLayout(new FlowLayout());
addWindowListener(this);// added to the
JFrame
cp.add(jlb);
cp.add(jtf);
setSize(200,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}
public void windowOpened(WindowEvent we)
{
jtf.setText(str);
}
public void windowClosed(WindowEvent e)
{}
public void windowActivated(WindowEvent e)
{}
public void windowDeactivated(WindowEvent e)
{}
public void windowDeiconified(WindowEvent e)
{}
public void windowIconified(WindowEvent e)
{}
public void windowClosing(WindowEvent e)
{}
public static void main(String args[])
{
new TimeDisp();
}
}

// program to demonstrate how a single frame


can hold multiple panels
import javax.swing.*;
import java.awt.*;
class TabPanel extends JFrame
{
Container c;
JTabbedPane jtp;
JPanel red;
JPanel green;
JPanel blue;
JPanel yellow;
JButton jb1,jb2,jb3,jb4;
public TabPanel()
{
super("Tabbed Panel");
c=getContentPane();
c.setLayout(new GridLayout(1,1));
// first int represents ROWS, second int
represents COLUMN, number of cells= first int
* second int
jtp = new JTabbedPane(JTabbedPane.TOP);
jb1=new JButton("red");
jb2=new JButton("blue");
jb3=new JButton("green");
jb4=new JButton("yellow");
red=new JPanel(new FlowLayout());
blue=new JPanel(new FlowLayout());
green=new JPanel(new FlowLayout());
yellow=new JPanel(new FlowLayout());
red.add(jb1); // button to panel
blue.add(jb2);
green.add(jb3);
yellow.add(jb4);
jtp.add("RED",red); // panels to tabbed
pane
jtp.add("BLUE",blue);
jtp.add("GREEN",green);
jtp.add("YELLOW",yellow);
c.add(jtp); // tabbed pane to container
setSize(350,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}
public static void main(String args[])
{
TabPanel tp = new TabPanel();
}
}

// program to demonstrate how to create


menu, menu items, and sub menus
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SwingMenu extends JFrame implements
ActionListener
{
JMenuBar mb;
JMenu mFile,mEdit, mFont;
JMenuItem mNew, mOpen, mClose, mCopy,
mPaste, fAr, fCr, fGr;
JTextField tf;
Container c;
public SwingMenu()
{
setTitle("Frame with menu");
c=getContentPane();
tf=new JTextField(20);
c.setLayout(new FlowLayout());
c.add(tf);
mb=new JMenuBar(); // creation of component
menu bar
setJMenuBar(mb); // menu bar gets placed
below the title bar
mFile=new JMenu("File"); //create a component
File Menu
mEdit=new JMenu("Edit");
mb.add(mFile); // adding menu's to menubar
mb.add(mEdit);
mNew=new JMenuItem("New"); // creating
menu items
mOpen=new JMenuItem("Open");
mClose=new JMenuItem("Close");
mFile.add(mNew); // adding menuitems to menu
mFile.add(mOpen);
mFile.add(mClose);
mCopy=new JMenuItem("Copy");
mPaste=new JMenuItem("Paste");
mEdit.add(mCopy);
mEdit.add(mPaste);
mClose.setEnabled(false); // menu item is
disabled
mFont=new JMenu("Font");
fAr=new JMenuItem("Arial");
fCr=new JMenuItem("Courier");
fGr=new JMenuItem("GaraMond");

mFont.add(fAr);
mFont.add(fCr);
mFont.add(fGr);
mFile.add(mFont);// adding menu to another
menu
mNew.addActionListener(this); // adding
listeners to menu items and they listen
mOpen.addActionListener(this); // to events,
otherwise they dont react
mClose.addActionListener(this);
mCopy.addActionListener(this);
mPaste.addActionListener(this);
fAr.addActionListener(this);
fCr.addActionListener(this);
fGr.addActionListener(this);
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==mNew) // we need to
find responsible for event
tf.setText("New Selected");
else if(ae.getSource()==mOpen)
{
tf.setText("Open Selected");
mClose.setEnabled(true);
}
else if(ae.getSource()==mClose)
tf.setText("Close Selected");
else if(ae.getSource()==mCopy)
tf.setText("Copy Selected");
else if(ae.getSource()==mPaste)// with
getSource(), object name is compared
tf.setText("Paste Selected");
if(ae.getActionCommand()=="Arial")
// with getActionCommand() , text on the
component is compared
tf.setFont(new Font("Arial",Font.PLAIN,12));
if(ae.getActionCommand()=="Courier")
tf.setFont(new
Font("Courier",Font.PLAIN,12));
if(ae.getActionCommand()=="GaraMond")
tf.setFont(new
Font("GaraMond",Font.PLAIN,12));
}
public static void main(String args[])
{
new SwingMenu();
}
}

// demonstrate how to add components without


specifying a layout
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
class nuser extends JFrame
{
int ip;
JButton b1,b2,exit;
JLabel l,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,ld;
JTextField t1,t2,t3,t4,t5,t6,t22;
JPasswordField p1,p2;
JTextArea ta;
JComboBox d,m,y,ts;
String year[]=new String[5];
String date[]=new String[31];
String month[]=new String[12];
String tsex[]=new String[3];
Container c;
public nuser()
{
setTitle("New Employee Registratiom
Form");
l=new JLabel(" Employee Registration
Form");
l.setBounds(250,30,850,40);
// first two integer args specify x and y
position where component need to be placed,
third int specifies width of the component,
fourth specifies the height of the component.
l.setFont(new
Font("garamond",Font.BOLD,40));
l.setForeground(Color.white);
String
year[]={"1960","1961","1962","1963","1964","
1965","1966","1967","1968","1969","1970","1
971","1972","1973","1974","1975","1976","19
77","1978","1979","1980","1981","1982","198
3","1984","1985","1986","1987","1988","1999
","2000","2001","2002","2003","2004"};
String
month[]={"JAN","FEB","MARCH","APRIL","MAY
","JUNE","JULY","AUG","SEP","OCT","NOV","
DEC"};
String
date[]={"1","2","3","4","5","6","7","8","9","10"
,"11","12","13","14","15","16","17","18","19",
"20","21","22","23","24","25","26","27","28","
29","30","31"};
String tsex[]={"MALE","FEMALE","---"};
c=getContentPane();
ld=new JLabel("IDNO");
ld.setBounds(250,100,200,30);
ld.setFont(new
Font("garamond",Font.BOLD,20));
ld.setForeground(Color.black);
t22=new JTextField(10);
t22.setBounds(450,100,300,30);
t22.setFont(new
Font("garamond",Font.BOLD,20));
l1=new JLabel("Login Name");
l1.setBounds(250,140,200,30);
l1.setFont(new
Font("garamond",Font.BOLD,20));
l1.setForeground(Color.black);
t1=new JTextField(20);
t1.setBounds(450,140,300,30);
t1.setFont(new
Font("garamond",Font.BOLD,20));
l2=new JLabel("Password");
l2.setBounds(250,180,200,30);
l2.setFont(new
Font("garamond",Font.BOLD,20));
l2.setForeground(Color.black);
p1=new JPasswordField(15);
p1.setBounds(450,180,300,30);
p1.setFont(new
Font("garamond",Font.BOLD,20));
l3=new JLabel("Re-enter Password");
l3.setBounds(250,220,200,30);
l3.setFont(new
Font("garamond",Font.BOLD,20));
l3.setForeground(Color.black);
p2=new JPasswordField(15);
p2.setBounds(450,220,300,30);
p2.setFont(new
Font("garamond",Font.BOLD,20));
l4=new JLabel("First Name");
l4.setBounds(250,260,200,30);
l4.setFont(new
Font("garamond",Font.BOLD,20));
l4.setForeground(Color.black);
t2=new JTextField(20);
t2.setBounds(450,260,300,30);
t2.setFont(new
Font("garamond",Font.BOLD,20));
l5=new JLabel("Last Name");
l5.setBounds(250,300,200,30);
l5.setFont(new
Font("garamond",Font.BOLD,20));
l5.setForeground(Color.black);
t3=new JTextField(20);
t3.setBounds(450,300,300,30);
t3.setFont(new
Font("garamond",Font.BOLD,20));
l6=new JLabel("Age");
l6.setBounds(250,340,200,30);
l6.setFont(new
Font("garamond",Font.BOLD,20));
l6.setForeground(Color.black);
t4=new JTextField(3);
t4.setBounds(450,340,300,30);
t4.setFont(new
Font("garamond",Font.BOLD,20));
l7=new JLabel("Dapartment Name");
l7.setBounds(250,380,200,30);
l7.setFont(new
Font("garamond",Font.BOLD,20));
l7.setForeground(Color.black);
t5=new JTextField(15);
t5.setBounds(450,380,300,30);
t5.setFont(new
Font("garamond",Font.BOLD,20));
l8=new JLabel("Department No");
l8.setBounds(250,420,200,30);
l8.setFont(new
Font("garamond",Font.BOLD,20));
l8.setForeground(Color.black);
t6=new JTextField(10);
t6.setBounds(450,420,300,30);
t6.setFont(new
Font("garamond",Font.BOLD,20));
l9=new JLabel("Sex");
l9.setBounds(250,460,200,20);
l9.setFont(new
Font("garamond",Font.BOLD,20));
l9.setForeground(Color.black);
ts=new JComboBox(tsex);
ts.setBounds(450,460,150,30);
ts.setFont(new
Font("garamond",Font.BOLD,20));
ts.setForeground(Color.black);
l10=new JLabel("Date Of Birth");
l10.setBounds(250,500,200,30);
l10.setFont(new
Font("garamond",Font.BOLD,20));
l10.setForeground(Color.black);
d=new JComboBox(date);
d.setBounds(450,500,50,30);
d.setFont(new
Font("garamond",Font.BOLD,20));
d.setForeground(Color.black);
m=new JComboBox(month);
m.setBounds(525,500,80,30);
m.setFont(new
Font("garamond",Font.BOLD,14));
m.setForeground(Color.black);
y=new JComboBox(year);
y.setBounds(620,500,80,30);
y.setFont(new
Font("garamond",Font.BOLD,14));
y.setForeground(Color.black);
l11=new JLabel("Address");
l11.setBounds(250,540,200,30);
l11.setFont(new
Font("garamond",Font.BOLD,14));
l11.setForeground(Color.black);
ta=new JTextArea(5,5);
ta.setBounds(450,540,300,60);
ta.setFont(new
Font("garamond",Font.BOLD,20));
b1=new JButton("Submit");
b1.setBounds(300,620,100,30);
b1.setFont(new
Font("garamond",Font.BOLD,20));
b1.setForeground(Color.black);
b2=new JButton("Reset");
b2.setBounds(450,620,100,30);
b2.setFont(new
Font("garamond",Font.BOLD,20));
// b2.setForeground(Color.black);
exit=new JButton("Exit");
// exit.setBounds(600,620,100,30);
exit.setFont(new
Font("garamond",Font.BOLD,20));
exit.setForeground(Color.black);
c.setLayout(null);
c.add(l); c.add(t22);
c.add(l1); c.add(t1);
c.add(l2); c.add(p1);
c.add(l3); c.add(p2);
c.add(l4); c.add(t2);
c.add(l5); c.add(t3);
c.add(l6); c.add(t4);
c.add(l7); c.add(t5);
c.add(l8); c.add(t6);
c.add(l9); c.add(ts);
c.add(l10); c.add(d); c.add(m); c.add(y);
c.add(l11); c.add(ta);
c.add(b1); c.add(b2);
c.add(exit);
c.add(ld);
c.setBackground(Color.cyan);
setSize(1020,735);
setVisible(true);
}
public static void main(String a[])
{
new nuser();
}
}

// program to demonstrate list events


import javax.swing.*;
import javax.swing.event.*; // for
ListSelectionListener
import java.awt.*;
import java.awt.event.*; // Window, Action
and Item listeners
class ListDemo extends JFrame implements
ListSelectionListener
{
JList lst; // belongs to javax.swing
JTextField tf;
Container c;
public ListDemo()
{
c=getContentPane();
setTitle("ListDemo");
String str[]={ "sachin","Dravid","Dhoni"};
lst=new JList(str);
tf=new JTextField(10);
lst.addListSelectionListener(this);
c.add(lst);
c.add(tf);
c.setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
pack();//setSize(300,300);
setVisible(true);//show();
}
public void valueChanged(ListSelectionEvent e)
{
if(e.getSource()==lst)
{
tf.setText(lst.getSelectedValue().toString());
}
}
public static void main(String args[])
{
new ListDemo();
}
}

// program to find the available fonts in the


system
import java.awt.*;
import java.awt.event.*;
class KnowingFonts extends Frame implements
ActionListener,WindowListener
{
TextArea ta;
Button b;
public KnowingFonts()
{
ta=new TextArea(10,10);
b=new Button("CLICK HERE");
setLayout(new FlowLayout());
add(ta);
add(b);
addWindowListener(this);
b.addActionListener(this);
setSize(400,400);
setVisible(true);
}
public void windowOpened(WindowEvent ae){}
public void windowActivated(WindowEvent ae){}
public void windowDeactivated(WindowEvent ae)
{}
public void windowClosed(WindowEvent ae){}
public void windowClosing(WindowEvent ae)
{
System.exit(0);
}
public void windowIconified(WindowEvent ae){}
public void windowDeiconified(WindowEvent ae)
{}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b)
{
String a="Hello";
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironme
nt();
String[] s=ge.getAvailableFontFamilyNames();
for(int i=0;i<s.length;i++)
a=a+s[i]+"\n";
ta.setText(a);
}
}
public static void main(String args[])
{
new KnowingFonts();
}
}

// demonstrate tool bar


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JToolBarDemo extends JFrame
implements ActionListener
{
String str;
JLabel jl;
JButton jb1,jb2,jb3;
JToolBar jtb;
Container c;
ImageIcon ic1,ic2,ic3;
public JToolBarDemo()
{
c=getContentPane();
c.setLayout(new BorderLayout());
jtb=new JToolBar();
ic1=new ImageIcon("default.gif");
ic2=new ImageIcon("deletecursor.gif");
ic3=new ImageIcon("print_17x15.gif");
jb1=new JButton(ic1);// adding images to
buttons
jb2=new JButton(ic2);
jb3=new JButton(ic3);
jtb.add(jb1);// adding buttons to toolbar
jtb.add(jb2);
jtb.add(jb3);
jl=new JLabel("Click Here");
c.add(BorderLayout.NORTH,jtb); // adding
toolbar on container to north
c.add(BorderLayout.CENTER,jl);//
NORTH,SOUTH,EAST,WEST,CENTER
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb1)
str="Button1 Clicked";
if(ae.getSource()==jb2)
str="Button2 Clicked";
if(ae.getSource()==jb3)
str="Button3 Clicked";
jl.setText(str);
}
public static void main(String args[])
{
new JToolBarDemo();
}}
// demonstrate progress bar
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JProgressBarDemo extends JFrame
implements ActionListener
{
JButton jb;
JProgressBar bar;
Container c;
public JProgressBarDemo()
{
c=getContentPane();
bar=new JProgressBar();
jb=new JButton("Progress");
bar.setForeground(Color.cyan);
c.setLayout(new FlowLayout());
c.add(jb);
c.add(bar);
jb.addActionListener(this);
setSize(200,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb)
{
bar.setValue(bar.getValue()+5);
}
}
public static void main(String args[])
{
new JProgressBarDemo();
}
}

import java.awt.*;
import javax.swing.*;
class GLDemo extends JFrame
{
public GLDemo()
{
Container c = getContentPane();
c.setLayout(new GridLayout(2,3,10,10));
// first two specifies rows and columns, third
and fourth specifies horizontal and vertical gap
b/w components
JButton jb1,jb2,jb3,jb4,jb5;
jb1 = new JButton("Add");
jb2 = new JButton("Sub");
jb3 = new JButton("Mul");
jb4 = new JButton("Div");
jb5 = new JButton("Rem");
c.add(jb1);
c.add(jb2);
c.add(jb3);
c.add(jb4);
c.add(jb5);
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}
public static void main(String ar[])
{
new GLDemo();
}
}

//program to demonstrate calculator


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator1 extends JFrame
implements ActionListener
{
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton b1,b2,b3,b4,b5;
Container c;
public Calculator1()
{
c=getContentPane();
setTitle("Calculator");
l1=new JLabel("Number1");
l2=new JLabel("Number2");
l3=new JLabel("Result:");
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
b1=new JButton("add");
b2=new JButton("sub");
b3=new JButton("mul");
b4=new JButton("div");
b5=new JButton("exit");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
c.setLayout(new FlowLayout());
c.add(l1);
c.add(t1);
c.add(l2);
c.add(t2);
c.add(l3);
c.add(t3);
c.add(b1);
c.add(b2);
c.add(b3);
c.add(b4);
c.add(b5);
setSize(200,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}
public void actionPerformed(ActionEvent e)
{
int x,y,r;
x=Integer.parseInt(t1.getText());
y=Integer.parseInt(t2.getText());
if(e.getSource()==b1)
{
r=x+y;
t3.setText(String.valueOf(r));
}
else if(e.getSource()==b2)
{
r=x-y;
t3.setText(String.valueOf(r));
}
else if(e.getSource()==b3)
{
r=x*y;
t3.setText(String.valueOf(r));
}
else if(e.getSource()==b4)
{
r=x/y;
t3.setText(String.valueOf(r));
}
else if(e.getSource()==b5)
{
System.exit(0);
}
}
public static void main(String args[])
{
new Calculator1();
}
}

Dialogue Boxes : is a small window which is


used to provide information/accept data from
the user.
Types :
MessageDialogueBox : to display messages.
InputDialogueBox : to accept data from user.
ConfirmationDialogueBox : to display
confirmation message.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DialogDemo extends JFrame
implements ActionListener
{
JLabel lb;
JTextField tf;
JButton bmsg,binp,bext;
String str;
Container c;
public DialogDemo()
{
setTitle("Dialogue");
c=getContentPane();
lb=new JLabel("Text:");
tf=new JTextField(10);
bmsg=new JButton("Message");
binp=new JButton("Input Message");
bext=new JButton("Exit");
c.setLayout(new GridLayout(3,2,10,10));
c.add(lb);
c.add(tf);
c.add(bmsg);
c.add(binp);
c.add(bext);
bmsg.addActionListener(this);
binp.addActionListener(this);
bext.addActionListener(this);
setSize(250,250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}

public void actionPerformed(ActionEvent ae)


{
if(ae.getSource()==bmsg)
{
JOptionPane.showMessageDialog(this,"you
pressed message
button","ConfirMessage",JOptionPane.INFORM
ATION_MESSAGE);
}
else if(ae.getSource()==binp)
{
String
input=JOptionPane.showInputDialog(this,"Enter
a String :
","Accept",JOptionPane.PLAIN_MESSAGE);
tf.setText(input);
}
else if(ae.getSource()==bext)
{
int
resp=JOptionPane.showConfirmDialog(this,"Are
you sure, you want to
exit","Confirmation",JOptionPane.YES_NO_OP
TION,JOptionPane.QUESTION_MESSAGE);
if(resp==JOptionPane.YES_OPTION)
System.exit(0);
}
}
public static void main(String args[])
{
new DialogDemo();
}
}

// program to demonstrate keyboard


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class KDemo extends Applet implements
KeyListener
{
char ch;
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{}
public void keyReleased(KeyEvent ke)
{}
public void keyTyped(KeyEvent ke)
{
ch=ke.getKeyChar();
switch(ch)
{
case 'y' : setBackground(Color.yellow);break;
case 'b' : setBackground(Color.blue);break;
default : setBackground(Color.cyan);
}
}
}
/*
<applet code="KDemo.class" width=300
height=300></applet>*/
// Program to demonstrate mouse events
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class MDemo extends Applet implements
MouseListener
{
String msg;
public void init()
{
msg="";
addMouseListener(this); // register the
listener with source
}
public void mouseEntered(MouseEvent me)
{
msg="MouseEntered";
showStatus(msg); // displays text in status
bar
setBackground(Color.pink);
repaint(); // calls paint() method
}
public void mouseExited(MouseEvent me)
{
msg="MouseExited";
showStatus(msg); // displays text in status
bar
setBackground(Color.cyan);
repaint(); // calls paint() method
}
public void mousePressed(MouseEvent me)
{
msg="MousePressed";
showStatus(msg);
repaint();
}
public void mouseReleased(MouseEvent me)
{
msg="MouseReleased";
showStatus(msg);
repaint();
}
public void mouseClicked(MouseEvent me)
{
msg="MouseClicked";
showStatus(msg);
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,100,100);
}
}

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

//program to demostrate mouse movements


import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class MMDemo extends Applet
implements MouseMotionListener
{
int mx,my;
String msg;
public void init()
{
mx=0;
my=0;
msg=" ";
addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent me)
{
mx=me.getX();
my=me.getY();
msg="MouseMoved:"+mx+" "+my;
repaint();
}
public void mouseDragged(MouseEvent me)
{
mx=me.getX();
my=me.getY();
msg="MouseDragged:"+mx+" "+my;
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,mx,my);
}
}
/*
<applet code="MMDemo.class" width=300
height=300></applet>
*/
//JTree
//A control that displays a set of hierarchical
data as an outline.
// public
javax.swing.JTree(javax.swing.tree.TreeNode)
//javap
javax.swing.tree.DefaultMutableTreeNode

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

class jtree extends JFrame


{
Container cn=getContentPane();
DefaultMutableTreeNode root= new
DefaultMutableTreeNode("Computers");
DefaultMutableTreeNode sub1=new
DefaultMutableTreeNode("java");
DefaultMutableTreeNode sub2=new
DefaultMutableTreeNode("xml");

DefaultMutableTreeNode sub11=new
DefaultMutableTreeNode("J2SE");
DefaultMutableTreeNode sub12=new
DefaultMutableTreeNode("J2EE") ;

DefaultMutableTreeNode sub21=new
DefaultMutableTreeNode("xslt");
DefaultMutableTreeNode sub22=new
DefaultMutableTreeNode("xsql");

public jtree()
{
root.add(sub1);root.add(sub2);
sub1.add(sub11);sub1.add(sub12);
sub2.add(sub21);sub2.add(sub22);
JTree tr=new JTree(root);

JScrollPane sp=new JScrollPane(tr);


cn.setLayout(new BorderLayout());
cn.add(sp,BorderLayout.WEST);

setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
setSize(50,50);
setVisible(true);
}
public static void main(String[] args)
{
jtree j=new jtree();
}
}
import javax.swing.*;
import java.awt.*;
public class GBL extends JFrame
{
JButton b1,b2,b3,b4;
Container c;
GridBagLayout gbl;
GridBagConstraints gbc;
public GBL()
{
setTitle("GridBagLayoutDemo");
c=getContentPane();
b1=new JButton("Button1");
b2=new JButton("Button2");
b3=new JButton("Button3");
b4=new JButton("Button4");
gbl=new GridBagLayout();
gbc=new GridBagConstraints();
c.setLayout(gbl);
gbc.fill=GridBagConstraints.VERTICAL;
gbc.gridx=0;
gbc.gridy=0;
gbl.setConstraints(b1,gbc);
c.add(b1);
gbc.fill=GridBagConstraints.VERTICAL;
gbc.gridx=1;
gbc.gridy=0;
gbl.setConstraints(b2,gbc);
c.add(b2);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=2;
gbl.setConstraints(b4,gbc);
c.add(b4);
gbc.fill=GridBagConstraints.VERTICAL;
gbc.gridx=2;
gbc.gridy=0;
gbc.gridheight=2;
gbl.setConstraints(b3,gbc);
c.add(b3);
//pack();
//show();
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}
public static void main(String args[])
{
GBL g=new GBL();
g.pack();//g.setSize(300,300);
g.show();//g.setVisible(true);
}
}

Vous aimerez peut-être aussi