280 likes | 424 Views
Class 20. Class 20 Objectives. Use JCheckbox, JRadioButton, and a JComboBox in a UID. JCheckbox A boolean GUI component that is either selected or not selected JRadioButton A group of usually circular gui components that allow for only one in the group to be selected JComboBox
E N D
Class 20 Objectives • Use JCheckbox, JRadioButton, and a JComboBox in a UID
JCheckbox • A boolean GUI component that is either selected or not selected • JRadioButton • A group of usually circular gui components that allow for only one in the group to be selected • JComboBox • A drop-down list from which the user can make a selection (in AWT called Choice list)
Class JCheckBox is a JComponent creates a single box that can be selected or not selected
Write a windowed application that will display the following gui. After the user makes the selections, you program will tell the user how many classes they chose.
// Creating Checkbox buttons. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CheckBoxFrame extends JFrame { private String classes[] = {“Swim”, “Dance”,”Tennis”}; private JTextField nameField; private JCheckBox choices[]; private JLabel labelField; private JLabel label1; private JButton doneButton;
public CheckBoxFrame() { super( "JCheckBox Test" ); Container theWindow = getContentPane(); theWindow.setLayout(new FlowLayout()); doneButton = new JButton(“Done”); nameField = new JTextField( 20); label1 = new JLabel(“enter your name”); labelField = new JLabel(“Select the classes you“ + “want to take then click the \”Done\” button ”); nameField.setFont( new Font( "TimesRoman", Font.PLAIN, 14 ) ); theWindow.add(label1); theWindow.add( nameField); theWindow.add(labelField);
choices = new JCheckBox[classes.length]; for (int i = 0; i < classes.length; i++) { choices[i] = new JCheckBox(classes[i]); theWindow.add(choices[i]); } theWindow.add(doneButton); doneButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { buttonClicked(ev) }; } ); setSize(350,200); setVisible(true); } // end of constructor
public void buttonClicked( ActionEvent event ) { int classCount =0; for (int i=0; i< classes.length; i++) if ( choices[i].isSelected() ==true) ++classCount; JOptionPane.showMessageDialog( null, nameField.getText() + “ \n You have” + “ decided to take “ + classCount + “ class(es)”); }
public static void main(String a[]) { CheckBoxFrame cf = new CheckBoxFrame(); cf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } } // end of class
Add JRadioButtons to the previous application to allow user to choose MWF or Tues./Thurs classes.
// Creating Radio buttons. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RadioButtonTest extends JFrame { private String classes[] = {“Swim”, “Dance”,”Tennis”}; private JTextField nameField; private JCheckBox choices[]; private JLabel labelField1,labelField2; private JLabel label1; private JButton doneButton;
// new gui components private JRadioButton dayChoice1; private JRadioButton dayChoice2; private ButtonGroupradioGroup;
public RadioButtonTest() { super( “RadioButton Test" ); Container theWindow = getContentPane(); windowFrame.setLayout(new FlowLayout()); doneButton = new JButton(“Done”); nameField = new JTextField( 20); label1 = new JLabel(“Enter your name”); labelField1 = new JLabel(“Select the classes you “ + “want to take ”); labelField2 = new JLabel(“and the days, then click” + “the \”Done\” button”); nameField.setFont( new Font( "TimesRoman", Font.PLAIN, 14 ) ); theWindow.add(label1); theWindow.add( nameField); theWindow.add(labelField1); theWindow.add(labelField2);
// create logical relationship between JRadioButtons radioGroup = new ButtonGroup(); radioGroup.add( dayChoice1); radioGroup.add( dayChoice2 );
choices = new JCheckBox[classes.length]; for (inti = 0; i < classes.length; i++) { choices[i] = new JCheckBox(classes[i]); theWindow.add(choices[i]); } dayChoice1 = new JRadioButton(“MWF”,true); dayChoice2= new JRadioButton(“TTH”,false); theWindow.add(dayChoice1); theWindow.add(dayChoice2); theWindow.add(doneButton); doneButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEventev) { buttonClicked(ev) }; } ); setSize(350,200); setVisible(true); } // end of constructor
public void buttonClicked( ActionEvent event ) { int classCount =0; String daySelection; if (dayChoice1.isSelected() == true) daySelection = “MWF”; else daySelection = “TTH”; for (int i=0; i< classes.length; i++) if ( choices[i].isSelected() ==true) ++classCount; JOptionPane.showMessageDialog( null, nameField.getText() + “ \n You have” + “ decided to take “ + classCount + “ class(es)” + “on ” + daySelection); }
public static void main(String a[]) { RadioButtonTest rt = new RadioButtonTest(); rt.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } }
// ComboBoxTest.java // Using a JComboBox to select an image to display. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxFrame extends JFrame { private JComboBox choicesJComboBox; private JLabel label; private String names[] = { “Aruba”, “Cancun”, “Dallas”, “Puerto Vallarta”, “San Juan” };
private double costs[] = {1800.00, 1300.00, 0.0, 1345.00, 1500.00}; public ComboBoxFrame() { super( "Testing JComboBox" ); Container windowPane = getContentPane(); windowPane.setLayout( new FlowLayout() ); choicesJComboBox = new JComboBox( names ); choicesJComboBox.setMaximumRowCount( 3 ); windowPane.add( choicesJComboBox ); label = new JLabel(“Make a selection ” + “for your 7 day spring break trip” ); add( label );
choicesJComboBox.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { whatSelection(e); } ); setSize(350,200); setVisible(true); } // end of constructor private void whatSelection( ItemEvent event ) { if (event.getStateChange() == ItemEvent.SELECTED) { int selection = choicesJComboBox.getSelectedIndex(); JOptionPane.showMessageDialog(null, “You selected” + names[selection] + “for” + “ a total cost of “ + costs[selection]); } }
public static void main(String a[]) { ComboBoxFrame cf = new ComboBoxFrame(); cf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } }