1 / 28

Class 20

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

nikki
Download Presentation

Class 20

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class 20

  2. Class 20 Objectives • Use JCheckbox, JRadioButton, and a JComboBox in a UID

  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. Class JCheckBox is a JComponent creates a single box that can be selected or not selected

  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. // 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;

  7. 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);

  8. 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

  9. 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)”); }

  10. public static void main(String a[]) { CheckBoxFrame cf = new CheckBoxFrame(); cf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } } // end of class

  11. Add JRadioButtons to the previous application to allow user to choose MWF or Tues./Thurs classes.

  12. // 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;

  13. // new gui components private JRadioButton dayChoice1; private JRadioButton dayChoice2; private ButtonGroupradioGroup;

  14. 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);

  15. // create logical relationship between JRadioButtons radioGroup = new ButtonGroup(); radioGroup.add( dayChoice1); radioGroup.add( dayChoice2 );

  16. 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

  17. 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); }

  18. public static void main(String a[]) { RadioButtonTest rt = new RadioButtonTest(); rt.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } }

  19. Use a JComboBox for making a selection

  20. // 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” };

  21. 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 );

  22. 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]); } }

  23. public static void main(String a[]) { ComboBoxFrame cf = new ComboBoxFrame(); cf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } }

  24. Conclusion of class 20

More Related