我有四个面板是从单个面板“模具”动态创建的,每个面板都有从数组设置的不同信息。问题是,当信息更新时,只有集合中的最后一个面板发生了变化。
示例:有一个 MovesLeft 标签,清楚地显示了剩余的移动次数。随着数字的下降,为该玩家列出的数字应该会发生变化,每个玩家都有自己的面板。目前只有最后一个面板的 Moveleft 标签会更改,并且对于移动的任何玩家都不会更改,而不仅仅是最后一个玩家。因此,如果玩家 1 用尽了所有动作,它将在玩家 4 的面板上显示 0,而不是在玩家 1 的面板上。
问题:有没有办法让我指定要更改标签的四个实例中的哪一个,以便更新正确的玩家信息?
面板设置
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(false);
//panel.add(filler);
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
name = new JLabel(minirpg.MiniRPG.players.get(setupPlayerIndex).getName());
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
panel.add(name, c);
icon = new JLabel(MiniRPG.players.get(setupPlayerIndex).getIcon());
c.gridx = 1;
c.gridy = 2;
panel.add(icon, c);
lblClass = new JLabel("Class:");
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
panel.add(lblClass, c);
pClass = new JLabel(MiniRPG.players.get(setupPlayerIndex).getclass());
c.gridx = 2;
c.gridy = 3;
panel.add(pClass, c);
lblRole = new JLabel("Role:");
c.gridx = 1;
c.gridy = 4;
panel.add(lblRole, c);
role = new JLabel(MiniRPG.players.get(setupPlayerIndex).getRole());
c.gridx = 2;
c.gridy = 4;
panel.add(role, c);
dash = new JLabel("--------------------STATS--------------------");
c.gridwidth = 4;
c.gridx = 1;
c.gridy = 5;
panel.add(dash, c);
lblStr = new JLabel("Strengh:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 6;
panel.add(lblStr, c);
str = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getStr()));
//c.insets = new Insets(5, 5, 5, 5);
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 6;
panel.add(str, c);
lblDex = new JLabel("Dexterity:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 7;
panel.add(lblDex, c);
dex = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getDex()));
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 7;
panel.add(dex, c);
lblEnd = new JLabel("Endurance:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 8;
panel.add(lblEnd, c);
end = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getEnd()));
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 8;
panel.add(end, c);
lblDex = new JLabel("Wisdom:");
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 9;
panel.add(lblDex, c);
wiz = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getWis()));
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 9;
panel.add(wiz, c);
lblHp = new JLabel("HP: ");
c.gridx = 1;
c.gridy = 10;
panel.add(lblHp, c);
health = new JLabel(Integer.toString(
minirpg.MiniRPG.players.get(setupPlayerIndex).getHp()));
c.gridx = 2;
c.gridy = 10;
panel.add(health, c);
lblMoves = new JLabel("Moves Left: ");
c.gridx = 1;
c.gridy = 11;
panel.add(lblMoves, c);
movesLeft = new JLabel(Integer.toString(
MiniRPG.players.get(setupPlayerIndex).getMoves()));
c.gridx = 2;
c.gridy = 11;
panel.add(movesLeft, c);
dash = new JLabel("--------------------SKILLS--------------------");
c.gridwidth = 4;
c.gridx = 1;
c.gridy = 12;
panel.add(dash, c);
lblSkill1 = new JLabel("Skill 1:");
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 13;
panel.add(lblSkill1, c);
skill1 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill1());
c.gridx = 2;
c.gridy = 13;
panel.add(skill1, c);
lblSkill2 = new JLabel("Skill 2:");
c.gridx = 1;
c.gridy = 14;
panel.add(lblSkill2, c);
skill2 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill2());
c.gridx = 2;
c.gridy = 14;
panel.add(skill2, c);
skill2.setEnabled(false);
lblSkill3 = new JLabel("Skill 3:");
c.gridx = 1;
c.gridy = 15;
panel.add(lblSkill3, c);
skill3 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill3());
c.gridx = 2;
c.gridy = 15;
panel.add(skill3, c);
skill3.setEnabled(false);
lblSkill4 = new JLabel("Skill 4:");
c.gridx = 1;
c.gridy = 16;
panel.add(lblSkill4, c);
skill4 = new JButton(MiniRPG.players.get(setupPlayerIndex).getSkill4());
c.gridx = 2;
c.gridy = 16;
panel.add(skill4, c);
skill4.setEnabled(false);
skill1.addActionListener(e);
skill2.addActionListener(e);
skill3.addActionListener(e);
skill4.addActionListener(e);
return panel;
}
如何将面板添加到 GUI
final JTabbedPane characterInfoPane = new JTabbedPane();
while (setupPlayerIndex < 4) {
JComponent panel = makeTextPanel("");
characterInfoPane.addTab(minirpg.MiniRPG.players.get(setupPlayerIndex).getName(), null, panel,
"");
setupPlayerIndex++;
}
我目前如何尝试更改 x 是所选播放器的相关标签
movesLeft.setText(Integer.toString(MiniRPG.players.get(x).getMoves()));
请让我知道是否需要添加代码来回答是否可以做我正在寻找的问题。