Frage

I am having a little bit of trouble trying to get my JRadio Buttons to change the color of the text depending on what the player chooses. I am just making the GUI for the game setup page. Something like, choose your army, followed by choose the number of players you want to interact with.

Here is what I have:

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);
      }

    }

  }

}

The background was Foreground but alas, neither have worked. What is it that I am doing wrong?

War es hilfreich?

Lösung

In itemStateChanged() you should compare the source of the event and not the event itself. For example:

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

You can use ActionListener on radio buttons. See How to Use Radio Buttons for an example.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top