创建JRadioButton组件 : 单选按钮 « Swing « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » Swing » 单选按钮 
14. 9. 1. 创建JRadioButton组件
  1. A JRadioButton represents a radio button.
  2. You can use multiple JRadioButtons to represents a selection from which only one item can be selected.
  3. To indicate a logical grouping of radio buttons, you use a javax.swing.ButtonGroup object.
  4. To programmatically select a JRadioButton, you pass true to its setSelected method.
public JRadioButton()
JRadioButton aRadioButton = new JRadioButton();

public JRadioButton(Icon icon)
JRadioButton aRadioButton = new JRadioButton(new DiamondIcon(Color.CYAN, false));
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));

public JRadioButton(Icon icon, boolean selected)
JRadioButton aRadioButton = new JRadioButton(new DiamondIcon(Color.CYAN, false),  true);
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));

public JRadioButton(String text)
JRadioButton aRadioButton = new JRadioButton("4 slices");

public JRadioButton(String text, boolean selected)
JRadioButton aRadioButton = new JRadioButton("8 slices"true);

public JRadioButton(String text, Icon icon)
JRadioButton aRadioButton = new JRadioButton("12 slices",  new DiamondIcon(Color.CYAN, false));
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));

public JRadioButton(String text, Icon icon, boolean selected)
JRadioButton aRadioButton = new JRadioButton("16 slices",  new DiamondIcon(Color.CYAN, false)true);
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));

public JRadioButton(Action action)
Action action = ...;
JRadioButton aRadioButton = new JRadioButton(action);
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MainClass {

  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JRadioButtonDemo());
    f.setSize(500500);
    f.setVisible(true);
  }
}

class JRadioButtonDemo extends JPanel implements ActionListener {
  JTextField tf;

  public void init() {
    try {
      SwingUtilities.invokeAndWait(new Runnable() {
        public void run() {
          makeGUI();
        }
      });
    catch (Exception exc) {
      System.out.println("Can't create because of " + exc);
    }
  }

  private void makeGUI() {

    setLayout(new FlowLayout());

    JRadioButton b1 = new JRadioButton("A");
    b1.addActionListener(this);
    add(b1);

    JRadioButton b2 = new JRadioButton("B");
    b2.addActionListener(this);
    add(b2);

    JRadioButton b3 = new JRadioButton("C");
    b3.addActionListener(this);
    add(b3);

    ButtonGroup bg = new ButtonGroup();
    bg.add(b1);
    bg.add(b2);
    bg.add(b3);

    tf = new JTextField(5);
    add(tf);
  }

  public void actionPerformed(ActionEvent ae) {
    tf.setText(ae.getActionCommand());
  }
}
14. 9. 单选按钮
14. 9. 1. 创建JRadioButton组件
14. 9. 2. 使用JRadioButton使用JRadioButton
14. 9. 3. JRadioButton组件ButtonGroupJRadioButton组件ButtonGroup
14. 9. 4. 在一个按钮组确定所选JRadioButton
14. 9. 5. JRadioButton与ActionListenerJRadioButton与ActionListener
14. 9. 6. Reference to the last selected item and check for reselectionReference to the last selected item and check for reselection
14. 9. 7. JRadioButton, ChangeListenerJRadioButton, ChangeListener
14. 9. 8. JRadioButton与ItemListenerJRadioButton与ItemListener
14. 9. 9. 事件序列事件序列
14. 9. 10. 获得选中的JRadioButton获得选中的JRadioButton
14. 9. 11. Customizing JRadioButton Look and Feel
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.