home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java by Example
/
jbecd.bin
/
JBE-CD
/
NTUsers
/
JBECODE.ZIP
/
JavaByExample
/
chap23
/
MenuFrameApplet.java
< prev
next >
Wrap
Text File
|
1996-03-07
|
3KB
|
109 lines
import java.awt.*;
import java.applet.*;
public class MenuFrameApplet extends Applet
{
MenuFrameFrame frame;
Button button;
public void init()
{
frame = new MenuFrameFrame("MenuBar Window");
button = new Button("Show Window");
add(button);
}
public boolean action(Event evt, Object arg)
{
boolean visible = frame.isShowing();
if (visible)
{
frame.hide();
button.setLabel("Show Window");
}
else
{
frame.show();
button.setLabel("Hide Window");
}
return true;
}
}
class MenuFrameFrame extends Frame
{
MenuBar menuBar;
String str;
Frame frame;
MenuFrameFrame(String title)
{
super(title);
menuBar = new MenuBar();
setMenuBar(menuBar);
Menu menu = new Menu("Test");
menuBar.add(menu);
MenuItem item = new MenuItem("Command 1");
menu.add(item);
item = new MenuItem("Command 2");
menu.add(item);
item = new MenuItem("-");
menu.add(item);
CheckboxMenuItem checkItem =
new CheckboxMenuItem("Check");
menu.add(checkItem);
menu = new Menu("View");
menuBar.add(menu);
checkItem = new CheckboxMenuItem("Window");
menu.add(checkItem);
str = "";
Font font = new Font("TimesRoman", Font.BOLD, 20);
setFont(font);
frame = new Frame("Second Window");
}
public void paint(Graphics g)
{
resize(300, 250);
g.drawString(str, 20, 100);
}
public boolean action(Event evt, Object arg)
{
if (evt.target instanceof MenuItem)
{
if (arg == "Command 1")
str = "You selected Command 1";
else if (arg == "Command 2")
str = "You selected Command 2";
else if (arg == "Check")
str = "You selected the Check item";
else if (arg == "Window")
ShowHideWindow();
repaint();
return true;
}
else
return false;
}
protected void ShowHideWindow()
{
boolean visible = frame.isShowing();
if (visible)
frame.hide();
else
{
frame.show();
frame.resize(150, 150);
}
}
}