目录

JRadioButton

介绍 (Introduction)

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

Class 声明 (Class Declaration)

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

public class JRadioButton
   extends JToggleButton
      implements Accessible

类构造函数 (Class Constructors)

Sr.No. 构造函数和描述
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)

创建具有指定文本,图像和选择状态的单选按钮。

Class Methods

Sr.No. 方法和描述
1

AccessibleContext getAccessibleContext()

获取与此JRadioButton关联的AccessibleContext。

2

String getUIClassID()

返回呈现此组件的L&F类的名称。

3

protected String paramString()

返回此JRadioButton的字符串表示形式。

4

void updateUI()

将UI属性重置为当前外观的值。

方法继承 (Methods Inherited)

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

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

JRadioButton示例

使用您选择的任何编辑器创建以下Java程序,例如D:/ 》 SWING 》 com 》 iowiki 》 gui 》

SwingControlDemo.java

package com.iowiki.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showRadioButtonDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      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("Apple", true);
      final JRadioButton radMango = new JRadioButton("Mango");
      final JRadioButton radPeer = new JRadioButton("Peer");
      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("Apple RadioButton: " 
               + (e.getStateChange()==1?"checked":"unchecked"));
         }           
      });
      radMango.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {             
            statusLabel.setText("Mango RadioButton: " 
               + (e.getStateChange()==1?"checked":"unchecked")); 
         }           
      });
      radPeer.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {             
            statusLabel.setText("Peer 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);  
   }
}

使用命令提示符编译程序。 转到D:/ 》 SWING并键入以下命令。

D:\SWING>javac com\iowiki\gui\SwingControlDemo.java

如果没有错误发生,则表示编译成功。 使用以下命令运行该程序。

D:\SWING>java com.iowiki.gui.SwingControlDemo

验证以下输出。

Swing JRadioButton
↑回到顶部↑
WIKI教程 @2018