Tuesday, February 14, 2017

Menu and Dialog

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MD" width=250 height=250>
</applet>
*/

// Create a subclass of Frame

class MenuFrame extends Frame implements ActionListener
{

String msg = "";

CheckboxMenuItem debug, test;

MenuFrame(String title)
{

super(title);
// create menu bar and add it to frame

MenuBar mbar = new MenuBar();

setMenuBar(mbar);
// create the menu items

Menu file = new Menu("File");

MenuItem item1, item2, item3, item4, item5;

file.add(item1 = new MenuItem("New..."));

file.add(item2 = new MenuItem("Open..."));

file.add(item3 = new MenuItem("Close"));

file.add(item4 = new MenuItem("-"));

file.add(item5 = new MenuItem("Quit..."));

mbar.add(file);

Menu edit = new Menu("Edit");

MenuItem item6, item7, item8, item9;

edit.add(item6 = new MenuItem("Cut"));

edit.add(item7 = new MenuItem("Copy"));

edit.add(item8 = new MenuItem("Paste"));

edit.add(item9 = new MenuItem("-"));

Menu sub = new Menu("Special");

MenuItem item10, item11, item12;

sub.add(item10 = new MenuItem("First"));

sub.add(item11 = new MenuItem("Second"));

sub.add(item12 = new MenuItem("Third"));

edit.add(sub);

// these are checkable menu items

debug = new CheckboxMenuItem("Debug");

edit.add(debug);

test = new CheckboxMenuItem("Testing");

edit.add(test);

mbar.add(edit);
item1.addActionListener(this);

}
public void actionPerformed(ActionEvent ae)
{
SampleDialog d=new SampleDialog(this,"NEW DIALOG BOX");
d.setVisible(true);
}
}
class SampleDialog extends Dialog implements ActionListener
{
SampleDialog(Frame parent, String title)
{
super(parent, title, false);
setLayout(new FlowLayout());
setSize(300, 200);
add(new Label("Press this button:"));
Button b;
add(b = new Button("Cancel"));
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
dispose();
}
public void paint(Graphics g) {
g.drawString("This is in the dialog box", 10, 70);
}
}


public class MD extends Applet
{
Frame f;

public void init()
{
f = new MenuFrame("Menu Demo");

int width = Integer.parseInt(getParameter("width"));

int height = Integer.parseInt(getParameter("height"));

f.setSize(width, height);

f.setVisible(true);
}

public void start()
{
f.setVisible(true);
}

public void stop()
{
f.setVisible(false);
}

}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home