为什么 JFrame 没有响应?

发布于 2024-09-30 12:57:22 字数 342 浏览 2 评论 0原文

我有一个 JFrame 和两个 JPanel。当我的程序启动时,它将第一个 JPanel 添加到 JFrame 中。 JFrame 组件(x、minimize、KeyListener 等)都可以工作。但是当我删除第一个 JPanel 并添加第二个 JPanel 时,JFrame 的“x”按钮将不起作用,任何 KeyListener 也不起作用MouseListener。如何让 JFrame 正常运行?

I have a JFrame and two JPanels. When my program starts it adds the first JPanel to the JFrame. The JFrame components (x, minimize, KeyListener, etc) all work. but when I remove the first JPanel and add the second JPanel the JFrame's 'x' button wont work nor will any KeyListener or MouseListener. How can I get the JFrame to function correctly?

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

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

发布评论

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

评论(1

囚你心 2024-10-07 12:57:22

如果在更改组件后基本 GUI 操作停止运行,那么您很可能与 Swing 线程模型发生了冲突。 Swing 是单线程的,您需要遵循一个简单的规则,确保影响 GUI 绘制的操作是在事件调度线程中完成的,否则您将面临死锁或其他不良行为的风险。

你有几个选择。您可以使用 SwingWorker< /a>,或者您可以实例化一个 Runnable 并将其放入 SwingUtilities.invokeLaterSwingUtilities.invokeAndWait 中。请查看此链接,了解有关 Swing 中线程的概念。

If fundamental GUI operations cease to function after you have made changes to your components, there is a good chance that you have run foul of the Swing threading model. Swing is single threaded and you need to follow a simple rule of ensuring that actions which effect painting of the GUI are done in the Event Dispatch Thread or you run the risk of deadlock or other unwanted behavior.

You have a couple options. You can employ a SwingWorker, or you can instantiate a Runnable and drop it into SwingUtilities.invokeLater or SwingUtilities.invokeAndWait. Take a look at this link for concepts around threading in Swing.

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