Unit-17 Java Astract Window Toolkit-I

ILLUSTRATING LABEL CLASS IN JAVA….

Example : Labels

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.applet.*;
import java.awt.*;
/*<applet code="Labels"width=200 height=200>
</applet>
*/
public class Labels extends java.applet.Applet
{
public void init()
{
String s="Hello world";
Label L1=new Label(s,Label.LEFT);
add(L1);
Label L2=new Label(s,Label.CENTER);
add(L2);
Label L3=new Label(s,Label.RIGHT);
add(L3);
}
}
import java.applet.*; import java.awt.*; /*<applet code="Labels"width=200 height=200> </applet> */ public class Labels extends java.applet.Applet { public void init() { String s="Hello world"; Label L1=new Label(s,Label.LEFT); add(L1); Label L2=new Label(s,Label.CENTER); add(L2); Label L3=new Label(s,Label.RIGHT); add(L3); } }
import java.applet.*;
import java.awt.*;
/*<applet code="Labels"width=200 height=200>
</applet>
*/
public class Labels extends java.applet.Applet
{
public void init()
{
String s="Hello world";
Label L1=new Label(s,Label.LEFT);
add(L1);
Label L2=new Label(s,Label.CENTER);
add(L2);
Label L3=new Label(s,Label.RIGHT);
add(L3);
}
}
Output : Labels

Handling Buttons…

