简体   繁体   English

单击禁用的JMenuItem时如何触发动作?

[英]How to trigger an action when clicking a disabled JMenuItem?

I did a bit of googling and poking around in SO but all examples are find are for cases when the JMenuItem is enabled. 我在SO中做了一些谷歌搜索和查找,但是找到的所有示例都是针对启用JMenuItem情况。

Context for what I'm trying to do is that I want my disabled JMenuItem (because of limited privileges), when clicked, to display a pop up box requesting that the user upgrade so that they can access said JMenuItem . 我要执行的操作的上下文是,我希望禁用的JMenuItem (由于权限受限)在单击时显示一个弹出框,要求用户升级以便他们可以访问所述JMenuItem

The following is a stripped down version of what I currently have, nothing got printed out on the command line: 以下是我目前所拥有的精简版本,命令行上未打印任何内容:

public class ExportMenuItem extends JMenuItem
{
    public ExportMenuItem()
    {
        super("Menu Item Name");

        addMouseListener(new MouseAdapter()
            {
                @Override
                public void mouseClicked(MouseEvent mouseEvent)
                {
                    if (!isEnabled())
                    {
                        JOptionPane.showMessageDialog(editor.getFrame(), "Hello world.");
                        System.out.println("Sys print hello.");
                    }
                    System.out.println("Sys print hello outside.");
                }
            });
    }
}

is this what you are looking for? 这是你想要的?

import javax.swing.*;
import java.awt.event.*;

public class ExportMenuItem extends JMenuItem{

    public ExportMenuItem(){
        super("menu item");

        addMouseListener(new MouseListener(){
                @Override
                public void mouseClicked(MouseEvent mouseEvent){
                    if (!isEnabled())                    {
                        JOptionPane.showMessageDialog(null, "Upgrade me!");
                    }//end of if
                }//end of mouseClicked
                public void mouseExited(MouseEvent mouseEvent){}
                public void mouseEntered(MouseEvent mouseEvent){}
                public void mouseReleased(MouseEvent mouseEvent){}
                public void mousePressed(MouseEvent mouseEvent){}

                // And the remaining methods to implement...
            });//end of anonymous class
    }//end of constructor

    public static void main(String[] a){
        JFrame f = new JFrame();
        JMenu menu = new JMenu("menu");
        JMenuBar menuBar  = new JMenuBar();
        f.setJMenuBar(menuBar);
        f.setSize(300, 300);
        f.setVisible(true);
        menuBar.add(menu);
        JMenuItem item = new ExportMenuItem();
        menu.add(item);
        item.setEnabled(false);
    }//end of main
}//end of class

Maybe a complete different approach, that is more logical for users: 也许是一种完全不同的方法,对用户而言更合乎逻辑:

Place a describing text behind the menu item: 在菜单项后面放置一个描述文字:

private void addRestartHint(JMenuItem m, String text) {
    final String spaceholder = "                                        ";
    String t = m.getText() + spaceholder;
    m.setLayout(new BorderLayout());
    m.setText(t);
    m.add(new JLabel(text), BorderLayout.EAST);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM