循环重绘组件

发布于 2024-10-11 08:42:18 字数 522 浏览 6 评论 0原文

我正在创建一个简单的游戏,我想在每次移动后重新绘制棋盘。所以,在我调用 move() 之后,我想做的是这样的:(顺便说一下,View 是一个保存碎片的 JComponent;由于移动后碎片数量发生了变化,所以需要重新绘制

for(View v : views){            
        v.repaint();
    }

)不工作。当我在单个视图上调用 repaint() 时,它工作正常。我尝试使用paintImmediatelyrevalidateupdate...循环内没有任何效果。

有什么想法吗?提前致谢。

编辑: 我应该补充一点,当窗口大小调整时, repaint() 确实会被调用,所以我知道 View 的 PaintComponent 方法是有效的并且可以工作。只是没有从循环中调用它。当调试器单步执行循环时,它不会进入 repaint() 并且屏幕上没有任何反应。

I'm creating a simple game and I'd like to repaint the board after every move. So, after I call move(), what I would like to do is this: (by the way, a View is a JComponent that holds pieces; since the number of pieces has changed after the move, it needs to be repainted)

for(View v : views){            
        v.repaint();
    }

It's not working. When I call repaint() on a single View, it works fine. I tried using paintImmediately, and revalidate, and update... nothing works within the loop.

Any ideas? Thanks in advance.

EDIT: I should add that repaint() does get called when the window is resized, so I know that View's paintComponent method is valid and works. It's just not being called from the loop. When the debugger steps through the loop, it does not enter repaint() and nothing happens to the screen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就此别过 2024-10-18 08:42:18

与 UI 相关的所有内容都必须在事件调度线程 (EDT) 中调用:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        for(View v : views){
            v.repaint();
        }
    }
});

您还可以使用 invokeAndWait 而不是 invokeLater。如果您想要一个响应式应用程序,您应该阅读 EDT。

例如,如果您向按钮添加一个actionListener,则该actionListener中执行的代码将在EDT线程中执行,因此您必须限制该进程,否则您的UI将停止响应。

另外,请查看 SwingUtilities .isEventDispatchingThread()

Everything related to the UI must be called in the Event Dispatching Thread (EDT):

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        for(View v : views){
            v.repaint();
        }
    }
});

You can also use invokeAndWait instead of invokeLater. You should read on the EDT if you want a responsive application.

For example, if you add an actionListener to a button, the code executed in that actionListener is executed in the EDT thread, so you must limit the process or you UI will stop responding.

Also, take a look to SwingUtilities.isEventDispatchingThread()

挽清梦 2024-10-18 08:42:18

有时,如果最近的 validateRoot 是 JScrollPane,则重新验证不起作用。不知道为什么。尝试对框架本身调用 revalidate 以查看是否有效。如果是这样,则说明 validateRoot 无法正确验证您的组件。循环完成后,您只需要调用一次 revalidate 即可。

Sometimes revalidate does not work if the nearest validateRoot is a JScrollPane. Not sure why. Try calling revalidate on the frame itself to see if that works. If it does, you have a problem with a validateRoot not properly validating your components. You only need to call revalidate once when the loop is finished.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文