/* * Copyright (c) 1999-2002, Xiaoping Jia. * All Rights Reserved. * * Modified to be an application rather than an applet. * Barbara Lerner Sept. 4, 2006 */ import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /** * A simple Java application. It displays a text message and an image of the planet Venus. * There are two buttons that allow the user to select one of two messages to display. * * This class "implements ActionListener" so that it can handle the button clicks. */ public class HelloFromVenus implements ActionListener { // The panel containing the image and the text private VenusPanel venusPanel; // The button used to display the hi message. private JButton hiButton; // The button used to display the bye message. private JButton byeButton; // Creates the user interface public HelloFromVenus() { // Create the window and set its size. JFrame f = new JFrame(); f.setSize(new Dimension(300, 350)); // Create the panel that will display the image and text venusPanel = new VenusPanel(); // Add the venus panel to the center of the window Container contentPane = f.getContentPane(); contentPane.add(venusPanel, BorderLayout.CENTER); // Create a panel to hold the buttons JPanel buttonPanel = new JPanel(); // Indicate that the buttons will be in a row. buttonPanel.setLayout (new BoxLayout (buttonPanel, BoxLayout.X_AXIS)); // Create the hi button hiButton = new JButton ("Hi"); // Indicate that this object will manage the clicks on the hi button. hiButton.addActionListener(this); // Add the button to the button panel. buttonPanel.add(hiButton); // Repeat the previous 3 steps to create the bye button. byeButton = new JButton ("Bye"); byeButton.addActionListener (this); buttonPanel.add(byeButton); // Add the buttons as a group to the bottom of the window. contentPane.add(buttonPanel, BorderLayout.SOUTH); // Display the window. f.setVisible(true); } /** * @param e information about the button that was clicked. */ public void actionPerformed(ActionEvent e) { // If the hi button was clicked, tell the venus panel to display the hi message. if (e.getSource() == hiButton) { venusPanel.sayHi(); } else { // Otherwise tell the venus panel to display the bye message. venusPanel.sayBye(); } } // Start the program simply by creating the user interface. It will just wait for user // interaction or for the user to quit the program. public static void main(String[] args) { new HelloFromVenus(); } }