81 Program to demonstrate an Adapter
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AdapterDemo extends Applet {
public void init() {
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
}}
class MyMouseAdapter extends MouseAdapter{
AdapterDemo adapterDemo;
public MyMouseAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}
public void mouseClicked(MouseEvent me) {
adapterDemo.showStatus("Mouse clicked");
}}
class MyMouseMotionAdapter extends MouseMotionAdapter{
AdapterDemo adapterDemo;
public MyMouseMotionAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}
public void mouseDragged(MouseEvent me) {
adapterDemo.showStatus("Mouse dragged");
}}
82 Program to Demonstrate Button
import java.awt.*;
import java.awt.event.*;
public class demobutton extends Applet implements ActionListener
{
String msg="";
Button b1,b2;
public void init()
{
b1=new Button("OK");
b2=new Button("CANCEL");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("OK"))
{
msg="You pressed OK";
}
else
{
msg="You pressed CANCEL";
}
repaint();
}
public void paint(Graphics g){
g.drawString(msg,40,40);
}
}
83 Program to Demonstrate Checkbox
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class democheckbox extends Applet implements ItemListener {
String msg="";
Checkbox c1,c2,c3;
public void init() {
c1=new Checkbox("JAVA",null,true);
c2=new Checkbox("VB");
c3=new Checkbox("ORACLE");
add(c1);
add(c2);
add(c3);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
}
public void itemStateChanged(ItemEvent e) {
repaint();
}
public void paint(Graphics g) {
msg="JAVA:"+c1.getState();
g.drawString(msg,40,40);
msg="VB:"+c2.getState();
g.drawString(msg,40,60);
msg="ORACLE:"+c3.getState();
g.drawString(msg,40,80);
}}
84 Program to Demonstrate Radio Buttons (CheckboxGroup)
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class democheckboxgrp extends Applet implements ItemListener {
String msg="";
Checkbox c1,c2,c3;
CheckboxGroup cg;
public void init() {
cg=new CheckboxGroup();
c1=new Checkbox("JAVA",cg,true);
c2=new Checkbox("VB",cg,false);
c3=new Checkbox("ORACLE",cg,false);
add(c1);
add(c2);
add(c3);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
}
public void itemStateChanged(ItemEvent e) {
repaint();
}
public void paint(Graphics g) {
msg="curently selected:"+cg.getSelectedCheckbox().getLabel();
g.drawString(msg,40,40);
}
}
85 Program to Demonstrate Choice
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class demochoice extends Applet implements ItemListener {
Choice c;
public void init() {
c=new Choice();
c.addItem("Red");
c.addItem("Green");
c.addItem("Blue");
add(c);
c.addItemListener(this); }
public void itemStateChanged(ItemEvent e) {
String st=c.getSelectedItem();
if(st.equals("Red")) {
setBackground(Color.red); }
if(st.equals("Green")) {
setBackground(Color.green); }
if(st.equals("Blue")) {
setBackground(Color.blue);}}}
86 Program to Demonstrate File Dialog
import java.awt.event.*;
import java.applet.*;
class TestMenu extends Frame {
String msg="";
CheckboxMenuItem workoff;
TestMenu(String title) {
super(title);
MenuBar mbar=new MenuBar();
setMenuBar(mbar);
Menu file=new Menu("File");
MenuItem i1,i2,i3,i4,i5;
file.add(i1=new MenuItem("New...."));
file.add(i2=new MenuItem("Open...."));
file.add(i3=new MenuItem("Save...."));
file.add(i4=new MenuItem("-"));
file.add(i5=new MenuItem("Quit...."));
mbar.add(file);
Menu edit=new Menu("Edit");
MenuItem i6,i7,i8,i9;
edit.add(i6=new MenuItem("Cut...."));
edit.add(i7=new MenuItem("Copy...."));
edit.add(i8=new MenuItem("Paste...."));
edit.add(i9=new MenuItem("-"));
Menu submenu1=new Menu("Text Size...");
MenuItem i10,i11;
submenu1.add(i10=new MenuItem("Large...."));
submenu1.add(i11=new MenuItem("Small...."));
edit.add(submenu1);
workoff=new CheckboxMenuItem("Work Offline");
edit.add(workoff);
mbar.add(edit);
Mymenuhandler handler=new Mymenuhandler(this);
i1.addActionListener(handler);
i2.addActionListener(handler);
i3.addActionListener(handler);
i5.addActionListener(handler);
i6.addActionListener(handler);
i7.addActionListener(handler);
i8.addActionListener(handler);
i10.addActionListener(handler);
i11.addActionListener(handler);
workoff.addItemListener(handler);
}
public void paint(Graphics g) {
g.drawString(msg,10,100);
if(workoff.getState())
g.drawString("Checked",10,200);
else
g.drawString("Unchecked",10,200);
}}
class Mymenuhandler implements ActionListener,ItemListener{
TestMenu tm;
Mymenuhandler(TestMenu tm) {
this.tm=tm;
}
public void actionPerformed(ActionEvent ae) {
String msg=" You selected";
String s=ae.getActionCommand();
if(s.equals("New...."))
msg="New";
else if(s.equals("Quit....")) {
TestDialog d=new TestDialog(tm,"Confirm Exit Dialog");
d.setVisible(true);
}
else if(s.equals("Open....")) {
FileDialog fopen=new FileDialog(tm,"Open a File",FileDialog.LOAD);
fopen.setVisible(true);
msg="You selected "+fopen.getDirectory()+" "+fopen.getFile();
}
else if(s.equals("Save....")) {
FileDialog fsave=new FileDialog(tm,"Save a File",FileDialog.SAVE);
fsave.setVisible(true);
}}
public void itemStateChanged(ItemEvent ie) {
tm.repaint();
}}
class TestDialog extends Dialog implements ActionListener {
Frame f;
String s;
Button b,b1;
TestDialog(Frame f,String title) {
super(f,title,false);
this.f=f;
this.s=title;
setLayout(new FlowLayout());
setSize(200,400);
add(new Label("Are you sure you want to exit"));
add(b=new Button("Yes"));
add(b1=new Button("No"));
b.addActionListener(this);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b){
dispose();
f.setVisible(false);
}
else {
dispose();
}}}
public class DemoFileDialog extends Applet {
Frame f;
public void init() {
f=new TestMenu("My Menu Frame");
f.setSize(300,500);
f.setVisible(true); }
public void start() {
f.setVisible(true);
}
public void stop() {
f.setVisible(false);
}}
87 Program to Demonstrate Labels
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class demolabel extends Applet
{
public void init()
{
Label nm=new Label("Name");
Label addr=new Label("Address");
//add labels to window
add(nm);
add(addr);
}
}
88 Program to Demonstrate Lists
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ListDemo extends Applet implements ActionListener {
List os,browser;
String msg= "";
public void init() {
os=new List(4,true);
browser=new List(4,false);
os.add("windows 98");
os.add("Windows NT");
os.add("Solaris");
os.add("MacOS");
browser.add("Netscape 1.1");
browser.add("Netscape 2.x");
browser.add("Netscape 3.x");
browser.add("Netscape 4.x");
browser.add("Internet Explorer 2.0");
browser.add("Internet Explorer 3.0");
browser.add("Internet Explorer 4.0");
browser.add("Lynx 2.4");
browser.select(1);
add(os);
add(browser);
os.addActionListener(this);
browser.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
repaint();
}
public void paint(Graphics g) {
int idx[];
msg="Current OS:";
idx=os.getSelectedIndexes();
for(int i=0; i
msg += os.getItem(idx[i]) + " ";
g.drawString(msg,6,120);
msg="Current Browser: ";
msg += browser.getSelectedItem();
g.drawString(msg,6,140);
}}
89 Program to Demonstrate Menu
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class TestMenu extends Frame { String msg="";
CheckboxMenuItem workoff;
TestMenu(String title) {
super(title);
MenuBar mbar=new MenuBar();
setMenuBar(mbar);
Menu file=new Menu("File");
MenuItem i1,i2,i3,i4,i5;
file.add(i1=new MenuItem("New...."));
file.add(i2=new MenuItem("Open...."));
file.add(i3=new MenuItem("Close...."));
file.add(i4=new MenuItem("-"));
file.add(i5=new MenuItem("Quit...."));
mbar.add(file);
Menu edit=new Menu("Edit");
MenuItem i6,i7,i8,i9;
edit.add(i6=new MenuItem("Cut...."));
edit.add(i7=new MenuItem("Copy...."));
edit.add(i8=new MenuItem("Paste...."));
edit.add(i9=new MenuItem("-"));
Menu submenu1=new Menu("Text Size...");
MenuItem i10,i11;
submenu1.add(i10=new MenuItem("Large...."));
submenu1.add(i11=new MenuItem("Small...."));
edit.add(submenu1);
workoff=new CheckboxMenuItem("Work Offline");
edit.add(workoff);
mbar.add(edit);
Mymenuhandler handler=new Mymenuhandler(this);
i1.addActionListener(handler);
i2.addActionListener(handler);
i3.addActionListener(handler);
i5.addActionListener(handler);
i6.addActionListener(handler);
i7.addActionListener(handler);
i8.addActionListener(handler);
i10.addActionListener(handler);
i11.addActionListener(handler);
workoff.addItemListener(handler);
}
public void paint(Graphics g)
{
g.drawString(msg,10,200);
if(workoff.getState())
g.drawString("Checked",10,200);
else
g.drawString("Unchecked",10,200);
}}
class Mymenuhandler implements ActionListener,ItemListener {
TestMenu tm;
Mymenuhandler(TestMenu tm) {
this.tm=tm;
}
public void actionPerformed(ActionEvent ae) {
String msg=" You selected";
String s=ae.getActionCommand();
if(s.equals("New...."))
msg="New";
else if(s.equals("Quit....")) {
//activate a dialog box
TestDialog d=new TestDialog(tm,"Confirm Exit Dialog");
d.setVisible(true);
} }
public void itemStateChanged(ItemEvent ie) {
tm.repaint();
}}
class TestDialog extends Dialog implements ActionListener {
Frame f;
String s;
Button b,b1;
TestDialog(Frame f,String title){
super(f,title,false);
this.f=f;
this.s=title;
setLayout(new FlowLayout());
setSize(300,200);
add(new Label("Are you sure you want to exit"));
add(b=new Button("Yes"));
add(b1=new Button("No"));
b.addActionListener(this);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b){
dispose();
f.setVisible(false);
}
else {
dispose();
}}}
public class DemoMenu extends Applet {
Frame f;
public void init() {
f=new TestMenu("My Menu Frame");
f.setSize(300,500);
f.setVisible(true);
}
public void start() {
f.setVisible(true);
}
public void stop() {
f.setVisible(false);
}}90 Program to Demonstrate Text Area
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class DemoTextArea extends Applet implements ActionListener {
TextArea remark;
Button b1;
String msg="";
public void init() {
remark=new TextArea("Enter your remarks",5,50);
b1=new Button("Show");
add(remark);
add(b1);
b1.addActionListener(this);}
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if(s.equals("Show")) {
msg=remark.getText();
repaint(); }}
public void paint(Graphics g){
g.drawString(msg,130,160); }}
0 comments:
Post a Comment