Please send questions to
st10@humboldt.edu .
/**
* GuiApplic3
*
* this Java application with a GUI adds a menu bar.
*
* it does so using code borrowed and adapted from "Java Examples in
* a Nutshell", FIRST EDITION, Flanagan, O'Reilly, pp. 124-127,
* in Example 6-11, MenuScribble.java
*
* modified by: Sharon M. Tuttle
* last modified: 2-5-01
**/
import java.awt.*;
import java.awt.event.*;
public class GuiApplic3
{
// make use of GuiApplic3Frame class (below) to set up
// frame for this application
public static void main(String args[])
{
GuiApplic3Frame mainFrame = new GuiApplic3Frame();
mainFrame.setTitle("GuiApplic3");
mainFrame.show();
// I'll set up size and window-closing in class
// GuiApplic3Frame...
}
}
// extension of Frame for this application
class GuiApplic3Frame extends Frame
{
private int count;
private Label countLabel;
private Button toggleButton, countButton;
private boolean nowGreen;
private Dialog helpBox;
// constructor for this extension of Frame
public GuiApplic3Frame()
{
Label welcome, author, version;
// these will be instantiated as inner listener
// classes for the two buttons and the menu!
ActionListener ctListener, toggListener, menuListener;
// we'll have a menu bar, for this frame subclass
MenuBar menuBar;
// pulldown menus for menuBar
Menu fileMenu, colorsMenu, helpMenu;
// menu items for the menus
// ...for fileMenu
MenuItem quitItem;
// ...for colorsMenu
MenuItem blueItem, redItem, greenItem;
// ...for helpMenu
MenuItem blurbItem;
// these are related to help dialog that will appear if
// help blurb is requested
Label helpLabel; // "text" for help dialog box
Button closeHelp; // button to get rid of help dialog box
CloseHelpListener closeListener; // to close help dialog box
// set up window-closing for the instance of this
// GuiApplic3Frame, again with an anonymous inner class
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
// changing from BorderLayout default to FlowLayout
setLayout(new FlowLayout());
setBackground(Color.lightGray);
// these are simply labels, to start.
welcome = new Label("Welcome to a 3rd Application with a GUI");
welcome.setForeground(Color.black);
add(welcome);
author = new Label("Sharon M. Tuttle");
author.setForeground(Color.red);
add(author);
version = new Label("Version 1.3");
version.setForeground(Color.cyan);
add(version);
countButton = new Button("count clicks");
add(countButton);
// instantiate ActionListener ctListener to be an
// instance of inner class CountListener, add
// ctListener as an action listener to countButton
count = 0;
ctListener = new CountListener();
countButton.addActionListener(ctListener);
countLabel = new Label("count clicks button pressed: 0 times");
add(countLabel);
nowGreen = true;
toggleButton = new Button("toggle color");
toggleButton.setBackground(Color.green);
add(toggleButton);
// instantiate ActionListener toggListener to be an
// instance of inner class ToggleListener, add
// toggListener as an action listener to toggleButton
toggListener = new ToggleListener();
toggleButton.addActionListener(toggListener);
// set the size for the instance of this GuiApplic2Frame
setSize(250, 200);
// create a menubar and tell the frame about it
menuBar = new MenuBar();
setMenuBar(menuBar);
// here's an instantiation of the inner class for handling
// the menu actions
menuListener = new MenuListener();
// create pulldown menus for the menubar
fileMenu = new Menu("File");
colorsMenu = new Menu("Colors");
helpMenu = new Menu("Help");
// add the menus to the menubar
menuBar.add(fileMenu);
menuBar.add(colorsMenu);
menuBar.add(helpMenu);
// remember to treat Help menu separately, also calling
// setHelpMenu()
menuBar.setHelpMenu(helpMenu);
// add an item, with keyboard shortcut, to the File menu
// (this lets control-q quit the application, also)
quitItem = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q));
quitItem.addActionListener(menuListener);
// did you know that you could SET the desired action command??
quitItem.setActionCommand("quit");
fileMenu.add(quitItem);
// add color option items, without keyboard shortcuts,
// to the Colors menu
blueItem = new MenuItem("Blue");
blueItem.addActionListener(menuListener);
blueItem.setActionCommand("blue");
colorsMenu.add(blueItem);
redItem = new MenuItem("Red");
redItem.addActionListener(menuListener);
redItem.setActionCommand("red");
colorsMenu.add(redItem);
greenItem = new MenuItem("Green");
greenItem.addActionListener(menuListener);
greenItem.setActionCommand("green");
colorsMenu.add(greenItem);
blurbItem = new MenuItem("Show Help Blurb");
blurbItem.addActionListener(menuListener);
blurbItem.setActionCommand("blurb");
helpMenu.add(blurbItem);
// set up Help dialog box to be shown if blurb
// requested, BUT do not make it visible!
// (some of you saw Dialog boxes in CIS 235...)
helpBox = new Dialog(this, "Help");
helpBox.setBackground(Color.yellow);
helpBox.setSize(700, 200);
// "text" of help dialog box
helpLabel = new Label("You are not missing anything, " +
"this application really " +
"DOES do very little right now...");
helpBox.add(helpLabel, "Center");
// button to close help dialog box
closeHelp = new Button("click when done reading");
helpBox.add(closeHelp, "South");
closeListener = new CloseHelpListener();
closeHelp.addActionListener(closeListener);
// help blurb dialog will NOT show yet, because it has
// NOT been set to be visible; (by default, it starts
// out invisible)
}
// this special listener handles counting button
private class CountListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
count++;
countLabel.setText("count clicks button pressed: " +
count + " times");
}
}
// this special listener handles toggling the toggle
// button's background color
private class ToggleListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (nowGreen)
{
toggleButton.setBackground(Color.orange);
nowGreen = false;
}
else
{
toggleButton.setBackground(Color.green);
nowGreen = true;
}
}
}
// this special listener handles the frame's menu bar
private class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String cmd; // action command for this action
cmd = event.getActionCommand();
if (cmd.equals("quit"))
{
// note! this is ANOTHER way to quit this application,
// now, BESIDES closing the window;
System.exit(0);
}
else if (cmd.equals("blue"))
{
setBackground(Color.blue);
}
else if (cmd.equals("red"))
{
setBackground(Color.red);
}
else if (cmd.equals("green"))
{
setBackground(Color.green);
}
else if (cmd.equals("blurb"))
{
helpBox.setVisible(true);
}
}
}
// this special listener handles closing the help dialog box
private class CloseHelpListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
helpBox.setVisible(false);
}
}
}