Download Class 1 ~ Chapter 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Class 20
Class 20 Objectives

Use JCheckbox, JRadioButton, and a
JComboBox in a UID
2

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)
3
Class JCheckBox


is a JComponent
creates a single box that can be
selected or not selected
4
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.
5
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
13
// 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;
14
// new gui components
private JRadioButton dayChoice1;
private JRadioButton dayChoice2;
private ButtonGroup radioGroup;
15
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);
16
// create logical relationship between JRadioButtons
radioGroup = new ButtonGroup();
radioGroup.add( dayChoice1);
radioGroup.add( dayChoice2 );
17
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
18
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);
}
19
public static void main(String a[])
{ RadioButtonTest rt = new RadioButtonTest();
rt.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
}
20
Use a JComboBox for making
a selection
21
22
23
// 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” };
24
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 );
25
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]);
}
26
public static void main(String a[])
{ ComboBoxFrame cf = new ComboBoxFrame();
cf.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
}
27
Conclusion of class 20
28