package rebelsky.swing; import java.awt.GridLayout; import java.awt.Dimension; 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.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 NewHello { String message; private NewHello(String _message) { this.message = _message; } // NewHello(String) /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame(this.message); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add the specified label final JLabel label = new JLabel(this.message); frame.getContentPane().setLayout(new GridLayout(0,1)); frame.getContentPane().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"); } } }); frame.getContentPane().add(click); //Display the window. frame.pack(); frame.setVisible(true); } // createAndShowGUI() 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 NewHello("Hello")).createAndShowGUI(); (new NewHello("Goodbye")).createAndShowGUI(); } }); } // main(String[]) } // class NewHello