Example : ButtonDemo

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ButtonDemo"width=250 height=150>
</applet>
*/
public class ButtonDemo extends Applet implements ActionListener
{
String msg="";
Button yes,no,maybe;
public void init()
{
yes=new Button("Yes");
no=new Button("No");
maybe=new Button("Undecided");
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Yes"))
{
msg="You pressed Yes.";
}
else if(str.equals("No"))
{
msg="You pressed No.";
}
else {
msg="you pressed Undecided.";
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,6,100);
}
}
import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="ButtonDemo"width=250 height=150> </applet> */ public class ButtonDemo extends Applet implements ActionListener { String msg=""; Button yes,no,maybe; public void init() { yes=new Button("Yes"); no=new Button("No"); maybe=new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(str.equals("Yes")) { msg="You pressed Yes."; } else if(str.equals("No")) { msg="You pressed No."; } else { msg="you pressed Undecided."; } repaint(); } public void paint(Graphics g) { g.drawString(msg,6,100); } }
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ButtonDemo"width=250 height=150>
</applet>
*/
public class ButtonDemo extends Applet implements ActionListener
{
String msg="";
Button yes,no,maybe;
public void init()
{
yes=new Button("Yes");
no=new Button("No");
maybe=new Button("Undecided");
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Yes"))
{
msg="You pressed Yes.";
}
else if(str.equals("No"))
{
msg="You pressed No.";
}
else                                                                                                                                              {                                                                                                                                             
msg="you pressed Undecided.";
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,6,100);
}
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
Output : ButtonDemo

Checkboxes…

Example : CheckboxDemo

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CheckboxDemo"width=250 height=200>
</applet>
*/
public class CheckboxDemo extends Applet implements ItemListener
{
String msg="";
Checkbox Win2000,linux,unix,WinMe;
public void init()
{
Win2000=new Checkbox("Windows2000",null,true);
linux=new Checkbox("Linux");
unix=new Checkbox("Unix");
WinMe=new Checkbox("Mac OS");
add(Win2000);
add(linux);
add(unix);
add(WinMe);
Win2000.addItemListener(this);
linux.addItemListener(this);
unix.addItemListener(this);
WinMe.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g){
msg="Current state:";
g.drawString(msg,6,80);
msg="Windows2000:"+Win2000.getState();
g.drawString(msg,6,100);
msg="Linux:"+linux.getState();
g.drawString(msg,6,120);
msg="Unix:"+unix.getState();
g.drawString(msg,6,140);
msg="MacOS:"+WinMe.getState();
g.drawString(msg,6,160);
}
}
import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="CheckboxDemo"width=250 height=200> </applet> */ public class CheckboxDemo extends Applet implements ItemListener { String msg=""; Checkbox Win2000,linux,unix,WinMe; public void init() { Win2000=new Checkbox("Windows2000",null,true); linux=new Checkbox("Linux"); unix=new Checkbox("Unix"); WinMe=new Checkbox("Mac OS"); add(Win2000); add(linux); add(unix); add(WinMe); Win2000.addItemListener(this); linux.addItemListener(this); unix.addItemListener(this); WinMe.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g){ msg="Current state:"; g.drawString(msg,6,80); msg="Windows2000:"+Win2000.getState(); g.drawString(msg,6,100); msg="Linux:"+linux.getState(); g.drawString(msg,6,120); msg="Unix:"+unix.getState(); g.drawString(msg,6,140); msg="MacOS:"+WinMe.getState(); g.drawString(msg,6,160); } }
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CheckboxDemo"width=250 height=200>
</applet>
*/
public class CheckboxDemo extends Applet implements ItemListener
{
String msg="";
Checkbox Win2000,linux,unix,WinMe;
public void init()
{
Win2000=new Checkbox("Windows2000",null,true);
linux=new Checkbox("Linux");
unix=new Checkbox("Unix");
WinMe=new Checkbox("Mac OS");
add(Win2000);
add(linux);
add(unix);
add(WinMe);
Win2000.addItemListener(this);
linux.addItemListener(this);
unix.addItemListener(this);
WinMe.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g){
msg="Current state:";
g.drawString(msg,6,80);
msg="Windows2000:"+Win2000.getState();
g.drawString(msg,6,100);
msg="Linux:"+linux.getState();
g.drawString(msg,6,120);
msg="Unix:"+unix.getState();
g.drawString(msg,6,140);
msg="MacOS:"+WinMe.getState();
g.drawString(msg,6,160);
}
}
Output : CheckboxDemo

Radio Buttons…

Example : CBGroup

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CBGroup"width=250 height=200>
</applet>
*/
public class CBGroup extends Applet implements ItemListener
{
String msg="";
Checkbox Win98,linux,unix,WindowsMe;
CheckboxGroup cbg,cbg1;
public void init()
{
cbg =new CheckboxGroup();
cbg1 =new CheckboxGroup();
Win98=new Checkbox("Windows 98",cbg,true);
linux=new Checkbox("Linux",cbg,false);
unix=new Checkbox("Unix",cbg1,true);
WindowsMe=new Checkbox("WindowsME",cbg1,false);
add(Win98);
add(linux);
add(unix);
add(WindowsMe);
Win98.addItemListener(this);
linux.addItemListener(this);
unix.addItemListener(this);
WindowsMe.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg="Selection1:";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,6,100);
msg="Selection2:";
msg+=cbg1.getSelectedCheckbox().getLabel();
g.drawString(msg,6,120);
}
}
import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="CBGroup"width=250 height=200> </applet> */ public class CBGroup extends Applet implements ItemListener { String msg=""; Checkbox Win98,linux,unix,WindowsMe; CheckboxGroup cbg,cbg1; public void init() { cbg =new CheckboxGroup(); cbg1 =new CheckboxGroup(); Win98=new Checkbox("Windows 98",cbg,true); linux=new Checkbox("Linux",cbg,false); unix=new Checkbox("Unix",cbg1,true); WindowsMe=new Checkbox("WindowsME",cbg1,false); add(Win98); add(linux); add(unix); add(WindowsMe); Win98.addItemListener(this); linux.addItemListener(this); unix.addItemListener(this); WindowsMe.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { msg="Selection1:"; msg+=cbg.getSelectedCheckbox().getLabel(); g.drawString(msg,6,100); msg="Selection2:"; msg+=cbg1.getSelectedCheckbox().getLabel(); g.drawString(msg,6,120); } }
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CBGroup"width=250 height=200>
</applet>
*/
public class CBGroup extends Applet implements ItemListener
{
String msg="";
Checkbox Win98,linux,unix,WindowsMe;
CheckboxGroup cbg,cbg1;
public void init()
{
cbg =new CheckboxGroup();
cbg1 =new CheckboxGroup();
Win98=new Checkbox("Windows 98",cbg,true);
linux=new  Checkbox("Linux",cbg,false);
unix=new Checkbox("Unix",cbg1,true);
WindowsMe=new Checkbox("WindowsME",cbg1,false);
add(Win98);
add(linux);
add(unix);
add(WindowsMe);
Win98.addItemListener(this);
linux.addItemListener(this);
unix.addItemListener(this);
WindowsMe.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg="Selection1:";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,6,100);
msg="Selection2:";
msg+=cbg1.getSelectedCheckbox().getLabel();
g.drawString(msg,6,120);
}
}
Output : CBGroup

Choice menus…

