栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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();
    }
}

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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号