PowerPoint Sunusu
Download
Report
Transcript PowerPoint Sunusu
JCheckBox &JCombobox
JCheckBox
The following application uses two
JCheckBoxes to select the desired font style of
the text displayed in a JTextField.
When selected, one applies a bold style and
the other an italic style.
If both are selected, the style is bold and
italic.
When the application initially executes,
neither JCheckBox is checked (i.e., they’re
both false), so the font is plain.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
public class CheckBoxFrame extends JFrame
{
private final JTextField textField; // displays text in changing
fonts
private final JCheckBox boldJCheckBox;
// to select/deselect bold
private final JCheckBox italicJCheckBox;
// to select/deselect italic
// CheckBoxFrame constructor adds JCheckBoxes to JFrame
public CheckBoxFrame()
{ super("JCheckBox Test");
setLayout(new FlowLayout());
// set up JTextField and set its font ……….
textField = new JTextField("Watch the font style change",
20);
textField.setFont(new Font("Serif", Font.PLAIN, 14));
add(textField); // add textField to JFrame
JTextField is created and initialized ,
setFont method is inherited by JTextField indirectly from
class Component to set the font of the JTextField to a
new object of class Font (package java.awt).
The new Font is initialized with "Serif" (a generic font
name that represents a font such as Times and is
supported on all Java platforms), Font.PLAIN style and 14point size.
boldJCheckBox = new JCheckBox("Bold");
italicJCheckBox = new JCheckBox("Italic");
add(boldJCheckBox); // add bold checkbox to JFrame
add(italicJCheckBox); // add italic checkbox to Jframe
two JCheckBox objects are created
The String passed to the JCheckBox
constructor is the checkbox label that appears
to the right of the JCheckBox by default
// register listeners for JCheckBoxes
CheckBoxHandler handler = new
CheckBoxHandler();
boldJCheckBox.addItemListener(handler);
italicJCheckBox.addItemListener(handler);
}
When the user clicks a JCheckBox, an ItemEvent occurs.
This event can be handled by an ItemListener object,
which must implement method itemStateChanged.
The event handling is performed by an instance of
private inner class CheckBoxHandler
After an instance of class CheckBoxHandler is created,
It is registered with method addItemListener as the
listener for both the JCheckBox objects.
// private inner class for ItemListener event handling
private class CheckBoxHandler implements ItemListener
{ // respond to checkbox events
@Override
public void itemStateChanged(ItemEvent event)
{
Font font = null; // stores the new Font
// determine which CheckBoxes are checked and create Font
if (boldJCheckBox.isSelected() && italicJCheckBox.isSelected())
font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
else if (boldJCheckBox.isSelected())
font = new Font("Serif", Font.BOLD, 14);
else if (italicJCheckBox.isSelected())
font = new Font("Serif", Font.ITALIC, 14);
else
font = new Font("Serif", Font.PLAIN, 14);
textField.setFont(font);
} }
} // end class CheckBoxFrame
CheckBoxHandler method itemStateChanged is called
when the user clicks the either boldJCheckBox or
italicJCheckBox.
we do not determine which JCheckBox was clicked
we use both of their states to determine the font to
display.
JCheckBox method isSelected to determine if both
JCheckBoxes are selected.
a bold italic font is created by adding the Font constants
Font.BOLD and Font.ITALIC for the font-style argument of
the Font constructor.
If the boldJCheckBox is selected, a bold font is created
If the italicJCheckBox is selected, an italic font is
created
If none of the preceding conditions are true, creates a
plain font using the Font constant Font.PLAIN is created
textField.setFont(font); sets textField’s new font,
which changes the font in the JTextField on the screen.
// CheckBoxTest.java is testing CheckBoxFrame
import javax.swing.JFrame;
public class CheckBoxTest
{
public static void main(String[] args)
{
CheckBoxFrame checkBoxFrame = new CheckBoxFrame();
checkBoxFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
checkBoxFrame.setSize(275, 100);
checkBoxFrame.setVisible(true);
}
}
Relationship Between an Inner Class
and Its Top-Level Class
Class CheckBoxHandler used variables boldJCheckBox,
italicJCheckBox and textField , even though they are
not declared in the inner class.
an inner class has a special relationship with its toplevel class
it’s allowed to access all the variables and methods of the
top-level class.
CheckBoxHandler method itemStateChanged uses this
relationship to determine which JCheckBoxes are
checked and to set the font on the JTextField.
None of the code in inner class CheckBoxHandler
requires an explicit reference to the top-level class
object.
JComboBox
Using an Anonymous Inner Class for Event
Handling
A combo box (sometimes called a drop-down list)
enables the user to select one item from a list
Combo boxes are implemented with class
JComboBox, which extends class JComponent.
JComboBox is a generic class like the class ArrayList
When you create a JComboBox, you specify the
type of the objects that it manages
the JCombo-Box then displays a String representation of
each object.
// ComboBoxFrame.java displays a list of image names
import java.awt.FlowLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class ComboBoxFrame extends JFrame
{
private final JComboBox<String> imagesJComboBox;
// holds icon names
private final JLabel label; // displays selected icon
private static final String[] names =
{"bug1.gif", "bug2.gif", "travelbug.gif", "buganim.gif"};
//String array names contains the names of the four image files
that are stored in the same directory as the application
private final Icon[] icons =
{ new ImageIcon(getClass().getResource(names[0])),
new ImageIcon(getClass().getResource(names[1])),
new ImageIcon(getClass().getResource(names[2])),
new ImageIcon(getClass().getResource(names[3])) };
//declare and initialize array icons with four new ImageIcon
objects.
// ComboBoxFrame constructor adds JComboBox to JFrame
public ComboBoxFrame()
{
super("Testing JComboBox");
setLayout(new FlowLayout());
// set frame layout
imagesJComboBox = new JComboBox<String>(names);
the constructor initializes a JComboBox object with the
Strings in array names as the elements in the list.
Each item in the list has an index.
The first item is added at index 0, the next at index 1
and so forth.
The first item added to a JComboBox appears as the
currently selected item when the JComboBox is
displayed.
Other items are selected by clicking the JComboBox,
then selecting an item from the list that appears.
imagesJComboBox.setMaximumRowCount(3);
uses JComboBox method setMaximumRowCount
to set the maximum number of elements that are
displayed when the user clicks the JComboBox.
If there are additional items, the JComboBox
provides a scrollbar that allows the user to scroll
through all the elements in the list.
The user can click the scroll arrows at the top and
bottom of the scrollbar to move up and down
through the list one element at a time
imagesJComboBox = new JComboBox<String>(names);
// set up JComboBox
imagesJComboBox.setMaximumRowCount(3);
// display three rows
imagesJComboBox.addItemListener( new ItemListener()
// anonymous inner class
{ // handle JComboBox event
@Override
public void itemStateChanged(ItemEvent event)
{ // determine whether item selected
if (event.getStateChange() == ItemEvent.SELECTED)
label.setIcon(icons[ imagesJComboBox.getSelectedIndex()]);
} } // end anonymous inner class
); // end call to addItemListener
JComboBoxes generate ItemEvents just as JCheckBoxes
and JRadioButtons do.
JComboBox provides a list of four image filenames
from which the user can select one image to display.
When the user selects a name, the application displays
the corresponding image as an Icon on a JLabel.
Class ComboBoxTest contains the main method that
executes this application.
The screen captures for this application show the
JComboBox list after the selection was made to
illustrate which image filename was selected.
add(imagesJComboBox); // add combo box to Jframe
The JComboBox is attached to the
ComboBoxFrame’s FlowLayout
label = new JLabel(icons[0]);
// display first icon
the Jlabel is created that displays ImageIcons and
initializes it with the first ImageIcon in array icons.
add(label);
// add label to Jframe
The JLabel is attached to the ComboBoxFrame’s
FlowLayout
}
} // end class ComboBoxFrame
// ComboBoxTest.java is testing ComboBoxFrame.
import javax.swing.JFrame;
public class ComboBoxTest
{
public static void main(String[] args)
{
ComboBoxFrame comboBoxFrame = new
ComboBoxFrame();
comboBoxFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
comboBoxFrame.setSize(350, 150);
comboBoxFrame.setVisible(true);
}
} // end class ComboBoxTest