PropertyChangeListener中的JOptionPane.showMessageDialog会导致意外行为(JOptionPane.showMessageDialog inside Pr

编程入门 行业动态 更新时间:2024-10-27 08:27:23
PropertyChangeListener中的JOptionPane.showMessageDialog会导致意外行为(JOptionPane.showMessageDialog inside PropertyChangeListener causes unexpected behavior)

我用JCheckBoxMenuItem创建了一个可检查的菜单项。 我试图做的是当复选框被选中时,应该出现一条消息:

JCheckBoxMenuItem checkbox = new JCheckBoxMenuItem("Checkbox"); checkbox.addPropertyChangeListener(evt -> { boolean isCheck = ((JCheckBoxMenuItem) evt.getSource()).isSelected(); if(isCheck){ JOptionPane.showMessageDialog(null, "You checked the checkbox", "Information", JOptionPane.INFORMATION_MESSAGE); } });

但是,当我尝试选中复选框时,没有任何内容出现。 第二次点击JMenu出现一堆模态,并导致一个ArrayIndexOutOfBoundsException :

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0 at java.util.Vector.elementAt(Vector.java:474) at javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:117) at javax.swing.MenuSelectionManager.clearSelectedPath(MenuSelectionManager.java:151) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.cancelPopupMenu(BasicPopupMenuUI.java:917) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(BasicPopupMenuUI.java:828) at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2425) at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Toolkit.java:2317) at java.awt.Toolkit.notifyAWTEventListeners(Toolkit.java:2275) at java.awt.Component.dispatchEventImpl(Component.java:4777) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109) ...

是什么导致这种情况发生?

I have created a checkable menu item with JCheckBoxMenuItem. What I tried to do is when the checkbox gets checked, a message should appear:

JCheckBoxMenuItem checkbox = new JCheckBoxMenuItem("Checkbox"); checkbox.addPropertyChangeListener(evt -> { boolean isCheck = ((JCheckBoxMenuItem) evt.getSource()).isSelected(); if(isCheck){ JOptionPane.showMessageDialog(null, "You checked the checkbox", "Information", JOptionPane.INFORMATION_MESSAGE); } });

However when I tried to check the checkbox, nothing appears. The second time I click on the JMenu a bunch of modal appears, and causes an ArrayIndexOutOfBoundsException:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0 at java.util.Vector.elementAt(Vector.java:474) at javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:117) at javax.swing.MenuSelectionManager.clearSelectedPath(MenuSelectionManager.java:151) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.cancelPopupMenu(BasicPopupMenuUI.java:917) at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(BasicPopupMenuUI.java:828) at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2425) at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Toolkit.java:2317) at java.awt.Toolkit.notifyAWTEventListeners(Toolkit.java:2275) at java.awt.Component.dispatchEventImpl(Component.java:4777) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109) ...

What is causing that to happen?

更多推荐

本文发布于:2023-07-14 17:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意外   JOptionPane   PropertyChangeListener   showMessageDialog   behavior

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!