简述 Java 图形用户界面设计 (Swing) - 学院 - CHINAUI.com 优艾网 - 中国人机界面设计门户网站
您的位置:首页 >> 学院 >> 交互技术 >> 简述 Java 图形用户界面设计 (Swing)
简述 Java 图形用户界面设计 (Swing)
来源:CSDN 作者:边城狂人 发布时间:2008-03-21
 
三. 按钮、切换按钮、复选按钮和单选按钮
 
  按钮……就是按钮,不会连按钮都不知道吧?
  切换按钮,有两种状态的按钮,即按下状态和弹起状态,若称为选择状态或未选择状态。
  复选按钮,又叫复选框,用一个小方框中是否打勾来表示两种状态。
  单选按钮,又叫收音机按钮,以小圆框打点表示被选中。常成组出现,一组单选按钮中只有一个能被选中。
 
  发现什么了吗?——对了,这一部分是在讲各种各样的按钮,而且后三种按钮都有两种状态。先看看这些按钮都长成什么样:
 
 
  上图中,从上到下,依次就是按钮、切换按钮、复选按钮和单选按钮。图示的窗口,就是下面这个例子的运行结果:
 
/*
 * TestButtons.java
 * @author Fancy
 
*/


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;

public final class TestButtons {

    
public static void main(String[] args) {
        TestButtons tb 
= new TestButtons();
        tb.show();
    }


    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(
200250);
    }


    
public void show() {
        frame.setVisible(
true);
    }


}
 
  除一般按钮外,其余三种按钮都有两种状态,即选择 (按下) 状态和未选择 (弹起) 状态。那么我们又该如何判断呢?切换按钮 (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 event)。按钮、切换按钮和单选按钮被单击时都会触发动作事件,引起动作监听器调用 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 James
 
*/


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
 
  这说明多个不用的组件添加同一个监听器是可行的——不过前提是这些组件都能添加这个监听器。
 
相关阅读
推荐文章
热点文章