gpt4 book ai didi

java - 菜单项回调不起作用

转载 作者:行者123 更新时间:2023-12-02 04:47:14 25 4
gpt4 key购买 nike

//当我点击"file"时,以下回调起作用

    menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);

mnFile = new JMenu("File");
mnFile.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
btnNewButton_0.setText("File pressed");
}
});
menuBar.add(mnFile);

//点击菜单项:open时以下回调未能响应

    mntmOpen = new JMenuItem("Open");
mntmOpen.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
btnNewButton_0.setText("Open pressed");
}
});
mnFile.add(mntmOpen);

//不确定我错过了什么?

最佳答案

不要使用 MouseListener!!!

如果您想在单击 JMenu 时执行某些操作,请使用 JPopupMenuListener:

JMenu file = new JMenu( "File" );

JPopupMenu popup = file.getPopupMenu();
popup.addPopupMenuListener(new PopupMenuListener()
{
public void popupMenuCanceled(PopupMenuEvent e) {}

public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}

public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
System.out.println("menu clicked");
}
});

否则,只需将 ActionListener 添加到 JMenuItem 即可,如 How to Use Menus 上的 Swing 教程中所示。 .

关于java - 菜单项回调不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29565209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com