第9章 反转按钮、复选框和单选框
本章介绍三种Swing按钮:反转按钮、复选按钮和单选按钮,它们分别由JToggleButton、JCheckBox和JRadioButton类代表。
这三种按钮都是AbstractButton类的最终扩展。AbstractButton类实现了这三个类提供的几乎所有功能。有关AbstractButton类的更多信息,请参见第8章“标签与按钮”。
9.1 JToggleButton类
例9-1 JToggleButton的一个简单例子
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
ImageIcon icon = new ImageIcon("togglebuttonImage.gif");
JToggleButton button = new JToggleButton(icon);
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
}
}
9.1.1 JToggleButton属性
9.1.2 JToggleButton事件
例9-2 用一个子项监听器来处理反转按钮的选取操作
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
ImageIcon icon = new ImageIcon("togglebuttonImage.gif");
final JToggleButton button = new JToggleButton(icon);
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
button.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int state = e.getStateChange();
String s;
if(state == ItemEvent.SELECTED) s = "selected";
else s = "deselected";
showStatus("Item Event: " + s);
}
});
}
}
9.1.3 JToggleButton类总结
例9-3 创建反转按钮
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
Icon icon = new ImageIcon("beach_umbrella.gif");
JToggleButton button_1 = new JToggleButton(),
button_2 = new JToggleButton(icon),
button_3 = new JToggleButton(icon, true),
button_4 = new JToggleButton("beach"),
button_5 = new JToggleButton("beach",true),
button_6 = new JToggleButton("beach",icon),
button_7 = new JToggleButton("beach",icon,
true);
contentPane.setLayout(new FlowLayout());
contentPane.add(button_1);
contentPane.add(button_2);
contentPane.add(button_3);
contentPane.add(button_4);
contentPane.add(button_5);
contentPane.add(button_6);
contentPane.add(button_7);
}
}
9.1.4 AWT兼容
9.2 按钮框
例9-4 为相互排斥的选取行为使用一个按钮组
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
ButtonGroup group = new ButtonGroup();
JToggleButton[] buttons = new JToggleButton[] {
new JToggleButton(new ImageIcon("ballot_box.gif")),
new JToggleButton(new ImageIcon("palette.gif")),
new JToggleButton(new ImageIcon("light_bulb1.gif")),
new JToggleButton(new ImageIcon("dining.gif")),
new JToggleButton(new ImageIcon("scissors.gif")),
new JToggleButton(new ImageIcon("tricycle.gif")),
};
Box box = Box.createVerticalBox();
for(int i=0; i < buttons.length; ++i) {
group.add(buttons[i]);
box.add(Box.createVerticalStrut(5));
box.add(buttons[i]);
}
box.add(Box.createVerticalStrut(5));
contentPane.add(box);
}
}
9.3 复选框
例9-5 运行中的Swing复选框
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();
contentPane.add(new PrintOptionsPanel(this), "Center");
}
}
class PrintOptionsPanel extends JPanel {
JCheckBox oddPages, evenPages, collate, lastFirst;
Listener listener = new Listener();
JApplet applet;
public PrintOptionsPanel(JApplet applet) {
this.applet = applet;
oddPages = new JCheckBox("Odd Pages");
evenPages = new JCheckBox("Even Pages");
collate = new JCheckBox("Collate");
lastFirst = new JCheckBox("Last Page First");
oddPages.addItemListener (listener);
evenPages.addItemListener(listener);
collate.addItemListener (listener);
lastFirst.addItemListener(listener);
add(oddPages);
add(evenPages);
add(collate);
add(lastFirst);
}
class Listener implements ItemListener {
public void itemStateChanged(ItemEvent event) {
applet.showStatus(
"Odd Pages: " + oddPages.isSelected() + ", " +
"Even Pages: " + evenPages.isSelected() + ", " +
"Collate: " + collate.isSelected() + ", " +
"Last Page First: " + lastFirst.isSelected());
}
}
}
9.3.1 JCheckBox属性
9.3.2 JCheckBox事件
9.3.3 JCheckBox类总结
例9-6 实例化复选框
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
Icon icon = new ImageIcon("bulb.gif");
JCheckBox[] checkboxes = new JCheckBox[] {
new JCheckBox(),
new JCheckBox(icon),
new JCheckBox(icon, true),
new JCheckBox("idea!"),
new JCheckBox("idea!",true),
new JCheckBox("idea!",icon),
new JCheckBox("idea!",icon, true)
};
contentPane.setLayout(new FlowLayout());
for(int i=0; i < checkboxes.length; ++i) {
checkboxes[i].setBorderPainted(true);
contentPane.add(checkboxes[i]);
if(checkboxes[i].getIcon() != null) {
System.out.println("setting selected icon");
checkboxes[i].setSelectedIcon(
new ImageIcon("bulb_bright.gif"));
}
}
}
}
9.4 单选钮
例9-7 运行中的单选钮
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();
contentPane.add(new PrintRangePanel(100, 101));
}
}
class PrintRangePanel extends JPanel {
JRadioButton printAll, printRange;
JLabel startPage, endPage;
JTextField startField, endField;
public PrintRangePanel(int start, int end) {
ButtonGroup group= new ButtonGroup();
printAll = new JRadioButton("Print All");
printRange = new JRadioButton("Print Range");
startPage = new JLabel("Start Page:");
endPage = new JLabel("End Page:");
startField = new JTextField(Integer.toString(start));
endField = new JTextField(Integer.toString(end));
add(printAll); add(printRange);
add(startPage); add(startField);
add(endPage); add(endField);
printRange.setSelected(true);
group.add(printAll);
group.add(printRange);
printRange.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(printRange.isSelected()) {
startField.setEnabled(true);
endField.setEnabled (true);
startPage.setEnabled (true);
endPage.setEnabled (true);
//startPage.repaint();
//endPage.repaint();
startField.requestFocus();
}
}
});
printAll.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(printAll.isSelected()) {
startField.setEnabled(false);
endField.setEnabled (false);
startPage.setEnabled (false);
endPage.setEnabled (false);
startPage.repaint();
endPage.repaint();
}
}
});
}
}
9.4.1 JRadioButton属性
9.4.2 JRadioButton事件
9.4.3 JRadioButton类总结
例9-8 实例化单选按钮
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
Icon icon = new ImageIcon("bulb.gif");
JRadioButton[] radioButtons = new JRadioButton[] {
new JRadioButton(),
new JRadioButton(icon),
new JRadioButton(icon, true),
new JRadioButton("idea!"),
new JRadioButton("idea!",true),
new JRadioButton("idea!",icon),
new JRadioButton("idea!",icon, true)
};
contentPane.setLayout(new FlowLayout());
for(int i=0; i < radioButtons.length; ++i) {
radioButtons[i].setBorderPainted(true);
contentPane.add(radioButtons[i]);
if(radioButtons[i].getIcon() != null) {
System.out.println("setting selected icon");
radioButtons[i].setSelectedIcon(
new ImageIcon("bulb_bright.gif"));
}
}
}
}