Source of HelloGUIApp4.java


  1: //HelloGUIApp4.java
  2: //An application to display "Hello, world!" in a window, 
  3: //and this time it's a window that will close, either by
  4: //clicking on the window's close box or by clicking on
  5: //the Quit button of the application. In addition, the
  6: //button is in color, and the window size should be 
  7: //changed by the user after it opens to see how the
  8: //"flow layout" adjusts the positions of the button
  9: //and label. Compare this with the fixed position of
 10: //the other text.

 12: import java.awt.*;
 13: import java.awt.event.*;

 15: public class HelloGUIApp4
 16: extends Frame
 17: implements ActionListener, WindowListener
 18: {
 19:     private Button button;
 20:     private Label label;

 22:     public static void main(String[] args)
 23:     {
 24:         HelloGUIApp4 application = new HelloGUIApp4();
 25:         application.setSize(100, 100);
 26:         application.setVisible(true);
 27:     }


 30:     public HelloGUIApp4()
 31:     {
 32:         setTitle("Hello with Quit Button");

 34:         setLayout(new FlowLayout());

 36:         button = new Button("Quit");
 37:         button.setBackground(Color.yellow);
 38:         add(button);
 39:         button.addActionListener(this);

 41:         label = new Label("Hello, world!");
 42:         add(label);

 44:         addWindowListener(this);
 45:     }


 48:     public void actionPerformed(ActionEvent event)
 49:     {
 50:             System.exit(0);
 51:     }

 53: 
 54:     public void windowClosed(WindowEvent event) {}
 55:     public void windowDeiconified(WindowEvent event) {}
 56:     public void windowIconified(WindowEvent event) {}
 57:     public void windowActivated(WindowEvent event) {}
 58:     public void windowDeactivated(WindowEvent event) {}
 59:     public void windowOpened(WindowEvent event) {}
 60:     public void windowClosing(WindowEvent event)
 61:     {
 62:             System.exit(0);
 63:     }


 66:     //Try commenting out this method.
 67:     public void paint(Graphics g)
 68:     {
 69:         g.drawString("Hello, world!", 20, 90);
 70:     }
 71: }