TimeTestWindow2.java
Created with JBuilder |
// Fig. 10.23: TimeTestWindow2.java
// Demonstrating the Time class set and get methods
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TimeTestWindow2 extends JFrame {
private Time time;
private JLabel hourLabel, minuteLabel, secondLabel;
private JTextField hourField, minuteField, secondField, displayField;
// constructor
public TimeTestWindow2()
{
// call JFrame constructor to set title bar string
super( "Anonymous Inner Class Demonstration" );
time = new Time(); // create Time object
createGUI(); // set up GUI
registerEventHandlers(); // set up event handling
}
// create GUI components and attach to content pane
private void createGUI()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() );
hourLabel = new JLabel( "Set Hour" );
hourField = new JTextField( 10 );
container.add( hourLabel );
container.add( hourField );
minuteLabel = new JLabel( "Set minute" );
minuteField = new JTextField( 10 );
container.add( minuteLabel );
container.add( minuteField );
secondLabel = new JLabel( "Set Second" );
secondField = new JTextField( 10 );
container.add( secondLabel );
container.add( secondField );
displayField = new JTextField( 30 );
displayField.setEditable( false );
container.add( displayField );
} // end method createGUI
// register event handlers for hourField, minuteField and secondField
private void registerEventHandlers()
{
// register hourField event handler
hourField.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
time.setHour( Integer.parseInt(
event.getActionCommand() ) );
hourField.setText( "" );
displayTime();
}
} // end anonymous inner class
); // end call to addActionListener for hourField
// register minuteField event handler
minuteField.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
time.setMinute( Integer.parseInt(
event.getActionCommand() ) );
minuteField.setText( "" );
displayTime();
}
} // end anonymous inner class
); // end call to addActionListener for minuteField
secondField.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
time.setSecond( Integer.parseInt(
event.getActionCommand() ) );
secondField.setText( "" );
displayTime();
}
} // end anonymous inner class
); // end call to addActionListener for secondField
} // end method registerEventHandlers
// display time in displayField
public void displayTime()
{
displayField.setText( "The time is: " + time );
}
// create TimeTestWindow2 object, register for its window events
// and display it to begin application's execution
public static void main( String args[] )
{
TimeTestWindow2 window = new TimeTestWindow2();
// register listener for windowClosing event
window.addWindowListener(
// anonymous inner class for windowClosing event
new WindowAdapter() {
// terminate application when user closes window
public void windowClosing( WindowEvent event )
{
System.exit( 0 );
}
} // end anonymous inner class
); // end call to addWindowListener for window
window.setSize( 400, 105 );
window.setVisible( true );
} // end main
} // end class TimeTestWindow2
TimeTestWindow2.java
Created with JBuilder |