java swing 中怎样获取单选框的值?

java swing 中怎样获取单选框的值?
2024-11-25 17:25:27
推荐回答(2个)
回答1:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class MyTest extends JFrame{
private JRadioButton jrb;
public MyTest(){
jrb = new JRadioButton("点我");
this.setLayout(new FlowLayout());
this.add(jrb);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public boolean getJRadioButtonValue(){
return jrb.isSelected();
}
public static void main(String[] args){
MyTest my = new MyTest();
my.jrb.setSelected(true);
System.out.println(my.getJRadioButtonValue());
my.jrb.setSelected(false);
System.out.println(my.getJRadioButtonValue());
}

}

回答2:

对单选框的实例调用使用getXXX()方法