Исходный текст приложения MenuApp
Исходный текст приложения MenuApp представлен в листинге 1.
Листинг 1
. Файл MenuApp.java
import java.awt.*;
public class MenuApp
{
public static void main(String args[])
{
MainFrameWnd frame =
new MainFrameWnd("MenuApp");
frame.setSize(
frame.getInsets().left +
frame.getInsets().right + 320,
frame.getInsets().top +
frame.getInsets().bottom + 240);
frame.show();
}
}
class MainFrameWnd extends Frame
{
MenuBar mbMainMenuBar;
Menu mnFile;
Menu mnHelp;
public MainFrameWnd(String sTitle)
{
super(sTitle);
setSize(400, 200);
setBackground(Color.yellow);
setForeground(Color.black);
setLayout(new FlowLayout());
mbMainMenuBar = new MenuBar();
mnFile = new Menu("File");
mnFile.add("New");
mnFile.add("-");
mnFile.add("Exit");
mnHelp = new Menu("Help");
mnHelp.add("Content");
mnHelp.add("-");
mnHelp.add("About");
mbMainMenuBar.add(mnFile);
mbMainMenuBar.add(mnHelp);
setMenuBar(mbMainMenuBar);
}
public void paint(Graphics g)
{
g.setFont(new Font(
"Helvetica", Font.PLAIN, 12));
g.drawString("Frame window", 10, 70);
super.paint(g);
}
public boolean handleEvent(Event evt)
{
if(evt.id == Event.WINDOW_DESTROY)
{
setVisible(false);
System.exit(0);
return true;
}
else
return super.handleEvent(evt);
}
public boolean action(Event evt, Object obj)
{
MenuItem mnItem;
if(evt.target instanceof MenuItem)
{
mnItem = (MenuItem)evt.target;
if(obj.equals("Exit"))
{
System.exit(0);
}
else if(obj.equals("New"))
{
MessageBox mbox;
mbox = new MessageBox(
"Item New selected",
this, "Dialog from Frame", true);
mbox.show();
}
else if(obj.equals("Content"))
{
MessageBox mbox;
mbox = new MessageBox(
"Item Content selected",
this, "Dialog from Frame", true);
mbox.show();
}
else if(obj.equals("About"))
{
MessageBox mbox;
mbox = new MessageBox(
"Item About selected",
this, "Dialog from Frame", true);
mbox.show();
}
else
return false;
return true;
}
return false;
}
}
class MessageBox extends Dialog
{
Label lbMsg;
Button btnOK;
public MessageBox(String sMsg,
Frame parent, String sTitle,
boolean modal)
{
super(parent, sTitle, modal);
resize(200, 100);
setLayout(new GridLayout(2, 1));
lbMsg = new Label(sMsg, Label.CENTER);
add(lbMsg);
btnOK = new Button("OK");
add(btnOK);
}
public boolean handleEvent(Event evt)
{
if(evt.id == Event.WINDOW_DESTROY)
{
dispose();
return true;
}
else
return super.handleEvent(evt);
}
public boolean action(Event evt, Object obj)
{
Button btn;
if(evt.target instanceof Button)
{
btn = (Button)evt.target;
if(evt.target.equals(btnOK))
{
dispose();
}
else
return false;
return true;
}
return false;
}
}