package rebelsky.swing; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; /** * A fairly simple sample Swing program. Based loosely * on the program found at * http://java.sun.com/docs/books/tutorial/uiswing/learn/example-1dot4/HelloWorldSwing.java * * @author Samuel A. Rebelsky * @version 1.0 of October 2004 */ public class HelloFrame extends JFrame { /** The message shown in the one label in this application. */ String message; private HelloFrame(String _message) { super(_message); this.message = _message; //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); // Exit nicely when closing this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Get the useful part of this framme. Container pane = this.getContentPane(); pane.setLayout(new GridLayout(0,1)); //Add the specified label final JLabel label = new JLabel(this.message); pane.add(label); // Add a button JButton click = new JButton("Click Me!"); click.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if (label.getText().equals("Click")) { label.setText(message); } else { label.setText("Click"); } } }); pane.add(click); //Display the window. this.pack(); this.setVisible(true); } // HelloFrame(String) public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { new HelloFrame("Hello"); new HelloFrame("Goodbye"); } }); } // main(String[]) } // class HelloFrame