我有一个 JFrame“GameWindow”和一个名为“Combat”的类。
我正在尝试从 Combat 类中的变量更新 GameWindow 中的各种组件(JLabels、JProgressBars 等)。但是,细节似乎永远不会更新。考虑以下:
public class Combat {
public static String attackName1;
public static String pUnitName;
public static void setPlayerUnit(GameUnit u) {
attackName1 = u.getAttackName1();
pUnitName = u.getName().toUpperCase();
}
}
和:
public GameWindow() {
initialize();
gameFrame.setVisible(true);
}
private void initialize() {
gameFrame = new JFrame();
gameFrame.setResizable(false);
gameFrame.setTitle("GameWindow");
gameFrame.setBounds(100, 100, 800, 480);
gameFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
JButton pAttack1 = new JButton(Combat.attackName1); //<--------NOT BEING CHANGED
sl_gamePanel.putConstraint(SpringLayout.NORTH, pAttack1, 0, SpringLayout.NORTH, gameTextPanel);
sl_gamePanel.putConstraint(SpringLayout.WEST, pAttack1, 6, SpringLayout.EAST, textAttackSeparator);
sl_gamePanel.putConstraint(SpringLayout.SOUTH, pAttack1, 34, SpringLayout.NORTH, gameTextPanel);
sl_gamePanel.putConstraint(SpringLayout.EAST, pAttack1, 157, SpringLayout.EAST, textAttackSeparator);
gamePanel.add(pAttack1);
JLabel pMobName = new JLabel(Combat.pUnitName); //<--------NOT BEING CHANGED
sl_pPanel.putConstraint(SpringLayout.NORTH, pMobName, 10, SpringLayout.NORTH, pPanel);
sl_pPanel.putConstraint(SpringLayout.WEST, pMobName, 129, SpringLayout.WEST, pPanel);
pPanel.add(pMobName);
为什么按钮/JLabels 的文本没有更新?我需要使用“Combat.variable”更改为“Combat.getVariable()”吗?这甚至可能吗,如果是这样,我怎样才能使它工作?