Download presentation
Presentation is loading. Please wait.
Published byKory Winfred French Modified over 9 years ago
1
Class 20
2
2 Class 20 Objectives Use JCheckbox, JRadioButton, and a JComboBox in a UID
3
3 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)
4
4 Class JCheckBox is a JComponent creates a single box that can be selected or not selected
5
5 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.
6
6
7
7 // 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;
8
8 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);
9
9 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
10
10 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)”); }
11
11 public static void main(String a[]) { CheckBoxFrame cf = new CheckBoxFrame(); cf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } } // end of class
12
12 Add JRadioButtons to the previous application to allow user to choose MWF or Tues./Thurs classes.
13
13
14
14 // 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;
15
15 private JRadioButton dayChoice1; private JRadioButton dayChoice2; private ButtonGroup radioGroup; // new gui components
16
16 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);
17
17 // create logical relationship between JRadioButtons radioGroup = new ButtonGroup(); radioGroup.add( dayChoice1); radioGroup.add( dayChoice2 );
18
18 choices = new JCheckBox[classes.length]; for (int i = 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(ActionEvent ev) { buttonClicked(ev) }; } ); setSize(350,200); setVisible(true); } // end of constructor
19
19 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); }
20
20 public static void main(String a[]) { RadioButtonTest rt = new RadioButtonTest(); rt.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }
21
21 Use a JComboBox for making a selection
22
22
23
23
24
24 // 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” };
25
25 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 );
26
26 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]); }
27
27 public static void main(String a[]) { ComboBoxFrame cf = new ComboBoxFrame(); cf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }
28
28 Conclusion of class 20
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.