【发布时间】:2012-01-03 23:38:14
【问题描述】:
我们的一位客户报告了我们的应用程序出现异常。问题是,我完全无法理解如何重现此错误。
代码如下:
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
popup.show(btn, 3, btn.getHeight());
}
});
注意事项:
-
btn是JButton类型的最终局部变量。 -
popup是JPopupMenu类型的最终局部变量。
抛出以下异常:
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 团队审核后)。
感谢您的帮助。垃圾神和托马斯的回答很有帮助。
【问题讨论】:
-
问题出在您没有显示的代码中
-
@kleopatra 也许是这样。周围的代码与问题无关,只会给它带来噪音。我只能说 1) JButton 的创建是启用或禁用的(之后未启用或禁用) 2) JButton 的容器可以从其层次结构中删除 3) JButton 的可见性 (
setVisible) 是从未改变 4)actionPerformed从未被我的代码显式调用 5) JButton 实例被添加到容器中,但它从未在其他地方引用。也没有代码尝试通过其容器访问 JButton -
正如许多人所说,SSCCE 是必需的。我们知道您在使用
ActionListener做什么,但我们不知道您还在使用按钮做什么。如果我们要帮助您,我们需要查看该代码,而您发布的那段代码不足以让我们找到问题。如果您只是将相同的按钮添加到一个空框架而无法重现问题,那么问题出在您的代码中的其他地方。 -
@Laf 这个问题根本无法重现。所以,不,我不能给你一个 SSCCE。问题的性质禁止这样做。
-
“周围的代码与问题无关”这显然是错误的,因为您显示的 sn-p 看起来很好并且不会产生错误:-) 尽全力重现问题 -这是您找到根本原因的唯一方法。没有,你在这里得到的一切都是疯狂的猜测
标签: java swing focus action jbutton