🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #21568

Java ͼ���û��������3��ť���л���ť����ѡ��ť�͵

#21568
From: ����è->�ú�ѧϰ...
Date: Thu, 27 Nov 2003 16:38
190 lines
13874 bytes
��. ��ť���л���ť����ѡ��ť�͵�ѡ��ť
������ť�����ǰ�ť����������ť����֪���ɣ�
�����л���ť��������״̬�İ�ť��������״̬�͵���״̬������Ϊѡ��״̬��δѡ��
״̬��
������ѡ��ť���ֽи�ѡ����һ��С�������Ƿ������ʾ����״̬��
������ѡ��ť���ֽ�������ť����СԲ�����ʾ��ѡ�С���������֣�һ�鵥ѡ��
ť��ֻ��һ���ܱ�ѡ�С�
��������ʲô���𣿡������ˣ���һ�������ڽ����ָ���İ�ť�����Һ����ְ�ť����
����״̬���ȿ�����Щ��ť������ʲô��
������ͼ�У����ϵ��£����ξ��ǰ�ť���л���ť����ѡ��ť�͵�ѡ��ť��ͼʾ�Ĵ���
����������������ӵ����н��
  /**
 * 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) �ṩ��һ�� isSelect
ed() ���������жϵ�ǰ���״̬������ֵΪ�� (true) ʱ��ʾ����ѡ��״̬������
ֵΪ�� (false) ʱ��ʾ����δѡ��״̬����ѡ��ť (JCheckBox) �͵�ѡ��ť (JR
adioButton) ���Ǵ� JToggleButton �̳еģ�����Ҳ���� isSelected() ������������
�� if (toggle.isSelected()) { ... �ȡ�
������ѡ��ť��������ص���������DZ��������֣�����һ����ֻ����һ���ܱ�ѡ��
�����������Ҫ��һ��ר�ŵ��ࡪ��ButtonGroup���������?��ӵ� ButtonGroup ��
�����ѡ��ť�У������һ����ѡ���У�ͬ���е�����ѡ��ť�����Զ��ı���״̬Ϊ
δѡ��״̬���� ButtonGroup ����Ӱ�ť����ʹ����� add �������������е� group
.add(radio1);��
������Ȼ�����Ѿ��������ѡ��ť��ӵ�һ�� ButtonGroup ���ˣ���ô�����Dz��ǿ���
��һ��������ѡ��ť�� 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)��
��ť���л���ť�͵�ѡ��ť������ʱ���ᴥ�������¼������������������� actionP
erformed ��������ˣ���������ڵ�����ť֮����ʲô�£���ȻӦ������ ActionList
ener �� actionPerformed �����ˡ����ְ�ť���ṩ�� addActionListener ���������
������������
������ѡ���Ҫ����һЩ����Ȼ��Ҳ�� addActionListener ��������ζ�ſ���ʹ�ö���
������������ʹ��֮����ᷢ�ֶ�����������û����Ԥ������á�Ϊʲô��ԭ������
��һ����ѡ��ť�������IJ��Ƕ����¼���������Ŀ�¼� (ItemEvent) �е�״̬�仯 (i
temStateChanged) �¼�������Ŀ������ (ItemListener) ������Ӧ��Ҫ���صķ�����
 ItemListener �� itemStateChanged ������
�������������ǽ�һ����Ϊ al �� ActionListener ��ӵ���ÿһ����ѡ��ť�У����
�ж����ĸ���ѡ��ť�������¼����� al �������أ����ǿ��Դ� ActionEvent �� ge
tSource() �����õ������¼���ѡ��ť������ getSource() ���ص���һ�� Object ����
����Ȼ�������ָ�����һ����ѡ��ť��ʵ�����ǻ�����Ҫ��������õ�����ת��
Ϊ JRadioButton���������еģ�JRadioButton radio = (JRadioButton) ae.getSourc
e();��ֻ���������Dz��ܵ��� JRadioButton �ж� Object û�еķ�����
����ͬʱ������Ҫ˵����һ���ǣ�ÿ����ѡ��ť���������һ�������� ActionListene
r ʵ���һ��Ҫ���ͬһ����ͬ��ĵ��?���ɺ�����ɵġ���Ҫ��� ActionLis
tener �����������Ҳ�������ͬһ�� ActionListener ʵ��ؼ����ڱ���߶� act
ionPerformed ���������ء�����������δ����Ϊһ�� JButton �����һ�� JRadioB
utton ���������ͬһ������������ʵ��
  /**
 * 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
������˵��������õ�������ͬһ���������ǿ��еġ�������ǰ������Щ���������
�������������

--
���������ʱ������㰮����һ���ˣ����㣬���㣬һ��Ҫ����ضԴ��������
�����֣�ҲҪ�úõ�˵���ټ������Ժ���Ż�֪��������Ȼ���׵�ɲ�ǣ�û��Թ�޵�
�ഺ�Ż������ź�����ɽ�������־��������¡���
����                                                      -----ϯĽ�� 

[23��1H
�� ��Դ:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 211.80.91.3]
Technical Details
Message-ID: <49QaO7$9mS@bbs.sjtu.edu.cn>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!c03.atl99!sjc70.webusenet.com!news.webusenet.com!news.bylinux.net!news.ccie.net.cn!SJTU

Show Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB
X-Google-Thread: f6cb0,716dfd081a57164f,start
X-Google-Attributes: gidf6cb0,public
X-Google-ArrivalTime: 2003-11-27 08:36:02 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!c03.atl99!sjc70.webusenet.com!news.webusenet.com!news.bylinux.net!news.ccie.net.cn!SJTU
From: bocoo.bbs@bbs.sjtu.edu.cn (����è->�ú�ѧϰ...)
Newsgroups: cn.bbs.comp.lang.java
Subject: Java ͼ���û��������3��ť���л���ť����ѡ��ť�͵
Date: 27 Nov 2003 16:38:55 GMT
Organization: ��ˮ˼Դ
Message-ID: <49QaO7$9mS@bbs.sjtu.edu.cn>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: Java/M.1069951111.A
Lines: 190