// Based from Fig. 12.27: PanelDemo.java from Deitel Java HTP // // Using a JPanel to help lay out components. // Note: the event handlers in this program use 3 different // methods for adding the action listener. This is done for // demostration purposes. Normally a single method should be // chosen for a program and used throughout. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Mp2Gui extends JFrame implements ActionListener { private JButton northButtons[]; private JButton southButtons[]; private JLabel emplId; private JTextField emplName; private JTextField dept; private JTextField startDate; public Mp2Gui() { super( "CS 441 MP2 GUI" ); JMenuBar bar = new JMenuBar(); // create menubar setJMenuBar( bar ); // set the menubar for the JFrame // create File menu and Exit menu item JMenu fileMenu = new JMenu( "File" ); fileMenu.setMnemonic( 'F' ); JMenuItem exitItem = new JMenuItem( "eXit" ); exitItem.setMnemonic( 'X' ); exitItem.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { System.exit( 0 ); } } ); fileMenu.add( exitItem ); // create Help menu and About menu item JMenu helpMenu = new JMenu( "Help" ); helpMenu.setMnemonic( 'H' ); JMenuItem aboutItem = new JMenuItem( "About..." ); aboutItem.setMnemonic( 'A' ); aboutItem.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { JOptionPane.showMessageDialog( Mp2Gui.this, "This is an example\nof using menus", "About", JOptionPane.PLAIN_MESSAGE ); } } ); helpMenu.add( aboutItem ); bar.add( fileMenu ); // add File menu bar.add( helpMenu ); // add Help Menu // set up the main panel JPanel northButtonPanel; String northNames[] = {"Prev", "Next"}; JPanel centerPanel; JLabel centerLabels []; String centerNames[] = {"Employee ID:", "Employee Name:", "Department:", "Start Date:"}; JPanel southButtonPanel; String southNames[] = {"Add", "Modify", "Delete", "Quit"}; Container c = getContentPane(); // set up button in the north panel northButtonPanel = new JPanel(); northButtons = new JButton[ northNames.length ]; northButtonPanel.setLayout( new GridLayout( 1, northButtons.length ) ); ButtonHandler handler = new ButtonHandler(); for ( int i = 0; i < northButtons.length; i++ ) { northButtons[ i ] = new JButton( northNames[i] ); northButtons[i].addActionListener (handler); northButtonPanel.add( northButtons[ i ] ); } c.add( northButtonPanel, BorderLayout.NORTH ); // set up the center Panel centerPanel = new JPanel(); centerPanel.setLayout ( new GridLayout( centerNames.length, 2) ); centerLabels = new JLabel [ centerNames.length ]; for (int i = 0; i < centerLabels.length; i++ ) { centerLabels[i] = new JLabel (centerNames[i]); } centerPanel.add (centerLabels[0]); emplId = new JLabel (); centerPanel.add (emplId); centerPanel.add (centerLabels[1]); emplName = new JTextField (); centerPanel.add (emplName); centerPanel.add (centerLabels[2]); dept = new JTextField (); centerPanel.add (dept); centerPanel.add (centerLabels[3]); startDate = new JTextField (); centerPanel.add (startDate); c.add( centerPanel, BorderLayout.CENTER ); // set up the button in the south panel southButtonPanel = new JPanel(); southButtons = new JButton[ southNames.length ]; southButtonPanel.setLayout( new GridLayout( 1, southButtons.length ) ); for ( int i = 0; i < southButtons.length; i++ ) { southButtons[ i ] = new JButton( southNames[i] ); southButtons[i].addActionListener (this); southButtonPanel.add( southButtons[ i ] ); } c.add( southButtonPanel, BorderLayout.SOUTH ); setSize( 600, 200 ); show(); } // event handler for the South buttons public void actionPerformed( ActionEvent e ) { JOptionPane.showMessageDialog (null, "You pressed button: " + e.getActionCommand() + "\n\nEmployee Number: " + emplId.getText () + "\nEmployee Name: " + emplName.getText() + "\nDepartment: " + dept.getText() + "\nStart Date: " + startDate.getText() ); String buttonText = e.getActionCommand(); if (buttonText.equalsIgnoreCase("Quit")) { System.exit(0); } } public static void main( String args[] ) { Mp2Gui app = new Mp2Gui(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } // inner class for buttoning handling events private class ButtonHandler implements ActionListener { // event handler for the NORTH buttons public void actionPerformed (ActionEvent e) { String id = emplId.getText(); int num = 0; if (!id.equals ("")) num = Integer.parseInt(id); // decrement or increment value based on which button was // pressed String buttonText = e.getActionCommand(); if (buttonText.equalsIgnoreCase("Prev")) num--; else num++; emplId.setText("" + num); } } }