gpt4 book ai didi

java - 单选按钮和字体更改

转载 作者:行者123 更新时间:2023-12-01 13:18:12 25 4
gpt4 key购买 nike

我在尝试让 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);
}

}

}

}

背景是前景,但可惜,两者都不起作用。我到底做错了什么?

最佳答案

itemStateChanged() 中,您应该比较事件源而不是事件本身。例如:

  if(event.getSource().equals(red)){
textField.setBackground(Color.RED);
}

您可以在单选按钮上使用ActionListener。请参阅How to Use Radio Buttons举个例子。

关于java - 单选按钮和字体更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22291887/

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