我正在尝试创建一个屏幕以更改我的游戏选项,所以我遇到一个问题,使用JRadioButton在一个案例而不是在另一个案件中工作,我不明白为什么......谁知道发生了什么?
package com.damas.screen;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class Options {
private JFrame frame;
protected com.damas.entidades.Options opcoes;
private JRadioButton radioPlayersNo;
private JRadioButton radioPlayersYes;
private JRadioButton radioSoundYes;
private JRadioButton radioSoundNo;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Options window = new Options();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Options() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
opcoes = new com.damas.entidades.Options();
frame = new JFrame();
frame.setBounds(100, 100, 280, 180);
frame.setResizable(Boolean.FALSE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
radioPlayersYes =new JRadioButton("Yes");
radioPlayersNo = new JRadioButton("No");
radioSoundYes = new JRadioButton("Yes");
radioSoundNo = new JRadioButton("No");
JLabel labelSound = new JLabel("Sound");
labelSound.setBounds(50, 99, 86, 23);
radioPlayersYes.setBounds(129, 39, 55, 14);
radioPlayersNo.setBounds(190, 30, 55, 32);
/* Setting default options*/
radioSoundYes.setSelected(opcoes.getSound());
radioPlayersYes.setSelected(opcoes.getTwoPlayers());
/* Setting labels*/
JLabel lblPlayers = new JLabel("Two Players");
lblPlayers.setBounds(50, 43, 73, 14);
JLabel lblColor = new JLabel("Color");
lblColor.setBounds(50, 74, 46, 14);
JLabel lblOptions = new JLabel("Options");
lblOptions.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 16));
lblOptions.setBounds(109, 11, 46, 14);
/* Setting radios*/
JRadioButton radioColorBlack = new JRadioButton("Black");
radioColorBlack.setBounds(115, 65, 77, 32);
JRadioButton radioColorWhite = new JRadioButton("White");
radioColorWhite.setBounds(194, 70, 61, 23);
JRadioButton radioSoundYes = new JRadioButton("Yes");
radioSoundYes.setBounds(120, 94, 55, 32);
JRadioButton radioSoundNo = new JRadioButton("No");
radioSoundNo.setBounds(194, 99, 61, 23);
addItemListeners();
frame.getContentPane().add(radioPlayersYes);
frame.getContentPane().add(radioPlayersNo);
frame.getContentPane().add(radioSoundYes);
frame.getContentPane().add(radioSoundNo);
frame.getContentPane().add(labelSound);
frame.getContentPane().add(lblPlayers);
frame.getContentPane().add(lblColor);
frame.getContentPane().add(radioColorBlack);
frame.getContentPane().add(radioColorWhite);
frame.getContentPane().add(lblOptions);
}
private void addItemListeners(){
radioPlayersYes.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == 1){
opcoes.setTwoPlayers(Boolean.TRUE);
radioPlayersNo.setSelected(Boolean.FALSE);
}else{
opcoes.setTwoPlayers(Boolean.FALSE);
radioPlayersNo.setSelected(Boolean.TRUE);
}
}
});
radioPlayersNo.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == 1){
opcoes.setTwoPlayers(Boolean.FALSE);
System.out.println(opcoes.getTwoPlayers());
radioPlayersNo.setSelected(Boolean.TRUE);
radioPlayersYes.setSelected(Boolean.FALSE);
}else{
opcoes.setTwoPlayers(Boolean.TRUE);
System.out.println(opcoes.getTwoPlayers());
radioPlayersNo.setSelected(Boolean.FALSE);
radioPlayersYes.setSelected(Boolean.TRUE);
}
}
});
radioSoundYes.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
radioSoundYes.setEnabled(Boolean.TRUE);
System.out.println("Radio Sound Yes: Enabled");
radioSoundNo.setEnabled(Boolean.FALSE);
}else{
radioSoundYes.setEnabled(Boolean.FALSE);
System.out.println("Radio Sound Yes: Disabled");
radioSoundNo.setEnabled(Boolean.TRUE);
}
}
});
radioSoundNo.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
radioSoundNo.setEnabled(Boolean.TRUE);
radioSoundYes.setEnabled(Boolean.FALSE);
}else{
radioSoundNo.setEnabled(Boolean.FALSE);
radioSoundYes.setEnabled(Boolean.TRUE);
}
}
});
}
}
答案 0 :(得分:2)
将单选按钮分组,如
ButtonGroup playOptionsButtonGroup = new ButtonGroup();
playOptionsButtonGroup.add(radioPlayersNo);
playOptionsButtonGroup.add(radioPlayersYes);
playOptionsButtonGroup.add(radioSoundYes);
playOptionsButtonGroup.add(radioSoundNo);