如果没有抛出java.lang.classcastexception,则无法在jpopupmenu中执行jmenuitem操作

8xiog9wr  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(290)

我试图让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)

下面是控制最小化行为的代码和jpopmpmenu的相关部分。它在一个扩展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抛出这个异常。有什么地方我搞砸了吗?

zz2j4svz

zz2j4svz1#

正如评论人士所指出的, JFrame 绝对不是你事件的源头。
如果您希望这段代码不抛出异常,那么只要在调用转换时捕获异常即可 JFrame :

try
{
    (JFrame) e.getSource(); 
}
catch(ClassCastException e)
{
    // nothing
}

你可能想做的是 Component 它是事件源,然后获取父级 Container 那是什么 Component .
它可能看起来像这样:

if(e.getSource() instanceof Component)
{
    Component component = (Component) e.getSource();

    if(component.getParent() instanceof JFrame)
    {
        JFrame frame = (JFrame) component;

        frame.setExtendedState(JFrame.NORMAL);
    }
}
lmyy7pcs

lmyy7pcs2#

前者可以正常工作,但是当试图从托盘图标的jpopmpmenu中的jmenuitem执行操作时,会抛出以下命令exception:...
前者的工作原理是将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();

相关问题