Académique Documents
Professionnel Documents
Culture Documents
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
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
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");
}
}
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();
}
}
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();
}
}
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);
}
/*
<applet code="MDemo.class" width=300
height=300></applet>
*/
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
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);
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);
}
}