【问题标题】:Can't perform JMenuItem action in JPopupMenu without getting thrown a java.lang.ClassCastException无法在 JPopupMenu 中执行 JMenuItem 操作而不会抛出 java.lang.ClassCastException
【发布时间】:2020-12-22 09:52:17
【问题描述】:

我试图让 JFrame 窗口隐藏自己,并在它关闭而不是退出时创建一个托盘图标。然后托盘图标应该有两个菜单项,分别能够使 JFrame 窗口再次可见并完全退出它。前者可以正常工作,但是当尝试从托盘图标的 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$5.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$4.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue$4.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$5.run(EventQueue.java:745)
        at java.desktop/java.awt.EventQueue$5.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 的代码的相关部分。它在扩展 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)e.getSource())?您正试图将 e.getSource() 的结果强制转换为 JFrame,但 e.getSource() 不返回 JFrame。
  • JFrame 不太可能是事件的来源

标签: java swing jframe awt


【解决方案1】:

正如评论者所指出的,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);
    }
}

【讨论】:

    【解决方案2】:

    前者可以正常工作,但是当尝试从托盘图标的 JPopupMenu 中的 JMenuItem 执行操作时,我会抛出以下异常:...

    前者有效,因为您将 WindowListener 添加到 JFrame。

    后者不起作用,因为您将 ActionListener 添加到 JMenuItem。

    ...线程“AWT-EventQueue-0”java.lang.ClassCastException 中的异常:类 javax.swing.JMenuItem 无法转换为类 javax.swing.JFrame

    您对该异常有什么困惑?如果您单击 JMenuItem,为什么您认为可以将 ActionEvent 的源视为 JFrame?

    您在论坛中的问题应该是:如何访问给定 JMenuItem 的框架?

    对此,您可以尝试:

    JMenuItem menuItem = (JMenuItem)e.getSource();
    Window window = SwingUtilitiels.windowForComponent( menuItem );
    window.dispose();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2021-02-25
      相关资源
      最近更新 更多