package simpleGUI; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** This application demonstrates some of the basics of * setting up a GUI. We demonstrate how to set up simple * interface elements, use a simple layout strategy, and * implement action listeners for buttons. * * You can read more about these topics in the text in * sections 13.4, 13.5, 13.8, 15.3, and 16.6 */ public class MainFrame extends JFrame{ // Declare all of the objects that will make up the GUI private JPanel mainPanel,topPanel,bottomPanel; private JTextField textField; private JButton oneButton,twoButton,threeButton; public MainFrame() { // Create the GUI objects mainPanel = new JPanel(); topPanel = new JPanel(); bottomPanel = new JPanel(); textField = new JTextField(); oneButton = new JButton(); twoButton = new JButton(); threeButton = new JButton(); // Set up the containment heirarchy for the objects topPanel.add(textField); bottomPanel.add(oneButton); bottomPanel.add(twoButton); bottomPanel.add(threeButton); mainPanel.add(topPanel); mainPanel.add(bottomPanel); // We use a GridLayout layout manager with 2 rows and one column // to arrange things nicely. mainPanel.setLayout(new GridLayout(2,1)); this.getContentPane().add(mainPanel); // Set the text properties of the text field and the buttons textField.setText("Type some text here"); oneButton.setText("One"); twoButton.setText("Two"); threeButton.setText("Three"); // Set up action listeners for the buttons. // The code below demonstrates three different methods for // doing this. // The first method is to use an external class as the action // listener. That external class will need a reference to this // window object so it can call our methods in response to the // button click. oneButton.addActionListener(new ButtonOneListener(this)); // The second method is to use an inner class as the action // listener. Inner classes can call our methods without needing // an explicit object reference. twoButton.addActionListener(new ButtonTwoListener()); // The third method is to use an anonymous inner class. This is // a class created 'on the fly' as we need it. threeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { setMessage("Three Pressed"); } }); // Do some final set-up for the window this.setTitle("GUI Example"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.pack(); } public void setMessage(String newMessage) { textField.setText(newMessage); } public static void main(String[] args) { // The typical main method in a GUI application // simply creates a window object and makes it visible. MainFrame window = new MainFrame(); window.setVisible(true); } class ButtonTwoListener implements ActionListener { public void actionPerformed(ActionEvent evt){ setMessage("Two Pressed"); } } } class ButtonOneListener implements ActionListener { private MainFrame window; ButtonOneListener(MainFrame window) { this.window = window; } public void actionPerformed(ActionEvent evt){ window.setMessage("One Pressed"); } }