gpt4 book ai didi

java - JButton 的操作调用了 focusLost 事件。这怎么可能?

转载 作者:搜寻专家 更新时间:2023-10-31 19:30:12 25 4
gpt4 key购买 nike

我们的一位客户报告我们的应用程序出现异常。问题是,我完全无法理解如何重现此错误。

代码如下:

btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
popup.show(btn, 3, btn.getHeight());
}
});

注释:

  • btnJButton 类型的最终局部变量。
  • popupJPopupMenu 类型的最终局部变量。

抛出以下异常:

java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Unknown Source)
at java.awt.Component.getLocationOnScreen(Unknown Source)
at javax.swing.JPopupMenu.show(Unknown Source)
at fr.def.iss.vd2.mod_site_watcher_gui.SiteElementPanel$4.actionPerformed(SiteElementPanel.java:117)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.focusLost(Unknown Source)
at java.awt.Component.processFocusEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

据我所知,show 方法提示 btn 没有显示。调用 actionPerformed 方法时 btn 怎么可能不显示?

此堆栈跟踪中最奇怪的是 actionPerformed 方法似乎在处理 FocusEvent 时被触发(实际上是 focusLost ).

问题是:您能解释一下这个堆栈跟踪是如何发生的吗?

结语

感谢trashgod的建议,我找到了问题。

在 Windows 上,当按钮在被按下时消失,然后它的 ActionListeners 被触发,就像按钮被点击一样。这种行为可以在 Windows 上观察到,但不能在 Linux 上观察到。

我在 Oracle/Sun 错误数据库上提交了错误。这是链接:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7115421

(此链接将在几天内生效,经过 Java 团队审核后)。

感谢您的帮助。 trashgod 和 Thomas 的回答帮了大忙。

最佳答案

一个可能的来源是竞争条件,它允许事件在接收者可见之前触发。验证您的 Swing GUI 对象是在 event dispatch thread 上构建和操作的.文章Debugging Swing, the final summary引用于 How to generate exceptions from RepaintManager提到了几种自动搜索的方法。

关于java - JButton 的操作调用了 focusLost 事件。这怎么可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8229311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com