// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 10 - Graphical User Interfaces // Program to demonstrate GUI w/ GridLayout // CustomerGUIThree.java 1 JAN 05 import javax.swing.*; // import Swing package import java.awt.*; // import AWT package public class CustomerGUIThree 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; private JLabel nameLabel, ssnLabel, addressLabel, phoneLabel; private JTextField nameTextField, ssnTextField, addressTextField, phoneTextField; public CustomerGUIThree() // 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); Container pane = getContentPane(); // get a content pane for the frame GridLayout aGrid = new GridLayout(4, 2); // create a four row, two column layout pane.setLayout(aGrid); // set the layout for the pane // add JLabel and JTextField 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); 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 public static void main(String [] args) // declare main() method { JFrame aCustomerGUIThree = new CustomerGUIThree(); // create the frame object aCustomerGUIThree.show(); // display the frame } // end main } // end class