gpt4 book ai didi

java - 为什么我的 JRadioButton 在这种情况下不起作用?

转载 作者:行者123 更新时间:2023-11-30 08:37:44 26 4
gpt4 key购买 nike

我正在尝试创建一个屏幕来更改我的游戏选项,所以我遇到了一个问题,即使用 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);
}

}

});

}


}

最佳答案

像这样对单选按钮进行分组

ButtonGroup playOptionsButtonGroup = new ButtonGroup();
playOptionsButtonGroup.add(radioPlayersNo);
playOptionsButtonGroup.add(radioPlayersYes);
playOptionsButtonGroup.add(radioSoundYes);
playOptionsButtonGroup.add(radioSoundNo);

关于java - 为什么我的 JRadioButton 在这种情况下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36983804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com