单击后更改JMenuItem的标题文本吗? [英] Change the title text of JMenuItem upon click?

查看:102
本文介绍了单击后更改JMenuItem的标题文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加一个JMenuItem(显示历史记录),单击该按钮将切换JPanel的外观.但是这样做之后,我想更改该菜单项的标题以声明相反的操作(隐藏历史记录).有没有一种方法可以仅更改该菜单项的文本,还是必须删除旧的JMenuItem并添加一个新的JMenuItem?

I am adding a JMenuItem (Show History) that will toggle the appearance of a JPanel upon click. But after doing so, I want to change the title of that menu item to state the opposite action (Hide History). Is there a way to change just the text for that menu item, or must I remove the old JMenuItem and add a new one?

JMenuItem history = new JMenuItem("Show History");
history.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        //code here to show the history
        //history.changeText("Hide History") OR viewMenu.remove(history) and create/add new one
    }
});
viewMenu.add(history);

推荐答案

这就是您要做的:

history.setText("Hide History");

并创建历史记录final.

这篇关于单击后更改JMenuItem的标题文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