使用JMenuItem选择JTabbedPane Panel

时间:2012-06-29 15:07:04

标签: java jtabbedpane jmenuitem pane

我在一个框架中有一个菜单栏和一个标签式窗格,我希望如果我选择一个menuitem,那么将打开所请求的选项卡。请帮帮我,谢谢!

2 个答案:

答案 0 :(得分:2)

ActionListener的{​​{1}}中,您可以拨打JTabbedPane#setSelectedIndex

答案 1 :(得分:1)

像SoboLAN说的那样:

    final JTabbedPane tabs = new JTabbedPane();
    JPanel panel = new JPanel();
    tabs.add("title", panel);
    //add more tabs...

    // here the important part starts
    JMenuItem item = new JMenuItem("open tab 1");
    item.addActionListener(new ActionListener() {
        //this function get called when you click the item.
        @Override
        public void actionPerformed(ActionEvent e) {
            //insert the index you want to select
            tabs.setSelectedIndex(0);
        }
    });