我正在为考试开发一个基于文本的简单角色扮演游戏,但我的 GUI 遇到了一些问题。
玩家注册时,可以消耗3类属性点。如果玩家有任何属性点,GUI 会显示“提高强度”等按钮。
这很酷,但是当玩家点击加注按钮时,会为他获取一个属性点,问题是,GUI 似乎没有更新。
if(Controller.player.getAttributePoints() > 0) {
JLabel attriL = new JLabel("You have " + Controller.player.getAttributePoints() + " unspent Attribute points.");
attriL.setBounds(110, 30, 250, 30);
hPanel.add(attriL);
JButton setStrB = new JButton("Raise Strength");
setStrB.setBounds(125, 60, 200, 30);
setStrB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// tabbedPane.removeAll();
Controller.player.setAttributePoints(Controller.player.getAttributePoints()-1);
Controller.player.setStrength(Controller.player.getStrength()+1);
gameCtn.validate();
gameCtn.repaint();
System.out.println(Controller.player.getStrength());
}
});
hPanel.add(setStrB);
}
正如你所看到的,我尝试在我的容器上使用重绘和验证,但没有运气,我也尝试过框架和面板,似乎没有任何效果? 我做错了什么吗?
谢谢
最佳答案
不清楚 Controller ...是否没有调用长时间且困难的任务,基本上(如果您删除然后添加新的JComponents
)到GUI,那么您必须调用
revalidate();
repaint();// not required on all cases
简单演示what's happens, what's possible或大部分完成here
关于java - GUI不会刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8463511/