import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; public class Sample5 extends JFrame implements ActionListener{ // inherit JFrame // // some instance variables, holds text field objects for the form // JTextField name = new JTextField("",30); // define a text field control for name, 30 characters wide JTextField street = new JTextField("",30);// define a text field control for street, 30 characters wide JTextField city = new JTextField("",30); // define a text field control for city, 30 characters wide JTextField state = new JTextField("",2); // define a text field control for state, 2 characters wide JTextField zip = new JTextField("",5); // define a text field control for zip, 5 characters wide JTextField dlNumber = new JTextField("",12); // define a text field control for dlNumber, 12 characters wide /** * Constructor sets up the window */ public Sample5() { // // setup the frames banner/title bar // super("Sample 5"); // call JFrame's constructor and give it a title setSize(400,300); // set the width of the window to 400 pixels, height to 300. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); // // Create a Panel and a GridbagLayout // JPanel mainPanel = new JPanel(); // create a Panel for the Frames content area GridBagLayout gridbag = new GridBagLayout(); // create a GridBagLayout object to control the format of the Panel mainPanel.setLayout(gridbag); // tell the Panel about the layout manager setContentPane(mainPanel); // tell the Frame about its content pane // GridBagConstraints constraints = new GridBagConstraints(); // create a constraints object to use when arranging controls buildConstraints(constraints, 0,0,1,1,40,14); // setup the contraints for the first field constraints.fill = GridBagConstraints.BOTH; // set the fill to BOTH, there the size is expanded to fill cell constraints.anchor = GridBagConstraints.WEST; // set the anchor to WEST, the control is left aligned in the cell JLabel label1 = new JLabel("Name", Label.LEFT); // create a label field gridbag.setConstraints(label1,constraints); // set constraints for it mainPanel.add(label1); // add it to the panel buildConstraints(constraints, 1,0,2,1,60,14); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(name,constraints); mainPanel.add(name); // buildConstraints(constraints, 0,1,1,1,40,14); constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.WEST; JLabel label2 = new JLabel("Street", Label.LEFT); gridbag.setConstraints(label2,constraints); mainPanel.add(label2); buildConstraints(constraints, 1,1,2,1,60,14); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(street,constraints); mainPanel.add(street); // buildConstraints(constraints, 0,2,1,1,40,14); constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.WEST; JLabel label3 = new JLabel("City", Label.LEFT); gridbag.setConstraints(label3,constraints); mainPanel.add(label3); buildConstraints(constraints, 1,2,2,1,60,14); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(city,constraints); mainPanel.add(city); // buildConstraints(constraints, 0,3,1,1,40,14); constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.WEST; JLabel label4 = new JLabel("State",Label.LEFT); gridbag.setConstraints(label4,constraints); mainPanel.add(label4); buildConstraints(constraints, 1,3,2,1,60,14); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(state,constraints); mainPanel.add(state); // buildConstraints(constraints, 0,4,1,1,40,14); constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.WEST; JLabel label5 = new JLabel("Zip",Label.LEFT); gridbag.setConstraints(label5,constraints); mainPanel.add(label5); buildConstraints(constraints, 1,4,2,1,60,14); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(zip,constraints); mainPanel.add(zip); // buildConstraints(constraints, 0,5,1,1,40,14); constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.WEST; JLabel label6 = new JLabel("License",Label.LEFT); gridbag.setConstraints(label6,constraints); mainPanel.add(label6); buildConstraints(constraints, 1,5,2,1,60,14); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(dlNumber,constraints); mainPanel.add(dlNumber); // // ok, cancel buttons // JButton okButton = new JButton("Ok"); okButton.addActionListener(this); buildConstraints(constraints, 1,6,1,1,30,14); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; gridbag.setConstraints(okButton,constraints); mainPanel.add(okButton); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(this); buildConstraints(constraints, 2,6,1,1,30,14); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; gridbag.setConstraints(cancelButton,constraints); mainPanel.add(cancelButton); } /** * This routine sets up a constraint object. it should be called for each * field added to the gridbag * @param gbc constraint object to be setup. * @param gx x location (column) of the field on the grid (numbering from 0) * @param gy y location (row) of the field on the grid(numbering from 0) * @param gw width in cells of the field * @param gh height in cells of the field * @param wx proportion of the entire width for the field * @param wy proportion of the entire height for the field */ private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy){ gbc.gridx = gx; gbc.gridy = gy; gbc.gridwidth = gw; gbc.gridheight = gh; gbc.weightx = wx; gbc.weighty = wy; } /** * * this method responds to the OK and Cancel buttons, if it is the ok * button it will display the contents of the textfields on the screen. */ public void actionPerformed(ActionEvent event){ if(event.getSource() instanceof JButton) { // is this a button? JButton b1 = (JButton)event.getSource(); System.out.println(b1.getText()+" was pressed"); if(b1.getText().compareTo("Ok") == 0) { System.out.println(name.getText()); System.out.println(street.getText()); System.out.println(city.getText()+", "+state.getText()); System.out.println(zip.getText()); System.out.println(dlNumber.getText()); } } } /*********************************************************************** * * Main routine, Note, this routine can be in any class. */ public static void main(String[] arguments){ Sample5 fr = new Sample5(); // create the sample 5 frame fr.setVisible(true); // allow it to be seen } }