java中JRadioButton清除选择的小程序!求帮助
想要的效果是,选中"男"或"女"后,点"清空",清空JRadioButton让JRadioButton的状态是false我写的这个,不加ButtonGroup的时候,点"...
想要的效果是,选中"男"或"女"后,点"清空",清空JRadioButton
让JRadioButton的状态是false
我写的这个,不加ButtonGroup 的时候,点"清空"可是清空!
加了ButtonGroup 以后就不行了!
现在怎么写啊?请高手指点指点!
帮忙改一下程序!帮忙解决给追分!!!
import java.awt.event.*;
import javax.swing.*;
public class Class4 extends JFrame {
JPanel jPanel = new JPanel();
JRadioButton jrb1 = new JRadioButton();
JRadioButton jrb2 = new JRadioButton();
JButton jButton1 = new JButton();
ButtonGroup bg = new ButtonGroup();
public Class4() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Class4 class4 = new Class4();
class4.setSize(300,100);
class4.setVisible(true);
class4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void jbInit() throws Exception {
jrb1.setText("男");
jrb2.setText("女");
jButton1.setText("清空");
jPanel.add(jrb1, null);
jPanel.add(jrb2, null);
jPanel.add(jButton1, null);
this.getContentPane().add(jPanel, null);
bg.add(jrb1);
bg.add(jrb2);
this.jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jrb1.setSelected(false);
jrb2.setSelected(false);
}
});
}
} 展开
让JRadioButton的状态是false
我写的这个,不加ButtonGroup 的时候,点"清空"可是清空!
加了ButtonGroup 以后就不行了!
现在怎么写啊?请高手指点指点!
帮忙改一下程序!帮忙解决给追分!!!
import java.awt.event.*;
import javax.swing.*;
public class Class4 extends JFrame {
JPanel jPanel = new JPanel();
JRadioButton jrb1 = new JRadioButton();
JRadioButton jrb2 = new JRadioButton();
JButton jButton1 = new JButton();
ButtonGroup bg = new ButtonGroup();
public Class4() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Class4 class4 = new Class4();
class4.setSize(300,100);
class4.setVisible(true);
class4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void jbInit() throws Exception {
jrb1.setText("男");
jrb2.setText("女");
jButton1.setText("清空");
jPanel.add(jrb1, null);
jPanel.add(jrb2, null);
jPanel.add(jButton1, null);
this.getContentPane().add(jPanel, null);
bg.add(jrb1);
bg.add(jrb2);
this.jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jrb1.setSelected(false);
jrb2.setSelected(false);
}
});
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询