我在尝试让我的JRadio按钮根据玩家选择的内容更改文本颜色方面遇到了一些麻烦。我只是为游戏设置页面制作GUI。比如,选择你的军队,然后选择你想与之互动的球员数量。
这就是我所拥有的:
package SystemandDesign.RISK;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.ButtonGroup;
public class CharacterCreation extends JFrame{
private Font redFont;
private Font blueFont;
private Font greenFont;
private Font yellowFont;
private Font grayFont;
private Font blackFont;
private JRadioButton three;
private JRadioButton four;
private JRadioButton five;
private JRadioButton six;
private JRadioButton red;
private JRadioButton blue;
private JRadioButton green;
private JRadioButton yellow;
private JRadioButton gray;
private JRadioButton black;
private JTextField textField;
private ButtonGroup army;
private ButtonGroup numPlayers;
public CharacterCreation(){
super("Character Creation");
setLayout(new FlowLayout());
textField = new JTextField("Your Army Color", 25);
add(textField);
//To remain true to the Game of Risk, instead of having individual name input for the game.
//I am allowing which army color the player wants to be.
//When the game is done being written out, I hope to have the font of individual player text, to be the color
//of their army.
//In the future when the model is done, the character screen will prevent players from having the same color army.
redFont = new Font("Serif", Font.PLAIN, 14);
blueFont = new Font("Serif", Font.PLAIN,14);
greenFont = new Font("Serif", Font.PLAIN,14);
yellowFont = new Font("Serif", Font.PLAIN,14);
grayFont = new Font("Serif", Font.PLAIN,14);
blackFont = new Font("Serif", Font.PLAIN,14);
textField.setFont(redFont);
red = new JRadioButton("Red Army", true);
blue = new JRadioButton("Blue Army", false);
green = new JRadioButton("Green Army", false);
yellow = new JRadioButton("Yellow Army", false);
gray = new JRadioButton("Gray Army", false);
black = new JRadioButton("Black Army", false);
add(red);
add(blue);
add(green);
add(yellow);
add(gray);
add(black);
army = new ButtonGroup();
army.add(red);
army.add(blue);
army.add(green);
army.add(yellow);
army.add(gray);
army.add(black);
red.addItemListener(new RadioButtonHandler(redFont));
blue.addItemListener(new RadioButtonHandler(blueFont));
green.addItemListener(new RadioButtonHandler(greenFont));
yellow.addItemListener(new RadioButtonHandler(yellowFont));
gray.addItemListener(new RadioButtonHandler(grayFont));
black.addItemListener(new RadioButtonHandler(blackFont));
//End of the Army selection RadioButtons.
//Now for the Player selection RadioButtons.
three = new JRadioButton("3 players", true);
four = new JRadioButton("4 players", false);
five = new JRadioButton("5 players", false);
six = new JRadioButton("6 players", false);
add(three);
add(four);
add(five);
add(six);
numPlayers = new ButtonGroup();
numPlayers.add(three);
numPlayers.add(four);
numPlayers.add(five);
numPlayers.add(six);
}
private class RadioButtonHandler implements ItemListener{
private Font font;
public RadioButtonHandler(Font f){
font = f;
}
public void itemStateChanged(ItemEvent event){
textField.setFont(font);
if(event.equals(red)){
textField.setBackGround(Color.RED);
}
else if(event.equals(blue)){
textField.setBackground(Color.BLUE);
}
else if(event.equals(green)){
textField.setBackground(Color.GREEN);
}
else if(event.equals(yellow)){
textField.setBackground(Color.YELLOW);
}
else if(event.equals(gray)){
textField.setBackground(Color.GRAY);
}
else if(event.equals(black)){
textField.setBackground(Color.BLACK);
}
}
}
}
背景是前景,但唉,两者都没有奏效。我做错了什么?
答案 0 :(得分:1)
在itemStateChanged()
中,您应该比较事件的来源而不是事件本身。例如:
if(event.getSource().equals(red)){
textField.setBackground(Color.RED);
}
您可以在单选按钮上使用ActionListener
。有关示例,请参阅How to Use Radio Buttons。