1

I'm writing a clone of Risk in Java, and am having some trouble with my code. When I create a new game, I create a JPanel with a JTextField (for the player name) and a JComboBox(for the player color), one panel for each player the user wants to create. Instances of this panel are created dynamically based on a second JComboBox which lets the user select a number of players from three to eight.

My problem is that when I want to create the player objects from the data entered into the player creation panel, each player object retrieves the data from the most-recently-created player creation panel. I have a functional solution, but I can't seem to figure out why what seems to be the 'proper' solution () won't work.

This is the code that I have working right now: Creation panel class:

package risk;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PlayerCreatePanel extends JPanel{

public static ImageIcon[] playerColors = {Resources.red,Resources.green,Resources.blue,Resources.cyan,Resources.magenta,Resources.yellow,Resources.orange,Resources.gray};

public JComboBox playerColor;

public JTextField playerName;

public PlayerCreatePanel(int index){

    this.setPreferredSize(new Dimension(360, 30));

    JLabel numberLabel = new JLabel ("Player " + index + ":  ");
    JLabel nameLabel = new JLabel("Name: ");
    JLabel colorLabel = new JLabel("  Color: ");

    playerName = new JTextField("");
    playerName.setColumns(13);

    playerColor = new JComboBox(playerColors);

    this.add(numberLabel);
    this.add(nameLabel);
    this.add(playerName);
    this.add(colorLabel);
    this.add(playerColor);
}
}

And for the new game class, the part that creates players:

package risk;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;

public class NewGame {

private static PlayerCreatePanel [] panels = {  new PlayerCreatePanel(1), new PlayerCreatePanel(2), 
                                                new PlayerCreatePanel(3), null, null, null, null, null};

private static int playerCount = 3;

public static void createPlayers(){
    Resources.players = new Player[playerCount];
    switch(playerCount){
    case 8: Resources.players[7] = new Player (panels[7].playerName.getText(), Resources.colors[panels[7].playerColor.getSelectedIndex()], 8);
    case 7: Resources.players[6] = new Player (panels[6].playerName.getText(), Resources.colors[panels[6].playerColor.getSelectedIndex()], 7);
    case 6: Resources.players[5] = new Player (panels[5].playerName.getText(), Resources.colors[panels[5].playerColor.getSelectedIndex()], 6);
    case 5: Resources.players[4] = new Player (panels[4].playerName.getText(), Resources.colors[panels[4].playerColor.getSelectedIndex()], 5);
    case 4: Resources.players[3] = new Player (panels[3].playerName.getText(), Resources.colors[panels[3].playerColor.getSelectedIndex()], 4);
    case 3: Resources.players[2] = new Player (panels[2].playerName.getText(), Resources.colors[panels[2].playerColor.getSelectedIndex()], 3);
            Resources.players[1] = new Player (panels[1].playerName.getText(), Resources.colors[panels[1].playerColor.getSelectedIndex()], 2);
            Resources.players[0] = new Player (panels[0].playerName.getText(), Resources.colors[panels[0].playerColor.getSelectedIndex()], 1); break;
    default: break;     
    }
}
}

Now, what I've learned is the proper way to do this is to make my JTextField and JComboBox private, and write accessors, like so:

private JComboBox playerColor;
private JTextField playerName;

//...same method as above

public static String getName(){
    return playerName.getText();
}

public static int getColorIndex(){
    return playerColor.getSelectedIndex();
}

and change the creation lines in the new game method to read something like this:

Resources.players [0] = new Player (panels[0].getName(), panels[0].getColorIndex(), 1);
4

0 回答 0