英文:
Cant change content on a JPanel
问题
我有一个JFrame,其中添加了一个名为gamePanel
的JPanel。在主窗口设置为可见后,游戏多次调用renderPlayerInfo
方法。然而,在第一次调用该方法后,面板没有改变。
renderPlayerInfo
void renderPlayerInfo(JPanel gamePanel) {
Font renderFontBold = new Font("Arial", Font.BOLD, 16);
Font renderFont = new Font("Arial", Font.PLAIN, 16);
renderPanel.removeAll();
System.out.println("e");
int yRender = 0;
for (int i = 0; i < players.size(); i++) {
JPanel playerPanel = new JPanel(null);
playerPanel.setBounds(0, yRender, 300, 110);
playerPanel.setBackground(new Color(204, 227, 199));
JLabel playerLabel = new JLabel("Player " + String.valueOf(i + 1));
playerLabel.setFont(renderFontBold);
playerLabel.setBounds(10, 10, 300, 18);
JLabel playerToken = players.get(i).getPlayerToken(244, 0);
playerPanel.add(playerToken);
JLabel moneyIcon = new JLabel(new ImageIcon("./res/money.jpg"));
moneyIcon.setBounds(30, 40, 64, 32);
playerPanel.add(moneyIcon);
JLabel moneyAmount = new JLabel("£" + moneyFormatter.format(players.get(i).playerMoney));
moneyAmount.setFont(renderFont);
moneyAmount.setBounds(100, 48, 200, 18);
playerPanel.add(moneyAmount);
JPanel separatorPanel = new JPanel();
separatorPanel.setBounds(0, 108, 300, 2);
separatorPanel.setBackground(new Color(146, 159, 149));
playerPanel.add(separatorPanel);
playerPanel.add(playerLabel);
renderPanel.add(playerPanel);
if (i + 1 == playerTurn) {
JLabel turnPointer = new JLabel(new ImageIcon("./res/turnPointer.png"));
turnPointer.setBounds(70, -12, 64, 64);
playerPanel.add(turnPointer);
}
yRender += 110;
}
renderPanel.repaint();
gamePanel.add(renderPanel);
gamePanel.repaint();
}
我尝试添加了renderPanel.repaint()
,但似乎没有改变任何内容。
英文:
I have a JFrame, which has a JPanel called gamePanel
added to it. The game calls the method renderPlayerInfo
multiple times after the main frame is set to visible. However, the panel does not change after the first time I call the method.
renderPlayerInfo
void renderPlayerInfo(JPanel gamePanel) {
Font renderFontBold = new Font("Arial", Font.BOLD, 16);
Font renderFont = new Font("Arial", Font.PLAIN, 16);
renderPanel.removeAll();
System.out.println("e");
int yRender = 0;
for (int i = 0; i < players.size(); i++) {
JPanel playerPanel = new JPanel(null);
playerPanel.setBounds(0, yRender, 300, 110);
playerPanel.setBackground(new Color(204, 227, 199));
JLabel playerLabel = new JLabel("Player " + String.valueOf(i + 1));
playerLabel.setFont(renderFontBold);
playerLabel.setBounds(10, 10, 300, 18);
JLabel playerToken = players.get(i).getPlayerToken(244, 0);
playerPanel.add(playerToken);
JLabel moneyIcon = new JLabel(new ImageIcon("./res/money.jpg"));
moneyIcon.setBounds(30, 40, 64, 32);
playerPanel.add(moneyIcon);
JLabel moneyAmount = new JLabel("£" + moneyFormatter.format(players.get(i).playerMoney));
moneyAmount.setFont(renderFont);
moneyAmount.setBounds(100, 48, 200, 18);
playerPanel.add(moneyAmount);
JPanel separatorPanel = new JPanel();
separatorPanel.setBounds(0, 108, 300, 2);
separatorPanel.setBackground(new Color(146, 159, 149));
playerPanel.add(separatorPanel);
playerPanel.add(playerLabel);
renderPanel.add(playerPanel);
if (i + 1 == playerTurn) {
JLabel turnPointer = new JLabel(new ImageIcon("./res/turnPointer.png"));
turnPointer.setBounds(70, -12, 64, 64);
playerPanel.add(turnPointer);
}
yRender += 110;
}
renderPanel.repaint();
gamePanel.add(renderPanel);
gamePanel.repaint();
}
I tried adding renderPanel.repaint()
but this does not seem to change anything.
答案1
得分: 2
"不能在 JPanel 上更改内容
可以。
但一般来说,人们不会以你尝试的方式实现这一点。而是会更新现有组件的模型,以便在重绘时视图会按照你的要求显示。例如,使用 JLabel.setText()
而不是创建一个全新的 JLabel
。当然,这需要你以一种能够清晰地进行修改的方式保持对要更新的组件的引用。在某些情况下,创建自定义组件来帮助你可能是有意义的。
另外,从你调用 renderPlayerPanel()
的上下文来看,如果它修改了 GUI 组件,那么它需要在事件分发线程上运行。如果它尚未在那里运行,你可以通过让调用者通过 SwingUtilities.invokeAndWait()
或 SwingUtilities.invokeLater()
来运行它来修复这个问题。
我尝试添加 renderPanel.repaint()
,但似乎没有任何变化。
这并不奇怪。你已经在重绘该面板的容器;直接重绘面板没有额外的效果。
英文:
> Cant change content on a JPanel
It can.
But generally speaking, one would not achieve that in the way you are trying to do. Instead of ripping out all of the components and replacing them with new, one would ordinarily update the models of the existing components so that when they are repainted, the view will be as you want. For example, use JLabel.setText()
instead of creating a whole new JLabel
. Of course, this does require you to keep references to the components you will want to update in a way that enables you to perform such modifications cleanly. Under some circumstances, it might make sense to create custom components to help you with that.
Also, however, it is unclear from what context you are calling renderPlayerPanel()
, but inasmuch as it modifies GUI components, it needs to run on the event dispatch thread. If it's not running there already, then you can fix that by having the invoker(s) go through SwingUtilities.invokeAndWait()
or SwingUtilities.invokeLater()
to run it.
> I tried adding renderPanel.repaint() but this does not seem to change anything.
Not surprising. You're already repainting that panel's container; it has no additional effect to also repaint the panel directly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论