Java 单选按钮

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java 单选按钮

小旺不正经   2022-05-23 我要评论

Java程序设计 图形用户界面 【九】单选按钮

单选按钮 JRadioButton

JRadioButton类

方法 作用
public JRadioButton(Icon icon) 建立一个单选按钮,并指定图片
public JRadioButton(Icon icon,boolean selected) 建立一个单选按钮,并指定图片和其是否选定
public JRadioButton(String text) 建立一个单选按钮,并指定其文字,默认不选定
public JRadioButton(String text,boolean selected) 建立一个单选按钮,并指定文字和是否选定
public JRadioButton(String text,Icon icon,boolean selected) 建立一个单选按钮,并指定图片、文字和其是否选定
public void setSelected(boolean b) 设置是否选中
public boolean isSelected() 返回是否被选中
public void setText(String text) 设置显示文本
public void setIcon(Icon defaultIcon) 设置图片
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyRadio{
    private JFrame frame = new JFrame("一");
    private Container cont = frame.getContentPane();
    private JRadioButton jrb1 = new JRadioButton("1");
    private JRadioButton jrb2 = new JRadioButton("2");
    private JRadioButton jrb3 = new JRadioButton("3");
    private JPanel pan = new JPanel();
    public MyRadio(){
        pan.setBorder(BorderFactory.createTitledBorder("请选择"));
        pan.setLayout(new GridLayout(1,3));
        pan.add(this.jrb1);
        pan.add(this.jrb2);
        pan.add(this.jrb3);
        ButtonGroup group = new ButtonGroup();
        group.add(this.jrb1);
        group.add(this.jrb2);
        group.add(this.jrb3);
        cont.add(pan);
        this.frame.setSize(330,80);
        this.frame.setVisible(true);
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
    }
}
public class Hello {
    public static void main(String[] args) {
        new MyRadio();
    }
}

image-20220213140637190

ButtonGroup group = new ButtonGroup();
group.add(this.jrb1);
group.add(this.jrb2);
group.add(this.jrb3);

将按钮添加到同一个组中实现单选功能

JRadioButton事件处理

使用ItemListener接口进行事件的监听

方法 作用
void itemStateChanged(ItemEvent e) 当用户取消或选定某个选项时调用

ItemEvent类

方法&常量 类型 作用
public static final int SELECTED 常量 选项被选中
public static final int DESELECTED 常量 选项未被选中
public Object getItem() 方法 返回受事件影响的选项
public int getStateChange() 方法 返回选定状态的类型(已选择或已取消)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyRadio implements ItemListener{
    private JLabel a = new JLabel("选中");
    private JLabel b = new JLabel("未选中");
    private JFrame frame = new JFrame("一");
    private Container cont = frame.getContentPane();
    private JRadioButton jrb1 = new JRadioButton("A",true);
    private JRadioButton jrb2 = new JRadioButton("B",true);
    private JPanel pan = new JPanel();
    public MyRadio(){
        ButtonGroup group = new ButtonGroup();
        group.add(this.jrb1);
        group.add(this.jrb2);
        jrb1.addItemListener(this);
        jrb2.addItemListener(this);
        pan.setLayout(new GridLayout(1,4));
        pan.add(this.a);
        pan.add(this.jrb1);
        pan.add(this.b);
        pan.add(this.jrb2);
        this.frame.add(pan);
        this.frame.setSize(200,100);
        this.frame.setVisible(true);
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
    }
    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getSource()==jrb2){
            a.setText("未选中");
            b.setText("选中");
        }else {
            b.setText("未选中");
            a.setText("选中");
        }
    }
}
public class Hello {
    public static void main(String[] args) {
        new MyRadio();
    }
}

image-20220213143404542

image-20220213143420956

到此这篇关于Java 入门图形用户界面设计之单选按钮的文章就介绍到这了,更多相关Java 单选按钮内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们