Java图形用户界面设计3

来源:互联网 发布:c语言中并且怎么表示 编辑:程序博客网 时间:2024/05/16 07:37
三. 按钮、切换按钮、复选按钮和单选按钮

  按钮,就是按钮,不会连按钮都不知道吧?

  切换按钮,有两种状态的按钮,即按下状态和弹起状态,若称为选择状态或未选择状态。

  复选按钮,又叫复选框,用一个小方框中是否打勾来表示两种状态。

  单选按钮,又叫收音机按钮,以小圆框打点表示被选中。常成组出现,一组单选按钮中只有一个能被选中。

  发现什么了吗?——对了,这一部分是在讲各种各样的按钮,而且后三种按钮都有两种状态。先看看这些按钮都长成什么样:


  上图中,从上到下,依次就是按钮、切换按钮、复选按钮和单选按钮。图示的窗口,就是下面这个例子的运行结果:

/**
* 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); 
原创粉丝点击