JMenu在单个选项卡上工作 [英] JMenu work on single tab
问题描述
我有一个JMenuBar
并且我有一个JTabbedPane
,当我创建多个标签时,JMenuBar
会在所有标签上进行操作,例如我打开一个文件,它在每个选项卡中打开相同的文件.
I have a JMenuBar
and I have a JTabbedPane
, when I create multiple tabs the JMenuBar
does it on all the tabs, e.g. I open a file it opens the same file in each tab.
但是,我只希望它在当前选项卡上打开文件.我添加了ChangeListener
,所以它知道其中的选项卡,但是即使可以工作,它也似乎无济于事.
However I only want it to open a file on the current tab. I have added a ChangeListener
so It know what tab its in however it does not seem help even though it works.
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("Tab=" + tabbedPane.getSelectedIndex());
}
});
getContentPane().add(tabbedPane, "Center");
pack();
我希望能够打开多个选项卡,而菜单栏仅对应于当前选项卡.
I want to be able to have multiple tabs open while the menu bar only corresponds to current tab.
推荐答案
让提供每个选项卡导出内容的对象是此处.
Let the object providing the content of each tab export it's own specific instances of Action
. When the user changes tabs, use setAction()
to set the desired action of each available JMenuItem
in the global menu. There's a related example here.
这篇关于JMenu在单个选项卡上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!