软件编程
位置:首页>> 软件编程>> java编程>> Java 入门图形用户界面设计之单选按钮

Java 入门图形用户界面设计之单选按钮

作者:小旺不正经  发布时间:2023-08-29 13:40:02 

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

Java 入门图形用户界面设计之单选按钮


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

Java 入门图形用户界面设计之单选按钮

Java 入门图形用户界面设计之单选按钮

来源:https://blog.csdn.net/weixin_42403632/article/details/122908840

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com