我正在添加一个 JMenuItem
(显示历史记录),它将在单击时切换 JPanel
的外观。但是这样做之后,我想更改该菜单项的标题以说明相反的操作(隐藏历史记录)。有没有办法只更改该菜单项的文本,或者我必须删除旧的 JMenuItem
并添加一个新的?
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");
并让历史成为最终的
。
关于java - 单击后更改 JMenuItem 的标题文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20234460/