Java 入门图形用户界面设计之单选按钮
作者:小旺不正经 时间:2023-08-29 13:40:02
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();
}
}
来源:https://blog.csdn.net/weixin_42403632/article/details/122908840
标签:Java,单选按钮,图形用户界面
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android保存联系人到通讯录的方法
2023-01-28 16:44:40
emoji表情与unicode编码互转的实现(JS,JAVA,C#)
2023-02-07 06:07:48
Android实现极简打开摄像头
2022-09-10 15:56:51
![](https://img.aspxhome.com/file/2023/2/96802_0s.jpg)
使用RecyclerView实现点赞头像叠加效果
2022-12-24 12:10:37
![](https://img.aspxhome.com/file/2023/8/139448_0s.gif)
C#自定义控件实现TextBox禁止粘贴的方法
2023-08-03 06:00:22
Java利用endorsed如何覆盖jdk提供的类详解
2021-05-30 08:26:33
![](https://img.aspxhome.com/file/2023/6/129136_0s.png)
Android本地实现搜索历史记录
2023-08-15 22:25:17
![](https://img.aspxhome.com/file/2023/3/99803_0s.jpg)
C# readnodefile()不能读取带有文件名为汉字的osg文件解决方法
2021-11-08 07:41:00
C#中调用命令行cmd开启wifi热点的实例代码
2023-11-28 03:15:04
![](https://img.aspxhome.com/file/2023/0/96350_0s.jpg)
Java反射机制详解_动力节点Java学院整理
2022-08-19 18:10:13
Java中BufferedReader与BufferedWriter类的使用示例
2022-10-30 01:00:04
java实现学生选课系统
2022-11-19 14:58:24
Java Structs框架原理案例详解
2023-12-11 16:46:22
![](https://img.aspxhome.com/file/2023/4/81224_0s.jpg)
IDEA java出现无效的源发行版14解决方案
2021-06-25 08:50:33
![](https://img.aspxhome.com/file/2023/2/62172_0s.png)
Android中一种巧妙的drawable.xml替代方案分享
2021-12-06 04:18:35
![](https://img.aspxhome.com/file/2023/9/138829_0s.png)
Android开发人脸识别登录功能
2022-03-02 14:16:12
![](https://img.aspxhome.com/file/2023/7/86757_0s.png)
详解Android Lint的原理及其使用
2022-12-23 16:05:47
![](https://img.aspxhome.com/file/2023/4/137484_0s.jpg)
Android三种网络通讯方式及Android的网络通讯机制
2021-08-20 12:54:31
![](https://img.aspxhome.com/file/2023/2/139472_0s.png)
详解Asp.Net MVC的Bundle捆绑
2021-09-10 05:21:38
![](https://img.aspxhome.com/file/2023/2/96322_0s.png)
详解JS与APP原生控件交互
2022-11-21 21:28:15
![](https://img.aspxhome.com/file/2023/3/108303_0s.png)