// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 10 - Graphical User Interfaces // Program to demonstrate GUI w/ labels // CustomerGUITwo.java 1 JAN 05 import javax.swing.*; // import Swing package import java.awt.*; // import AWT package public class CustomerGUITwo extends JFrame // this class inherits methods from the JFrame class { // static variables to hold frame dimensions in pixels private static final int WIDTH = 275; private static final int HEIGHT = 170; private JLabel nameLabel, ssnLabel, addressLabel, phoneLabel; public CustomerGUITwo() // 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.:"); Container pane = getContentPane(); // get a content pane for the frame GridLayout aGrid = new GridLayout(4, 1); // create a four row, one column layout pane.setLayout(aGrid); // set the layout for the pane // add label objects to the pane pane.add(nameLabel); pane.add(ssnLabel); pane.add(addressLabel); pane.add(phoneLabel); 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 aCustomerGUITwo = new CustomerGUITwo(); // create the frame object aCustomerGUITwo.show(); // display the frame } // end main } // end class