| Home | Family | Softwares | Resume | Activities |
/*File Gui01.java
The purpose of this program is to illustrate some of the
code in a relatively simple Graphical User Interface.
Note that this program uses Swing components. Therefore,
the Swing class library must be installed and accessible
to the compiler.
This program begins with a heavyweight Frame object on the
screen. The Frame contains five components:
A heavyweight Button with the caption "Toggle Color"
A lightweight JButton with the caption "Say Hello"
A lightweight JButton with the caption "Say Goodbye"
A green heavyweight Label with an initial caption of
"Color Me". This is the output component for the
program.
A red heavyweight label with the caption "Toggle Color".
This Label functions as a counterfeit Button object.
The lightweight components exhibit the JavaSoft metal
look and feel. The heavyweight components exhibit the
look and feel of the underlying operating system.
If you click the heavyweight Button object, the background
color of the display toggles between yellow and green.
If you click the heavyweight Label object with the "Toggle
Color" caption, a mouse listener on this component creates
a counterfeit ActionEvent object and posts it to the
system event queue, attributed to the heavyweight Button
object. When it emerges from the queue, the system thinks
that someone clicked the actual Button object and toggles
the background color of the display just as though the
Button object had been clicked.
If you click either of the two lightweight buttons, the
caption in the display changes to either "Hello" or
"Goodbye" depending on which lightweight button was
clicked.
Note that the controlling class implements
the ActionListener interface and therefore, an object of
this class can serve as an action listener registered
on its own components. This leads to a compact, but
somewhat cryptic programming style.
The program was tested using JDK 1.1.6 and Swing 1.0.1
under Win95.
********************************************************
//Import required packages*/
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;//JDK 1.1 version
//import javax.swing.*;//JDK 1.2 version
//=======================================================
public class Gui01 extends Frame implements ActionListener{
//Instance variables required to respond to the
// action event on the Button object.
boolean toggle = false;
//Instance variable used as the output display.
Label display;
//All applications have a main method
public static void main(String[] args){
//Instantiate and object of this type
Gui01 displayWindow = new Gui01();
}//end main
//-----------------------------------------------------
public Gui01()
{
//constructor
//Invoke property methods to set the title and the
// layout for the outer object of type Frame.
setTitle("Display Panel");
setLayout(new FlowLayout());
//set layout for container
//Instantiate a new object of type Button
Button toggleColorButton = new Button("Toggle Color");
//Instantiate the display as an object of type Label
// and initialize its color to green using a static
// constant of the Color class.
display = new Label("Color Me");
display.setBackground(Color.green);
//Instantiate a Label object that will be used as a
// fake button to generate counterfeit action events
// and attribute them to the Button object. Color
// it red.
Label toggleColorLabel = new Label("Toggle Color");
toggleColorLabel.setBackground(Color.red);
//If you import Swing class then Instantiate two lightweight
//buttons of the Swing lass otherwise use "Button" fro AWT
// class JButton that will be used to cause the words
// Hello and Goodbye to be displayed in the display.
// Note the different appearance of these buttons as
// compared to the Button object instantiated above.
// If you import Swing class then
// JButton sayHelloButton = new JButton("Say Hello");
// JButton sayGoodbyeButton = new JButton("Say Goodbye");
//ELSE
Button sayHelloButton = new Button("Say Hello");
Button sayGoodbyeButton = new Button("Say Goodbye");
//Add all of the components to the Frame object
this.add(toggleColorButton);
this.add(display);
this.add(toggleColorLabel);
this.add(sayHelloButton);
this.add(sayGoodbyeButton);
setSize(300,150);//Set frame size
setVisible(true);//Display the frame
//Register listener objects.
//First register a mouse listener on the Label object.
// The behavior of this event handler when a mouse
// event occurs on the label will be to generate a
// counterfeit action event and attribute it to the
// Button object.
toggleColorLabel.addMouseListener(
new MyMouseListener(toggleColorButton));
//Register this object as a listener object on the
// Button object as well as on the two JButton
// lightweight button objects.
toggleColorButton.addActionListener(this);
sayHelloButton.addActionListener(this);
sayGoodbyeButton.addActionListener(this);
//The statement which follows instantiates anonymous
// inner class object of type WindowAdapter, and
// registers it for handling a windowClosing event on
// Frame object. This code uses the abbreviated syntax
// which defines the listener class anonymously (the
// listener class does not have a class name and the
// object instantiated from the class does not have
// a name).The behavior of this listener object causes
// the program to terminate when the window is closed.
this.addWindowListener(
new WindowAdapter(){//anonymous class definition
public void windowClosing(WindowEvent e){
System.exit(0);//terminate the program
}//end windowClosing()
}//end WindowAdapter
);//end addWindowListener
}//end constructor
//-----------------------------------------------------//
//This is the actionPerformed method for this object.
// This method is invoked whenever one of the buttons
// is clicked, or a counterfeit action event is posted
// with the Button object as the specified source object.
// The code in this method causes the color of the
// display to toggle between yellow and green or causes
// it to display Hello or Goodbye, depending upon which
// component is clicked.
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Toggle Color")){
if(!toggle){
toggle = true;
display.setBackground(Color.yellow);
}else{
toggle = false;
display.setBackground(Color.green);
}//end else
}else
if(e.getActionCommand().equals("Say Hello"))
display.setText("Hello");
else display.setText("Goodbye");
}//end actionPerformed()
//-----------------------------------------------------//
}//end class Gui01
//=========================================================
/*This top level class is used to monitor for mouse
clicks on a Label object. Whenever the user clicks on the
label, the code in an object of this class creates a
counterfeit ActionEvent object and posts it to the
SystemEventQueue. The source of the event is specified to
be a particular Button object that is passed in when an
object of this class is instantiated. Thus, the Label
object "claims" to be the Button object and posts
ActionEvent objects that are interpreted by the runtime
system as originating at the Button object. The type of
ActionEvents generated are ACTION_PERFORMED events. The
events are automatically delivered to the actionPerformed()
method of an ActionListener object registered on the
Button. */
class MyMouseListener extends MouseAdapter{
Button toggleColorButton;//reference to the Button
//-----------------------------------------------------//
MyMouseListener(Button toggleColorButton){//constructor
//Save reference to Button
this.toggleColorButton =
toggleColorButton;
}//end constructor
//-----------------------------------------------------//
//This is an overridden mouseClicked() method that is
// declared in the MouseListener interface and defined
// as an empty method in the MouseAdapter class.
public void mouseClicked(MouseEvent e){
//Note that the following is a single statement
Toolkit.getDefaultToolkit().
getSystemEventQueue().
postEvent(new ActionEvent(toggleColorButton,
ActionEvent.
ACTION_PERFORMED,
"Toggle Color"));
}//end overridden mouseClicked() method
}//end MyMouseListener
//=======================================================//