无法在不抛出 java.lang.ClassCastException 的情况下在 JPopupMenu 中执行 JMenuItem 操作
Can't perform JMenuItem action in JPopupMenu without getting thrown a java.lang.ClassCastException
我试图让 JFrame window 隐藏自己并在它关闭而不是退出时创建一个托盘图标。托盘图标应该有两个菜单项,分别能够使 JFrame window 再次可见和完全退出。前者工作正常,但是当尝试从托盘图标的 JPopupMenu 中的 JMenuItem 执行操作时,我抛出以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class javax.swing.JMenuItem cannot be cast to class javax.swing.JFrame (javax.swing.JMenuItem and javax.swing.JFrame are in module java.desktop of loader 'bootstrap')
at Hierophant.actionPerformed(Hierophant.java:94)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.AbstractButton.doClick(AbstractButton.java:369)
at java.desktop/javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1020)
at java.desktop/javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1064)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
这里是控制最小化行为和 JPopupMenu 的相关代码部分。它是 运行 在 public class 扩展 JFrame 中没有参数格式化程序的方法。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
SystemTray tray = SystemTray.getSystemTray();
JPopupMenu menu = new JPopupMenu();
JMenuItem show = new JMenuItem("Show");
JMenuItem exit = new JMenuItem("Exit");
icon = Toolkit.getDefaultToolkit().getImage("icon.png");
trayIcon = new TrayIcon(icon, "Hierophant");
show.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((JFrame)e.getSource()).setExtendedState(JFrame.NORMAL);
((JFrame)e.getSource()).setExtendedState(((JFrame)e.getSource()).getExtendedState() & (~JFrame.ICONIFIED));
pack();
}
});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((JFrame)e.getSource()).dispose();
}
});
menu.add(show);
menu.addSeparator();
menu.add(exit);
trayIcon = new TrayIcon(icon, "Hierophant");
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter() {
public void showPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
menu.setLocation(e.getX(), e.getY());
menu.setInvoker(menu);
menu.setVisible(true);
}
}
@Override
public void mouseReleased(MouseEvent e) {
showPopup(e);
}
public void mousePressed(MouseEvent e) {
showPopup(e);
}
});
try {
tray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
((JFrame)e.getSource()).setExtendedState(JFrame.ICONIFIED);
((JFrame)e.getSource()).setExtendedState(((JFrame)e.getSource()).getExtendedState() | JFrame.ICONIFIED);
}
});
代码可能不是最漂亮的,但我仍然不知道是什么导致 Java 抛出此异常。有没有我特别搞砸的地方?
正如评论者所指出的,JFrame
绝对不是您活动的来源。
如果您希望这个确切的代码不抛出异常,您可以在调用转换为 JFrame
:
时简单地捕获异常
try
{
(JFrame) e.getSource();
}
catch(ClassCastException e)
{
// nothing
}
您可能想要做的是获取作为事件源的 Component
,然后获取该 Component
的父级 Container
。
它可能看起来像这样:
if(e.getSource() instanceof Component)
{
Component component = (Component) e.getSource();
if(component.getParent() instanceof JFrame)
{
JFrame frame = (JFrame) component;
frame.setExtendedState(JFrame.NORMAL);
}
}
The former works properly, but when attempting to perform an action from a JMenuItem from the tray icon's JPopupMenu, I am thrown the following exception:...
前者有效,因为您将 WindowListener 添加到 JFrame。
后者不起作用,因为您将 ActionListener 添加到 JMenuItem。
... Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class javax.swing.JMenuItem cannot be cast to class javax.swing.JFrame
你觉得那个异常有什么令人困惑的地方?如果您单击 JMenuItem,您为什么认为可以将 ActionEvent 的源视为 JFrame?
您在论坛中的问题应该是:如何访问给定 JMenuItem 的框架?
对此您可以尝试:
JMenuItem menuItem = (JMenuItem)e.getSource();
Window window = SwingUtilitiels.windowForComponent( menuItem );
window.dispose();
我试图让 JFrame window 隐藏自己并在它关闭而不是退出时创建一个托盘图标。托盘图标应该有两个菜单项,分别能够使 JFrame window 再次可见和完全退出。前者工作正常,但是当尝试从托盘图标的 JPopupMenu 中的 JMenuItem 执行操作时,我抛出以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class javax.swing.JMenuItem cannot be cast to class javax.swing.JFrame (javax.swing.JMenuItem and javax.swing.JFrame are in module java.desktop of loader 'bootstrap')
at Hierophant.actionPerformed(Hierophant.java:94)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.AbstractButton.doClick(AbstractButton.java:369)
at java.desktop/javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1020)
at java.desktop/javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1064)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
这里是控制最小化行为和 JPopupMenu 的相关代码部分。它是 运行 在 public class 扩展 JFrame 中没有参数格式化程序的方法。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
SystemTray tray = SystemTray.getSystemTray();
JPopupMenu menu = new JPopupMenu();
JMenuItem show = new JMenuItem("Show");
JMenuItem exit = new JMenuItem("Exit");
icon = Toolkit.getDefaultToolkit().getImage("icon.png");
trayIcon = new TrayIcon(icon, "Hierophant");
show.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((JFrame)e.getSource()).setExtendedState(JFrame.NORMAL);
((JFrame)e.getSource()).setExtendedState(((JFrame)e.getSource()).getExtendedState() & (~JFrame.ICONIFIED));
pack();
}
});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((JFrame)e.getSource()).dispose();
}
});
menu.add(show);
menu.addSeparator();
menu.add(exit);
trayIcon = new TrayIcon(icon, "Hierophant");
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter() {
public void showPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
menu.setLocation(e.getX(), e.getY());
menu.setInvoker(menu);
menu.setVisible(true);
}
}
@Override
public void mouseReleased(MouseEvent e) {
showPopup(e);
}
public void mousePressed(MouseEvent e) {
showPopup(e);
}
});
try {
tray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
((JFrame)e.getSource()).setExtendedState(JFrame.ICONIFIED);
((JFrame)e.getSource()).setExtendedState(((JFrame)e.getSource()).getExtendedState() | JFrame.ICONIFIED);
}
});
代码可能不是最漂亮的,但我仍然不知道是什么导致 Java 抛出此异常。有没有我特别搞砸的地方?
正如评论者所指出的,JFrame
绝对不是您活动的来源。
如果您希望这个确切的代码不抛出异常,您可以在调用转换为 JFrame
:
try
{
(JFrame) e.getSource();
}
catch(ClassCastException e)
{
// nothing
}
您可能想要做的是获取作为事件源的 Component
,然后获取该 Component
的父级 Container
。
它可能看起来像这样:
if(e.getSource() instanceof Component)
{
Component component = (Component) e.getSource();
if(component.getParent() instanceof JFrame)
{
JFrame frame = (JFrame) component;
frame.setExtendedState(JFrame.NORMAL);
}
}
The former works properly, but when attempting to perform an action from a JMenuItem from the tray icon's JPopupMenu, I am thrown the following exception:...
前者有效,因为您将 WindowListener 添加到 JFrame。
后者不起作用,因为您将 ActionListener 添加到 JMenuItem。
... Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class javax.swing.JMenuItem cannot be cast to class javax.swing.JFrame
你觉得那个异常有什么令人困惑的地方?如果您单击 JMenuItem,您为什么认为可以将 ActionEvent 的源视为 JFrame?
您在论坛中的问题应该是:如何访问给定 JMenuItem 的框架?
对此您可以尝试:
JMenuItem menuItem = (JMenuItem)e.getSource();
Window window = SwingUtilitiels.windowForComponent( menuItem );
window.dispose();