使用容器在 Java Swing 中重新绘制
我有一个简单的网格,在用户点击的地方放置一个正方形。网格和对窗格的访问保存在“游戏”对象中。
这有效:
private void buildClicked(int x, int y) {
panel.repaint();
game.buy(x, y);
}
这不会触发重绘:
private void buildClicked(int x, int y) {
game.getPanel().repaint();
game.buy(x, y);
}
如果我将面板设为游戏的公共变量,这也不起作用:
private void buildClicked(int x, int y) {
game.panel.repaint();
game.buy(x, y);
}
“getPanel”仅返回顶部“面板”对象所引用的相同自定义面板对象。
我想将面板包含在游戏对象包装器中。同样,在“buy”函数中调用重绘函数也不起作用。
为什么“重绘”函数在上面的示例中表现不同?
I have a simple grid that places a square wherever the user clicks. The grid and access to the panes are held in a "Game" object.
This works:
private void buildClicked(int x, int y) {
panel.repaint();
game.buy(x, y);
}
This does not trigger a repaint:
private void buildClicked(int x, int y) {
game.getPanel().repaint();
game.buy(x, y);
}
If I make the panel a public variable of Game, this doesn't work either:
private void buildClicked(int x, int y) {
game.panel.repaint();
game.buy(x, y);
}
"getPanel" simply returns the same custom panel object that the top's "panel" object is referring to.
I would like to contain the panel in the Game object wrapper. Similarly, calling the repaint function inside the "buy" function doesn't work.
Why does the "repaint" function behave differently in the above examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的问题下的评论完全正确。我一直在游戏对象和窗口本身中单独声明面板。另一个例子是陷入新材料而错过了一些基本的东西。谢谢你!
The comments under my question were exactly right. I had been declaring the panel separately in the Game object and the window itself. Another case of being caught up in new material and missing something basic. Thank you!