为啥我的 JRadioButton 在这种情况下不起作用?
Posted
技术标签:
【中文标题】为啥我的 JRadioButton 在这种情况下不起作用?【英文标题】:Why does my JRadioButton not work in this case?为什么我的 JRadioButton 在这种情况下不起作用? 【发布时间】:2016-08-27 07:10:08 【问题描述】:我正在尝试创建一个屏幕来更改我的游戏选项,所以我遇到了一个问题,即使用 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 并查看this 【参考方案1】:将单选按钮分组,如
ButtonGroup playOptionsButtonGroup = new ButtonGroup();
playOptionsButtonGroup.add(radioPlayersNo);
playOptionsButtonGroup.add(radioPlayersYes);
playOptionsButtonGroup.add(radioSoundYes);
playOptionsButtonGroup.add(radioSoundNo);
【讨论】:
以上是关于为啥我的 JRadioButton 在这种情况下不起作用?的主要内容,如果未能解决你的问题,请参考以下文章