// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 10 - Graphical User Interfaces // Program to demonstrate GUI w/ buttons // CustomerGUIFour.java 1 JAN 05 import javax.swing.*; // import Swing package import java.awt.*; // import AWT package import java.awt.event.*; // import AWT Event package public class CustomerGUIFour extends JFrame // this class inherits methods from the JFrame class { // static variables to hold frame dimensions in pixels private static final int WIDTH = 350; private static final int HEIGHT = 175; // declare instance variables for GUI components private JLabel nameLabel, ssnLabel, addressLabel, phoneLabel; private JTextField nameTextField, ssnTextField, addressTextField, phoneTextField; private JButton acceptB, clearB; // declare instance variables for button handlers private AcceptButtonHandler abHandler; private ClearButtonHandler cbHandler; public CustomerGUIFour() // constructor method defines frame { setTitle("New Customer"); // set the title of the frame // instantiate JLabel objects nameLabel = new JLabel("Name:"); ssnLabel = new JLabel("SS No.:"); addressLabel = new JLabel("Address:"); phoneLabel = new JLabel("Phone No.:"); // instantiate JTextField objects nameTextField = new JTextField("Lastname, Firstname", 30); ssnTextField = new JTextField("xxx-xx-xxxx", 11); addressTextField = new JTextField("Street address, City, State, Zip", 40); phoneTextField = new JTextField("xxx-xxx-xxxx", 12); // instantiate Accept button acceptB = new JButton("Accept"); abHandler = new AcceptButtonHandler(); acceptB.addActionListener(abHandler); // instantiate Clear button clearB = new JButton("Clear"); cbHandler = new ClearButtonHandler(); clearB.addActionListener(cbHandler); Container pane = getContentPane(); // get a content pane for the frame GridLayout aGrid = new GridLayout(5,2); // create a five row, two column layout pane.setLayout(aGrid); // set the layout for the pane // add JLabel, JTextField, and JButton objects to the content pane pane.add(nameLabel); pane.add(nameTextField); pane.add(ssnLabel); pane.add(ssnTextField); pane.add(addressLabel); pane.add(addressTextField); pane.add(phoneLabel); pane.add(phoneTextField); pane.add(acceptB); pane.add(clearB); setSize(WIDTH, HEIGHT); // set the frame size centerFrameOnScreen(WIDTH, HEIGHT); // call method to center frame on screen } // end constructor method public void centerFrameOnScreen(int frameWidth, int frameHeight) // method declaration { // use the Toolkit and Dimension classes of the java.awt package // create a Toolkit object Toolkit aToolkit = Toolkit.getDefaultToolkit(); // create a Dimension object with user screen information Dimension screen = aToolkit.getScreenSize(); // assign x, y position of upper-left corner of frame int xPostionOfFrame = (screen.width - frameWidth)/2; int yPositionOfFrame = (screen.height - frameHeight)/2; // method to center frame on user's screen setBounds(xPostionOfFrame, yPositionOfFrame, frameWidth, frameHeight); } // end centerFrameOnScreen() method private class AcceptButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { String aName = nameTextField.getText(); String aSSN = ssnTextField.getText(); String anAddress = addressTextField.getText(); String aPhone = phoneTextField.getText(); Customer aCustomer = new Customer(aName, aSSN, anAddress, aPhone); System.out.println("Name: " + aCustomer.getName()); System.out.println("SSN: " + aCustomer.getSSN()); System.out.println("Address: " + aCustomer.getAddress()); System.out.println("Phone: " + aCustomer.getPhone()); } } private class ClearButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { nameTextField.setText(""); ssnTextField.setText(""); addressTextField.setText(""); phoneTextField.setText(""); } } public static void main(String [] args) // declare main() method { JFrame aCustomerGUIFour = new CustomerGUIFour(); // create the frame object aCustomerGUIFour.show(); // display the frame } // end main } // end class