三.
按钮、切换按钮、复选按钮和单选按钮 按钮,就是按钮,不会连按钮都不知道吧?
切换按钮,有两种状态的按钮,即按下状态和弹起状态,若称为选择状态或未选择状态。
复选按钮,又叫复选框,用一个小方框中是否打勾来表示两种状态。
单选按钮,又叫收音机按钮,以小圆框打点表示被选中。常成组出现,一组单选按钮中只有一个能被选中。
发现什么了吗?——对了,这一部分是在讲各种各样的按钮,而且后三种按钮都有两种状态。先看看这些按钮都长成什么样:
上图中,从上到下,依次就是按钮、切换按钮、复选按钮和单选按钮。图示的窗口,就是下面这个例子的运行结果:
/**
* TestButtons.java
* @author Fancy
*/
import javax.swing.*;
import java.awt.event.*;
public class TestButtons {
JFrame frame = new JFrame("Test Buttons");
JButton jButton = new JButton("JButton"); //按钮
JToggleButton toggle = new JToggleButton("Toggle Button");
//切换按钮
JCheckBox checkBox = new JCheckBox("Check Box"); //复选按钮
JRadioButton radio1 = new JRadioButton("Radio Button 1");
//单选按钮
JRadioButton radio2 = new JRadioButton("Radio Button 2");
JRadioButton radio3 = new JRadioButton("Radio Button 3");
JLabel label = new JLabel("Here is Status, look here.");
//不是按钮,是静态文本
public TestButtons() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new
java.awt.FlowLayout());
/* 为一般按钮添加动作监听器 */
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
label.setText("You clicked jButton");
}
});
/* 为切换按钮添加动作监听器 */
toggle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JToggleButton toggle = (JToggleButton) ae.getSource();
if (toggle.isSelected()) {
label.setText("You selected Toggle Button");
} else {
label.setText("You deselected Toggle Button");
}
}
});
/* 为复选按钮添加条目监听器 */
checkBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JCheckBox cb = (JCheckBox) e.getSource();
label.setText("Selected Check Box is " +
cb.isSelected());
}
});
/* 用一个按钮组对象包容一组单选按钮 */
ButtonGroup group = new ButtonGroup();
/* 生成一个新的动作监听器对象,备用 */
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JRadioButton radio = (JRadioButton) ae.getSource();
if (radio == radio1) {
label.setText("You selected Radio Button 1");
} else if (radio == radio2) {
label.setText("You selected Radio Button 2");
} else {
label.setText("You selected Radio Button 3");
}
}
};
/* 为各单选按钮添加动作监听器 */
radio1.addActionListener(al);
radio2.addActionListener(al);
radio3.addActionListener(al);
/* 将单选按钮添加到按钮组中 */
group.add(radio1);
group.add(radio2);
group.add(radio3);
frame.getContentPane().add(jButton);
frame.getContentPane().add(toggle);
frame.getContentPane().add(checkBox);
frame.getContentPane().add(radio1);
frame.getContentPane().add(radio2);
frame.getContentPane().add(radio3);
frame.getContentPane().add(label);
frame.setSize(200, 250);
}
public void show() {
frame.show();
}
public static void main(String[] args) {
TestButtons tb = new TestButtons();
tb.show();
}
}
除一般按钮外,其余三种按钮都有两种状态,即选择 (按下)
状态和未选择 (弹起) 状态。那么我们又该如何判断呢?切换按钮
(JToggleButton) 提供了一个 isSelected()
方法用来判断当前所处的状态,返回值为真 (true)
时表示它处于选择状态,返回值为假 (false)
时表示它处于未选择状态。而复选按钮 (JCheckBox) 和单选按钮
(JRadioButton) 都是从 JToggleButton 继承的,所以也具有 isSelected()
方法。如上例中 if (toggle.isSelected()) { ... 等。
单选按钮由自身的特点决定了它们必须成组出现,而且一组中只能有一个
能被选中。因此我们需要用一个专门的类——ButtonGroup——来管理。添加到
ButtonGroup
的多个单选按钮中,如果有一个被选择中,同组中的其它单选按钮都会自动改变其状态为未选择状态。在
ButtonGroup 中添加按钮,是使用它的 add 方法,如上例中的
group.add(radio1);。
既然我们已经将多个单选按钮添加到一个 ButtonGroup
中了,那么我们是不是可以将一个包含多个单选按钮的 ButtonGroup
对象添加到 JFrame 的 Content Pane
中,以达到添加其中所有单选按钮的目的呢?不行!ButtonGroup
不是一个可显示的组件,它仅用于管理。所以,在往 JFrame 中添加一组
JRadioButton 的时候,需要一个一个的添加
JRadioButton,而不是笼统的添加一个 ButtonGroup。
上例中还用到了
JLabel,这不是按钮,而是一个静态文本组件,主要用于显示提示文本。要获得一个
JLabel 对象当前显示的文本内容,可以使用它的 getText()
方法;反之,要改变一个 JLabel 对象显示的文本内容,应该使用它的
setText(String text) 方法,如上例中的 label.setText("You selected
Toggle Button");。
其实这两个方法同样可以用于 JButton 等类。比如上例中我们使用 new
JButton("JButton") 构造了一个按钮 jButton,如果使用
jButton.getText() 就可以得到字符串 "JButton"。而 jButton.setText("A
Button"),则可以改变按钮上显示的文字为 "A
Button"。这两句代码没有写出来,你可以自己试试。
上例中大量使用了动作监听器 (ActionListener)。ActionListener
只监听一个事件,这个事件在其相关组件上产生了动作时被触发,因此叫作动作事件
(ActionEvent)。ActionListener 只有一个方法需要实现,就是
actionPerformed(ActionEvent
ae)。按钮、切换按钮和单选按钮被单击时都会触发动作事件,引起动作监听器调用
actionPerformed
方法。因此,如果你想在单击按钮之后做什么事,当然应该重载
ActionListener 的 actionPerformed 方法了。各种按钮都提供了
addActionListener 方法以添加动作监听器。
复选框就要特殊一些。虽然它也有 addActionListener
方法,意味着可以使用动作监听器,但是使用之后你会发现动作监听器并没有起到预想的作用。为什么?原来,单击一个复选按钮,触发的不是动作事件,而是条目
事件 (ItemEvent) 中的状态变化 (itemStateChanged) 事件,由条目监听器
(ItemListener) 监听,相应需要重载的方法是 ItemListener 的
itemStateChanged 方法。
上例中我们将一个名为 al 的 ActionListener
添加到了每一个单选按钮中,如何判断是哪个单选按钮触发了事件并被 al
监听到了呢?我们可以从 ActionEvent 的 getSource()
方法得到触发事件单选按钮。由于 getSource() 返回的是一个 Object
引用,虽然这个引用指向的是一个单选按钮的实例,但我们还是需要将这个引用的类型转换为
JRadioButton,如上例中的:JRadioButton radio = (JRadioButton)
ae.getSource();,只有这样我们才能调用 JRadioButton 有而 Object
没有的方法。
同时,还需要 说明的一点是,每个单选按钮都可以添加一个单独的
ActionListener
实例,而不一定要添加同一个。同样的道理,若干毫不相干的、需要添加
ActionListener 的若干组件,也可以添加同一个 ActionListener
实例。关键在于编程者对 actionPerformed
方法的重载。比如下面这段代码就为一个 JButton 对象和一个
JRadioButton 对象添加了同一个动作监听器实例:
/**
* Test.java
* @author Fancy
*/
import javax.swing.*;
import java.awt.event.*;
public class Test {
JButton b;
JRadioButton rb;
public Test() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new java.awt.FlowLayout());
b = new JButton("JButton");
rb = new JRadioButton("RadioButton");
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b) {
System.out.println("You clicked the JButton");
} else {
System.out.println("You clicked the RadioButton");
}
}
};
b.addActionListener(a);
rb.addActionListener(a);
f.getContentPane().add(b);
f.getContentPane().add(rb);
f.pack();
f.show();
}
public static void main(String[] args) {
new Test();
}
}
运行程序后,分别单击两个按钮,相应的,在控制台能分别得到如下输出:
You clicked the JButton
You clicked the RadioButton
这说明多个不用的组件添加同一个监听器是可行的——不过前提是这些组件都能添加这个监听器。
四. 文本输入框、密码输入框
文本输入框包括两种,单行文本输入框 (JTextField)
和多行文本输入框 (JTextArea)。密码输入框则只有一种
(JPasswordField)。JPasswordField 是 JTextField
的子类,它们的主要区别是 JPasswordField
不会显示出用户输入的东西,而只会显示出程序员设定的一个固定字符,比如
'*'。
下面的示例图和代码是 JTextField、JPasswordField 和 JTextArea
的示例:
/**
* TestTexts.java
* @author Fancy
*/
import javax.swing.*;
import javax.swing.event.*;
public class TestTexts extends JFrame {
private JLabel label = new JLabel("Status");
private JTextField textField;
private JPasswordField pwdField;
private JTextArea textArea;
public TestTexts() {
super("Test Texts");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
textField = new JTextField(15);
/* 监听文本光标移动事件 */
textField.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
// 如果改变了内容,就可以即时更新 label 显示的内容
label.setText(textField.getText());
}
});
pwdField = new JPasswordField(15);
pwdField.setEchoChar('#');
textArea = new JTextArea(5, 15);
textArea.setLineWrap(true);
getContentPane().add(textField);
getContentPane().add(pwdField);
getContentPane().add(textArea);
getContentPane().add(label);
setSize(200, 200);
}
public static void main(String[] args) {
TestTexts tt = new TestTexts();
tt.show();
}
}
上例中,我们构造了一个宽度为 15 个字符的单行文本框 (textField
= new JTextField(15);),并使用 addCaretListener 方法添加了一个
CaretListener (textField.addCaretListener ...)。CaretListener
监听文本光标的移动事件。当用户使用键盘、鼠标等移动了文本光标在
JTextField 中的位置时触发这个事件。我们需要重载
caretUpdate(CaretEvent e) 对事件进行处理 (public void
caretUpdate(CaretEvent e) ...)。这样,我们可以在这里做类似 VB 中
TextBox 的 OnChange 事件中做的事情。
JTextField 有 5 个构造方法,常用其中的四个:
JTextField()
JTextField(int columns),如上例 textField = new
JTextField(15);
JTextField(String text)
JTextField(String text, int columns)
其中,参数 text 是单行文本框的初始内容,而 columns
指定了单行文本框的宽度,以字符为单位。JTextField 中的文本内容可以用
getText() 方法获得。也可以用 setText 方法指定 JTextField
中的文本内容。
JPasswordField 是 JTextField
的子类,其构造方法也是类似的。JPasswordField 提供了
setEchoChar(char ch) 方法设置为了隐藏密码而显示的字符,默认为 '*'
字符,上例中则设置为了 '#' 字符 (pwdField.setEchoChar('#');)。与
JTextField 一样,JPasswordField 也用 getText 方法和 setText
获得或者设置文本内容 (当然在用户界面上是隐藏的)。
JTextField
是单行文本框,不能显示多行文本,如果想要显示多行文本,就只好使用多行文本框
JTextArea 了。JTextArea 有六个构造方法,常用的也是四个:
JTextArea()
JTextArea(int rows, int columns)
JTextArea(String text)
JTextArea(String text, int rows, int columns)
text 为 JTextArea 的初始化文本内容;rows 为 JTextArea
的高度,以行为单位;columns 为 JTextArea
的宽度,以字符为单位。如上例中就构造了一个高 5 行,宽 15
个字符的多行文本框 (textArea = new JTextArea(5, 15);)。
多行文本框默认是不会自动折行的
(不过可以输入回车符换行),我们可以使用 JTextArea 的 setLineWrap
方法设置是否允许自动折行。setLineWrap(true)
是允许自动折行,setLineWrap(false)
则是不允许自动折行。多行文本框会根据用户输入的内容自动扩展大小,不信,自己做个实验——如果不自动折行,那么多行文本框的宽度由最长的一行文字确定
的;如果行数据超过了预设的行数,则多行文本框会扩展自身的高度去适应。换句话说,多行文本框不会自动产生滚动条。怎么办?后面讲到滚动窗格
(JScrollPane) 的时候,你就知道了。
多行文本框里文本内容的获得和设置,同样可以使用 getText 和
setText 两个方法来完成。