Please send questions to
st10@humboldt.edu .
/**
* GuiApplic4
*
* this Java application with a GUI with a menu bar also does
* some painting on itself...and repaints itself when toggle button
* is pushed;
*
* 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-11-01
**/
import java.awt.*;
import java.awt.event.*;
public class GuiApplic4
{
// make use of GuiApplic4Frame class (below) to set up
// frame for this application
public static void main(String args[])
{
GuiApplic4Frame mainFrame;
Graphics g; // Graphics context for frame, so
// can paint on it;
mainFrame = new GuiApplic4Frame();
mainFrame.setTitle("GuiApplic4");
mainFrame.show();
// I'll set up size and window-closing in class GuiApplic4Frame...
// now, it turns out that any displayed component can have a
// graphics context created for it using getGraphics. However,
// it must be on-screen for this to be done --- otherwise,
// getGraphics() returns null instead of a Graphics instance.
// SO --- since mainFrame becomes displayed thanks to show()
// above, we can now get a Graphics instance for it:
g = mainFrame.getGraphics();
// and, this g can be used to call mainFrame's paint()
// method (GuiApplic4Frame's paint() method)
// Why must we call? No browser application to do it for
// us...!
mainFrame.paint(g);
}
}
// extension of Frame for this application
class GuiApplic4Frame extends Frame
{
private int count;
private Label countLabel;
private Button toggleButton;
private boolean nowGreen;
private Dialog helpBox;
// constructor for this extension of Frame
public GuiApplic4Frame()
{
Label welcome, author, version;
Button countButton;
// these will be instantiated as inner listener
// classes for the two buttons
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 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
// GuiApplic4Frame, 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 4th 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 and message");
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
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);
}
// here's paint() method for GuiApplic4Frame!
// yes, a Frame subclass can override its paint() method...
public void paint(Graphics g)
{
if (nowGreen == true)
{
g.setColor(Color.green);
g.drawString("I am a PAINTED MESSAGE!", 25, 210);
}
else
{
g.setColor(Color.orange);
g.drawString("I am painted to match toggle button", 25, 210);
}
}
// 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;
}
// can I simply call repaint() here? so that
// painted message matches toggle button background
// color? Yes! and it works!
repaint();
}
}
// 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);
}
}
}