A dialog box will appear when user selects new menuitem.
import java.applet.*; import java.awt.*; import java.awt.event.*; /**/ class sampledialog extends Dialog implements ActionListener { sampledialog(Frame parent,String title) { super(parent,title,false); setLayout(new FlowLayout()); setSize(500,500); Button b1=new Button("click!"); add(b1); b1.addActionListener(this); } public void actionPerformed(ActionEvent ae) { dispose(); } public void paint(Graphics g) { g.drawString("This is in Dialog",10,20); } } class menuframe extends Frame { String msg=" "; CheckboxMenuItem first,second; public menuframe(String title) { super(title); MenuBar mb=new MenuBar(); setMenuBar(mb); Menu file=new Menu("File"); MenuItem it1,it2,it3; file.add(it1=new MenuItem("New")); file.add(it2=new MenuItem("open")); file.add(it3=new MenuItem("save")); mb.add(file); Menu edit=new Menu("Edit"); MenuItem it4,it5,it6; edit.add(it4=new MenuItem("cut")); edit.add(it5=new MenuItem("copy")); edit.add(it6=new MenuItem("paste")); mb.add(edit); mymenuhandler handler=new mymenuhandler(this); it1.addActionListener(handler); it2.addActionListener(handler); it3.addActionListener(handler); it4.addActionListener(handler); it5.addActionListener(handler); it6.addActionListener(handler); mywindowadapter adapter=new mywindowadapter(this); addWindowListener(adapter); } public void paint(Graphics g) { g.drawString(msg,100,450); } } class mywindowadapter extends WindowAdapter { menuframe menu; public mywindowadapter(menuframe menu) { this.menu=menu; } public void windowClosing(WindowEvent we) { menu.dispose(); } } class mymenuhandler implements ActionListener { menuframe menu; String msg=" "; public mymenuhandler(menuframe menu) { this.menu=menu; } public void actionPerformed(ActionEvent ae) { String msg="You selected:"; String arg=ae.getActionCommand(); if(arg.equals("New")) { msg+="New"; sampledialog d=new sampledialog(menu,"Dialogbox"); d.setVisible(true); } if(arg.equals("open")) msg+="open"; if(arg.equals("save")) msg+="save"; if(arg.equals("cut")) msg+="cut"; if(arg.equals("copy")) msg+="copy"; if(arg.equals("paste")) msg+="paste"; menu.msg=msg; menu.repaint(); } } public class dialogdemo extends Applet { Frame f; public void init() { f=new menuframe("MyFrame"); f.setSize(new Dimension(500,500)); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } }
No comments:
Post a Comment