【问题标题】:JRadio Buttons and Font changeJRadio 按钮和字体更改
【发布时间】:2014-04-13 01:37:12
【问题描述】:

我在尝试让我的 JRadio Buttons 根据玩家选择的内容更改文本颜色时遇到了一些麻烦。我只是为游戏设置页面制作 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);
      }

    }

  }

}

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

【问题讨论】:

    标签: java swing fonts colors jradiobutton


    【解决方案1】:

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

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

    您可以在单选按钮上使用ActionListener。示例见How to Use Radio Buttons

    【讨论】:

    • 哦,我明白你在说什么。因此,即使它正在读取事件已经发生,它也不会说“嘿,伙计改变颜色”。还是我使用了错误的类比?
    • @user3255993 在你的itemStateChanged 中没有一个if 条件永远是true,所以背景永远不会改变。
    • 现在开始制作加载页面的 GUI 和主游戏页面 wooo - 将植物放到桌面上 - 哦,顺便说一句,假设我有多个 GUI(已经有一个退出按钮工作)如何我可以将主页和角色创建 GUI 连接在一起吗?就像点击一个按钮一样。
    猜你喜欢
    • 2012-12-20
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2014-06-14
    • 1970-01-01
    相关资源
    最近更新 更多