// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 10 - Graphical User Interfaces // Program to demonstrate GUI w/ menu // CustomerGUISix.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 CustomerGUISix 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 CustomerGUISix() // constructor method defines frame { setTitle("New Customer"); // set the title of the frame // instantiate JLabel objects nameLabel = new JLabel("Name: ", SwingConstants.RIGHT); ssnLabel = new JLabel("SS No.: ", SwingConstants.RIGHT); addressLabel = new JLabel("Address: ", SwingConstants.RIGHT); phoneLabel = new JLabel("Phone No.: ", SwingConstants.RIGHT); // 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); // create two JMenu objects with mnemonics JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); JMenu helpMenu = new JMenu("Help"); helpMenu.setMnemonic('H'); // create and add two JMenuItem objects with mnemonics and action listeners JMenuItem acceptItem = new JMenuItem("Accept"); acceptItem.setMnemonic('A'); acceptItem.addActionListener(abHandler); fileMenu.add(acceptItem); JMenuItem clearItem = new JMenuItem("Clear"); clearItem.setMnemonic('C'); clearItem.addActionListener(cbHandler); fileMenu.add(clearItem); // create a JMenuBar object, set it, add menus JMenuBar bar = new JMenuBar(); setJMenuBar(bar); bar.add(fileMenu); bar.add(helpMenu); Container pane = getContentPane(); // get a content pane for the frame // define a grid and a flow GridLayout aGrid = new GridLayout(4, 1); FlowLayout flowRight = new FlowLayout(FlowLayout.RIGHT); // create three panels JPanel labelPanel = new JPanel(); JPanel textFieldPanel = new JPanel(); JPanel buttonPanel = new JPanel(); // set layouts for panels labelPanel.setLayout(aGrid); textFieldPanel.setLayout(aGrid); buttonPanel.setLayout(flowRight); // add labels to panel labelPanel.add(nameLabel); labelPanel.add(ssnLabel); labelPanel.add(addressLabel); labelPanel.add(phoneLabel); // add text fields to panel textFieldPanel.add(nameTextField); textFieldPanel.add(ssnTextField); textFieldPanel.add(addressTextField); textFieldPanel.add(phoneTextField); // add buttons to panel buttonPanel.add(acceptB); buttonPanel.add(clearB); // add panels to content pane using BorderLayout pane.add(labelPanel, BorderLayout.WEST); pane.add(textFieldPanel, BorderLayout.CENTER); pane.add(buttonPanel, BorderLayout.SOUTH); 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 aCustomerGUISix = new CustomerGUISix(); // create the frame object aCustomerGUISix.show(); // display the frame } // end main } // end class