Example : ChoiceDemo

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ChoiceDemo.java" width=300 height=180>
</applet>
*/
public class ChoiceDemo extends Applet implementsItemListener
{
Choice os,browser;
String msg="";
public void init()
{
os=new Choice();
browser=new Choice();
os.add("Windows2000");
os.add("Windows ME");
os.add("Linux");
os.add("Unix");
browser.add("Netscape 1.1");
browser.add("Netscape 2.x");
browser.add("Netscape 3.x");
os.add("MacOS");
browser.add("Internet Explorer 2.0");
browser.add("Internet Explorer 3.0");
browser.add("Internet Explorer 4.0");
browser.add("Lynx 2.4");
browser.add("Netscape 4.x");
add(os);
add(browser);
os.addItemListener(this);
browser.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg="Current OS:";
msg+=os.getSelectedItem();
g.drawString(msg,6,120);
msg="Current Browser:";
msg+=browser.getSelectedItem();
g.drawString(msg,6,140);
}
}
import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="ChoiceDemo.java" width=300 height=180> </applet> */ public class ChoiceDemo extends Applet implementsItemListener { Choice os,browser; String msg=""; public void init() { os=new Choice(); browser=new Choice(); os.add("Windows2000"); os.add("Windows ME"); os.add("Linux"); os.add("Unix"); browser.add("Netscape 1.1"); browser.add("Netscape 2.x"); browser.add("Netscape 3.x"); os.add("MacOS"); browser.add("Internet Explorer 2.0"); browser.add("Internet Explorer 3.0"); browser.add("Internet Explorer 4.0"); browser.add("Lynx 2.4"); browser.add("Netscape 4.x"); add(os); add(browser); os.addItemListener(this); browser.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { msg="Current OS:"; msg+=os.getSelectedItem(); g.drawString(msg,6,120); msg="Current Browser:"; msg+=browser.getSelectedItem(); g.drawString(msg,6,140); } }
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ChoiceDemo.java" width=300 height=180>
</applet>
*/
public class ChoiceDemo extends Applet implementsItemListener
{
Choice os,browser;
String msg="";
public void init()
{
os=new Choice();
browser=new Choice();
os.add("Windows2000");
os.add("Windows ME");
os.add("Linux");
os.add("Unix");
browser.add("Netscape 1.1");
browser.add("Netscape 2.x");
browser.add("Netscape 3.x");
os.add("MacOS");
browser.add("Internet Explorer 2.0");
browser.add("Internet Explorer 3.0");
browser.add("Internet Explorer 4.0");
browser.add("Lynx 2.4");
browser.add("Netscape 4.x");
add(os);
add(browser);
os.addItemListener(this);
browser.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg="Current OS:";
msg+=os.getSelectedItem();
g.drawString(msg,6,120);
msg="Current Browser:";
msg+=browser.getSelectedItem();
g.drawString(msg,6,140);
}
}

Output : ChoiceDemo

Handling Lists…

Example : ListEvents

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="ListEvents" width=400 height=200>
</applet>
*/
public class ListEvents extends Applet implements ActionListener,ItemListener
{
TextArea ta;
public void init()
{
List l=new List();
l.add("Monitor");
l.add("Keyboard");
l.add("Mouse");
l.add("Light pen");
l.add("joy strick");
l.add("printer");
l.add("scanner");
l.addActionListener(this);
l.addItemListener(this);
add(l);
ta=new TextArea(10,20);
add(ta);
}
public void actionPerformed(ActionEvent ae)
{
ta.append("Action Event:"+ae.getActionCommand()+"\n");
}
public void itemStateChanged(ItemEvent ie)
{
List l=(List)ie.getItemSelectable();
ta.append("ItemEvent:"+l.getSelectedItem()+"\n");
}
}
import java.applet.*; import java.awt.*; import java.awt.event.*; /* <applet code="ListEvents" width=400 height=200> </applet> */ public class ListEvents extends Applet implements ActionListener,ItemListener { TextArea ta; public void init() { List l=new List(); l.add("Monitor"); l.add("Keyboard"); l.add("Mouse"); l.add("Light pen"); l.add("joy strick"); l.add("printer"); l.add("scanner"); l.addActionListener(this); l.addItemListener(this); add(l); ta=new TextArea(10,20); add(ta); } public void actionPerformed(ActionEvent ae) { ta.append("Action Event:"+ae.getActionCommand()+"\n"); } public void itemStateChanged(ItemEvent ie) { List l=(List)ie.getItemSelectable(); ta.append("ItemEvent:"+l.getSelectedItem()+"\n"); } }
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="ListEvents" width=400 height=200>
</applet>
*/
public class ListEvents extends Applet implements ActionListener,ItemListener
{
TextArea ta;
public void init()
{
List l=new List();
l.add("Monitor");
l.add("Keyboard");
l.add("Mouse");
l.add("Light pen");
l.add("joy strick");
l.add("printer");
l.add("scanner");
l.addActionListener(this);
l.addItemListener(this);
add(l);
ta=new TextArea(10,20);
add(ta);
}
public void actionPerformed(ActionEvent ae)
{
ta.append("Action Event:"+ae.getActionCommand()+"\n");
}
public void itemStateChanged(ItemEvent ie)
{
List l=(List)ie.getItemSelectable();
ta.append("ItemEvent:"+l.getSelectedItem()+"\n");
}
}
Output : ListEvents

Text Fields…

Example : TextFieldDemo

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="TextFieldDemo"width=380 height=150>
</applet>
*/
public class TextFieldDemo extends Applet implements ActionListener
{
TextField name,pass;
public void init()
{
Label namep=new Label("Name:",Label.RIGHT);
Label passp=new Label("Password:",Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.setEchoChar('?');
add(namep);
add(name);
add(passp);
add(pass);
name.addActionListener(this);
pass.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString("Name:"+name.getText(),6,60);
g.drawString("Selected text in name:"+name.getSelectedText(),6,80);
g.drawString("Password:"+pass.getText(),6,100);
}
}
import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="TextFieldDemo"width=380 height=150> </applet> */ public class TextFieldDemo extends Applet implements ActionListener { TextField name,pass; public void init() { Label namep=new Label("Name:",Label.RIGHT); Label passp=new Label("Password:",Label.RIGHT); name=new TextField(12); pass=new TextField(8); pass.setEchoChar('?'); add(namep); add(name); add(passp); add(pass); name.addActionListener(this); pass.addActionListener(this); } public void actionPerformed(ActionEvent ae) { repaint(); } public void paint(Graphics g) { g.drawString("Name:"+name.getText(),6,60); g.drawString("Selected text in name:"+name.getSelectedText(),6,80); g.drawString("Password:"+pass.getText(),6,100); } }
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="TextFieldDemo"width=380 height=150>
</applet>
*/
public class TextFieldDemo extends Applet implements ActionListener
{
TextField name,pass;
public void init()
{
Label namep=new Label("Name:",Label.RIGHT);
Label passp=new Label("Password:",Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.setEchoChar('?');
add(namep);
add(name);
add(passp);
add(pass);
name.addActionListener(this);
pass.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString("Name:"+name.getText(),6,60);
g.drawString("Selected text in name:"+name.getSelectedText(),6,80);
g.drawString("Password:"+pass.getText(),6,100);
}
}
Output : TextFieldDemo

Text Areas…

Example : TextFieldEvents

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="TextFieldEvents" width=400 height=200>
</applet>
*/
public class TextFieldEvents extends Applet implements ActionListener,TextListener
{
TextArea ta;
TextField tf;
public void init()
{
tf=new TextField(20);
tf.addActionListener(this);
tf.addTextListener(this);
add(tf);
ta=new TextArea(10,20);
add(ta);
}
public void actionPerformed(ActionEvent ae)
{
ta.append("ActionEvent:"+ae.getActionCommand()+"\n");
}
public void textValueChanged(TextEvent te)
{
ta.setText("TextEvent :"+tf.getText()+"\n");
}
}
import java.applet.*; import java.awt.*; import java.awt.event.*; /* <applet code="TextFieldEvents" width=400 height=200> </applet> */ public class TextFieldEvents extends Applet implements ActionListener,TextListener { TextArea ta; TextField tf; public void init() { tf=new TextField(20); tf.addActionListener(this); tf.addTextListener(this); add(tf); ta=new TextArea(10,20); add(ta); } public void actionPerformed(ActionEvent ae) { ta.append("ActionEvent:"+ae.getActionCommand()+"\n"); } public void textValueChanged(TextEvent te) { ta.setText("TextEvent :"+tf.getText()+"\n"); } }
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="TextFieldEvents" width=400 height=200>
</applet>
*/
public class TextFieldEvents extends Applet implements ActionListener,TextListener
{
TextArea ta;
TextField tf;
public void init()
{
tf=new TextField(20);
tf.addActionListener(this);
tf.addTextListener(this);
add(tf);
ta=new TextArea(10,20);
add(ta);
}
public void actionPerformed(ActionEvent ae)
{
ta.append("ActionEvent:"+ae.getActionCommand()+"\n");
}
public void textValueChanged(TextEvent te)
{
ta.setText("TextEvent :"+tf.getText()+"\n");
}
}
Output : TextFieldEvents