[英]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)
...
是什么導致這種情況發生?
是什么導致這種情況發生?
您為錯誤的工作使用了錯誤的偵聽器。 PropertyChangeListener
不是您想要實現的目標的適當的偵聽器,相反,您應該使用ActionListener
...
JCheckBoxMenuItem checkbox = new JCheckBoxMenuItem("Checkbox");
checkbox.addActionListener((ActionEvent e) -> {
boolean isCheck = ((JCheckBoxMenuItem)e.getSource()).isSelected();
if (isCheck) {
JOptionPane.showMessageDialog(null, "You checked the checkbox", "Information", JOptionPane.INFORMATION_MESSAGE);
}
});
當更改JCheckBoxMenuItem
的選定狀態時, PropertyChangeListener
實際上(至少在我的測試下)不會觸發任何事件,但是您的代碼確實導致它不斷拋出對話框,迫使我殺死程序。
有關更多詳細信息,請參閱如何使用菜單 , 如何使用按鈕,復選框和單選按鈕以及如何編寫動作偵聽器 。
您應該首先檢查JOptioanPane
或偵聽器是否存在問題。 您可以使用System.out.println("check="+check);
首先而不是顯示JOptionPane
。 如果可以,請更改Listener
類型
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.