我正在用Java编写Risk的克隆版,我的代码遇到了一些麻烦。当我创建一个新游戏时,我创建了一个JPanel,其中包含一个JTextField(用于玩家名称)和一个JComboBox(用于玩家颜色),一个面板供用户想要创建的每个玩家使用。该面板的实例是基于第二个JComboBox动态创建的,该第二个JComboBox允许用户从3到8中选择多个玩家。
我的问题是,当我想从输入到玩家创建面板的数据创建玩家对象时,每个玩家对象都会从最近创建的玩家创建面板中检索数据。我有一个功能性的解决方案,但我似乎无法弄清楚为什么似乎是“正确的”解决方案()将无法正常工作。
这是我现在正在使用的代码: 创建面板类:
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);
}
}
对于新游戏类,创建玩家的部分:
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;
}
}
}
现在,我所学到的是正确的方法是使我的JTextField和JComboBox成为私有,并编写访问器,如下所示:
private JComboBox playerColor;
private JTextField playerName;
//...same method as above
public static String getName(){
return playerName.getText();
}
public static int getColorIndex(){
return playerColor.getSelectedIndex();
}
并更改新游戏方法中的创建行以阅读如下内容:
Resources.players [0] = new Player (panels[0].getName(), panels[0].getColorIndex(), 1);