Java 为什么我的按钮组无法工作并显示?
我正在学习GUI,在这个程序中使用了EclipseIDE(拖放式windows builder) 我声明了两个性别单选按钮:男和女 如果它点击男性或女性按钮,它应该只(点击一个)和去该按钮,而不是两者,因此我希望它自动另一个被取消选择,以避免重复选择 所以我已经做了一些研究,Java 为什么我的按钮组无法工作并显示?,java,swing,user-interface,radio-button,buttongroup,Java,Swing,User Interface,Radio Button,Buttongroup,我正在学习GUI,在这个程序中使用了EclipseIDE(拖放式windows builder) 我声明了两个性别单选按钮:男和女 如果它点击男性或女性按钮,它应该只(点击一个)和去该按钮,而不是两者,因此我希望它自动另一个被取消选择,以避免重复选择 所以我已经做了一些研究,import javax.swing.ButtonGroup但我仍然不明白为什么它不起作用,因为我仍然可以单击这两个单选按钮。我想这和我的面板有关 为什么会发生这种情况?我如何修复它 这是我的节目 你的代码甚至没有编译 移动
import javax.swing.ButtonGroup代码>但我仍然不明白为什么它不起作用,因为我仍然可以单击这两个单选按钮。我想这和我的面板有关
为什么会发生这种情况?我如何修复它
这是我的节目
你的代码甚至没有编译
移动线路
ButtonGroup btnBg = new ButtonGroup();
btnBg.add(rdbtnMale);
btnBg.add(rdbtnFemale);
直到代码的末尾
像这样
setUndecorated(true);
setBackground(Color.WHITE);
setVisible(true);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100, 100, 1063, 640);
JPanel contentPane = new JPanel();
contentPane.setBounds(new Rectangle(5, 0, 0, 0));
contentPane.setBackground(new Color(255, 255, 240));
contentPane.setBorder(new LineBorder(new Color(0, 0, 28), 1, true));
setLocationRelativeTo(null);
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel pnlInfo = new JPanel();
pnlInfo.setBounds(24, 20, 1015, 597);
contentPane.add(pnlInfo);
pnlInfo.setLayout(null);
JLabel lblGender = new JLabel("Gender:");
lblGender.setBounds(32, 174, 50, 10);
pnlInfo.add(lblGender);
lblGender.setFont(new Font("Tahoma", Font.PLAIN, 11));
JRadioButton rdbtnMale = new JRadioButton("Male");
rdbtnMale.setBounds(79, 169, 55, 21);
pnlInfo.add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("Female");
rdbtnFemale.setBounds(136, 169, 76, 21);
pnlInfo.add(rdbtnFemale);
ButtonGroup btnBg = new ButtonGroup();
btnBg.add(rdbtnMale);
btnBg.add(rdbtnFemale);
1) 为了更快地获得更好的帮助,请添加或。(而不是一个不可编译的代码片段——可能甚至不包含错误。)2)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。相反,使用布局管理器,或者与.BTW的布局填充和边框一起使用-删除每个静态声明。这通常会增加而不是解决问题。
setUndecorated(true);
setBackground(Color.WHITE);
setVisible(true);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100, 100, 1063, 640);
JPanel contentPane = new JPanel();
contentPane.setBounds(new Rectangle(5, 0, 0, 0));
contentPane.setBackground(new Color(255, 255, 240));
contentPane.setBorder(new LineBorder(new Color(0, 0, 28), 1, true));
setLocationRelativeTo(null);
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel pnlInfo = new JPanel();
pnlInfo.setBounds(24, 20, 1015, 597);
contentPane.add(pnlInfo);
pnlInfo.setLayout(null);
JLabel lblGender = new JLabel("Gender:");
lblGender.setBounds(32, 174, 50, 10);
pnlInfo.add(lblGender);
lblGender.setFont(new Font("Tahoma", Font.PLAIN, 11));
JRadioButton rdbtnMale = new JRadioButton("Male");
rdbtnMale.setBounds(79, 169, 55, 21);
pnlInfo.add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("Female");
rdbtnFemale.setBounds(136, 169, 76, 21);
pnlInfo.add(rdbtnFemale);
ButtonGroup btnBg = new ButtonGroup();
btnBg.add(rdbtnMale);
btnBg.add(rdbtnFemale);