/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package inclass.ch11; import javax.swing.*; import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionEvent; import java.awt.FlowLayout; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.*; import java.awt.Color; /** * * @author jfleming */ public class MyGUI_1 extends JFrame implements ActionListener, ItemListener, ListSelectionListener{ private JTextField nameField; private JCheckBox student; private JCheckBox instructor; private JRadioButton freshman; private JRadioButton sophmore; private JRadioButton junior; private JRadioButton senior; private JComboBox combo; private JList list; private String[] nameList = {"Jim", "George", "John", "David", "William"}; private String[] colorList = {"Red", "Green", "Blue", "Orange", "Magenta"}; public MyGUI_1() { setLayout( new BorderLayout() ); JPanel northPanel = new JPanel(); JPanel southPanel = new JPanel(); JPanel eastPanel = new JPanel(); JPanel westPanel = new JPanel(); JPanel centerPanel = new JPanel(); JPanel topWestPanel = new JPanel(); JPanel bottomWestPanel = new JPanel(); JPanel leftCenterPanel = new JPanel(); JPanel rightCenterPanel = new JPanel(); JPanel topEastPanel = new JPanel(); JPanel bottomEastPanel = new JPanel(); westPanel.setLayout( new GridLayout( 2, 1 ) ); centerPanel.setLayout( new GridLayout( 1, 2 ) ); eastPanel.setLayout( new GridLayout( 2, 1 ) ); // add the top and bottom panels to the west panel westPanel.add(topWestPanel); westPanel.add(bottomWestPanel); // add the left and right panels to the center panel centerPanel.add(leftCenterPanel); centerPanel.add(rightCenterPanel); // add the top and bottom panels to the east panel eastPanel.add(topEastPanel); eastPanel.add(bottomEastPanel); // add all the panels to the frame add(northPanel, BorderLayout.NORTH); add(southPanel, BorderLayout.SOUTH); add(eastPanel, BorderLayout.EAST); add(westPanel, BorderLayout.WEST); add(centerPanel, BorderLayout.CENTER); JLabel nameLabel = new JLabel( "Name" ); nameField = new JTextField(20); nameField.addActionListener(this); // add components to the north panel northPanel.add(nameLabel); northPanel.add(nameField); JButton submitButton = new JButton("Submit"); JButton cancelButton = new JButton("Cancel"); submitButton.addActionListener(this); cancelButton.addActionListener(this); submitButton.setBackground(Color.GREEN); cancelButton.setBackground(Color.ORANGE); // add components to the west panel topWestPanel.add(submitButton); bottomWestPanel.add(cancelButton); student = new JCheckBox("Student"); instructor = new JCheckBox("Instructor"); student.addItemListener(this); instructor.addItemListener(this); // add components to the west panel topEastPanel.add(student); bottomEastPanel.add(instructor); ButtonGroup classGroup = new ButtonGroup(); freshman = new JRadioButton("Freshman"); sophmore = new JRadioButton("Sophmore"); junior = new JRadioButton("Junior"); senior = new JRadioButton("Senior"); classGroup.add(freshman); classGroup.add(sophmore); classGroup.add(junior); classGroup.add(senior); freshman.addItemListener(this); sophmore.addItemListener(this); junior.addItemListener(this); senior.addItemListener(this); // add components to the west panel southPanel.add(freshman); southPanel.add(sophmore); southPanel.add(junior); southPanel.add(senior); combo = new JComboBox(nameList); combo.addItemListener(this); // add components to the left center panel leftCenterPanel.add(combo); list = new JList(colorList); list.setVisibleRowCount(4); list.addListSelectionListener(this); // add components to the right center panel rightCenterPanel.add(list); } @Override public void actionPerformed( ActionEvent ae ) { System.out.printf( "%s\n", ae.getActionCommand() ); } @Override public void itemStateChanged( ItemEvent ie ) { if (ie.getSource().equals(student) || ie.getSource().equals(instructor)) { if (student.isSelected()) System.out.printf( "%s\n", "Student is selected" ); else System.out.printf( "%s\n", "Student is NOT selected" ); if (instructor.isSelected()) System.out.printf( "%s\n", "Instructor is selected" ); else System.out.printf( "%s\n", "Instructor is NOT selected" ); } if (ie.getSource().equals(freshman) || ie.getSource().equals(sophmore) || ie.getSource().equals(junior) || ie.getSource().equals(senior)) { System.out.printf("%s", freshman.isSelected()? "Freshman\n":""); System.out.printf("%s", sophmore.isSelected()? "Sophmore\n":""); System.out.printf("%s", junior.isSelected()? "Junior\n":""); System.out.printf("%s", senior.isSelected()? "Senior\n":""); } if (ie.getSource().equals(combo) && ie.getStateChange()==ItemEvent.SELECTED) System.out.printf("%s\n", combo.getSelectedItem()); } public void valueChanged( ListSelectionEvent lse ) { if (lse.getSource().equals(list)) System.out.printf("%s\n", list.getSelectedValue()); } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package inclass.ch11; import javax.swing.JFrame; /** * * @author jfleming */ public class MyGUITest_1 { /** * @param args the command line arguments */ public static void main(String[] args) { MyGUI_1 mg = new MyGUI_1(); mg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mg.setSize(400, 200); mg.setVisible(true); } }