java swing如何获取单选框的值

2024-12-23 05:12:32
推荐回答(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("click here");
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:

能描述清楚点吗?你说的单选框是什么额?JRadioButton??还是什么
如果输JRadioButton的话就 就直接 用isSelected()就好了额!