/*
HelloSwing.java CS 585 example
Mike Barnes 9/4/02
This example also demonstrates the use of Java's javadoc tool.
Example Javadoc comments follow.
The Java docs for HelloSwing were generated with the following
command line (in the directory where the file HelloSwing.java exists.
$ javadoc -author -private *.java
There is also a HelloSwing.bat file that shows how to run java programs
in a windows system by double clicking the *.bat file
There is a makefile for building the application (if you are interested).
*/
// You need to import awt components and events and swing components.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* HelloSwing Java example program for CS 585.
*
* - Frame window with a button.
*
- Usage: click button and label changes.
*
- Trace statements are added to illustrate order of method calls.
*
- Demonstrates javadoc comments and usage. See Sun's
*
* How to write doc comments for the Javadoc Tool
*
*
* @author Mike Barnes 9/4/02
*/
public class HelloSwing extends JFrame {
// Examples field documenation with javadoc
// The first line in the javadoc comment below is terminated with a
// period and will be the summary line in the field or method summary list.
// It and the following lines will be shown in the field or method's detail.
/**
* Also used by the inner JButtonListener class.
* @see JButtonListener
*/
private JButton aButton;
/**
* Used to determine the next button label string to use.
* If true label become "Press me again".
* If false label become "Hello CS 585 !".
* @see JButtonListener
*/
private boolean toggle = true;
/**
* The GUI's main method (thread).
*
* - calls the application's constructor
*
- sets the size of the app's window
*
- adds an anonymous inner class listener to handle window close events
*
- displays the app with show() or setVisible(...)
*
- suspends waiting for application to close (window close event).
* While suspended the event dispatcher thread handles user and system
* events.
*
* @param args[] the app's command line arguments (if any)
*/
public static void main(String args[]) {
System.out.println("HelloSwing :: enter main()");
HelloSwing app = new HelloSwing("Hello Java");
app.setSize(300,100);
app.setVisible(true);
// need to be able to close the window.
app.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e)
{System.exit(0);} });
System.out.println("HelloSwing :: leave main()");
}
/**
* Create the application's interface.
*
* - sets the app's window title
*
- creates and gets the frame's contentPane
*
- creates a button with a listener and adds it to the contentPane
*
* @param frameTitle will be the window's displayed title
*/
public HelloSwing(String frameTitle) {
super(frameTitle);
System.out.println("HelloSwing :: enter HelloSwing()");
Container contentPane = getContentPane();
aButton = new JButton("Hello CS 585 !");
// listen for JButton events
aButton.addActionListener(new JButtonListener());
contentPane.add(aButton, BorderLayout.CENTER);
System.out.println("HelloSwing :: leave HelloSwing()");
}
/**
* Event handler class for button press.
* This class must define an actionPerformed () method.
*/
class JButtonListener implements ActionListener {
/**
* A button callback.
* This class defines the actionPerformed method for an ActionEvent.
* When the button is pressed the button's label is toggled between
* 2 strings.
* The aButton and toggle variables of HelloSwing are accessed here.
*/
public void actionPerformed(ActionEvent event) {
System.out.println("HelloSwing :: actionPerformed()");
if (toggle)
aButton.setText("Press me again");
else
aButton.setText("Hello CS 585 !");
toggle = !toggle;
}
}
}