橘子味的心
标题:Swing JRadioButton

JRadioButton类是单选按钮的实现 - 可以选择或取消选择的项,并向用户显示其状态。

类声明

以下是javax.swing.JRadioButton类的声明 -

public class JRadioButton
   extends JToggleButton
      implements Accessible

类构造函数

编号 构造函数 描述
1 JRadioButton() 创建一个没有设置文本的最初未选中的单选按钮。
2 JRadioButton(Action a) 创建一个radiobutton实例,属性取自提供的Action
3 JRadioButton(Icon icon) 使用指定的图像创建最初未选中的单选按钮,但不显示文本。
4 JRadioButton(Icon icon, boolean selected) 创建具有指定图像和选择状态但没有文本的单选按钮。
5 JRadioButton(String text, boolean selected) 创建具有指定文本和选择状态的单选按钮。
6 JRadioButton(String text, Icon icon) 创建一个具有指定文本和图像的单选按钮,此按钮最初未被选中。
7 JRadioButton(String text, Icon icon, boolean selected) 创建具有指定文本,图像和选择状态的单选按钮。

类方法

编号 方法 描述
1 AccessibleContext getAccessibleContext() 获取与此JRadioButton关联的AccessibleContext
2 String getUIClassID() 返回呈现此组件的L&F类的名称。
3 protected String paramString() 返回此JRadioButton的字符串表示形式。
4 void updateUI() 将UI属性重置为当前外观的值。

方法继承

该类继承以下类中的方法 -

  • javax.swing.AbstractButton
  • javax.swing.JToggleButton
  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JRadioButton示例

使用编辑器创建以下Java程序:JRadioButtonExample.java



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JRadioButtonExample {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public JRadioButtonExample(){
      prepareGUI();
   }
   public static void main(String[] args){
       JRadioButtonExample  swingControlDemo = new JRadioButtonExample();      
      swingControlDemo.showRadioButtonDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing JRadioButton示例");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showRadioButtonDemo(){
      headerLabel.setText("Control in action: RadioButton"); 

      final JRadioButton radApple = new JRadioButton("Java/Swing", true);
      final JRadioButton radMango = new JRadioButton("Python");
      final JRadioButton radPeer = new JRadioButton("MySQL");

      radApple.setMnemonic(KeyEvent.VK_C);
      radMango.setMnemonic(KeyEvent.VK_M);
      radPeer.setMnemonic(KeyEvent.VK_P);

      radApple.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {         
            statusLabel.setText("Java/Swing RadioButton: " 
               + (e.getStateChange()==1?"checked":"unchecked"));
         }           
      });
      radMango.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {             
            statusLabel.setText("Python RadioButton: " 
               + (e.getStateChange()==1?"checked":"unchecked")); 
         }           
      });
      radPeer.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {             
            statusLabel.setText("MySQL RadioButton: " 
               + (e.getStateChange()==1?"checked":"unchecked"));
         }           
      });

      //Group the radio buttons.
      ButtonGroup group = new ButtonGroup();

      group.add(radApple);
      group.add(radMango);
      group.add(radPeer);

      controlPanel.add(radApple);
      controlPanel.add(radMango);
      controlPanel.add(radPeer);       

      mainFrame.setVisible(true);  
   }
}

执行上面示例代码,得到以下结果 -


目录

分类