更改 JTabbedPane 中的选定选项卡 "automatically"

change the selected tab in JTabbedPane "automatically"

我是第一次使用Java swing,有一个问题我无法解决

我有一个带有 3 个选项卡的 JTabbedPane,我需要在计算结束时切换到下一个选项卡 "automatically",计算开始时单击 JButton(在其中一个选项卡内)。 我试过使用 setSelectedIndex() 但没有用。 在 JButton 的 ActionEvent 之后,选定的选项卡(在 eclipse 控制台中打印)发生了变化,但在 GUI 中没有效果。

是的,我试过validate()revalidate()方法,甚至repaint()方法,但都没有用

这是我的代码示例

public class Tab1 extends JPanel {
    //when click on this button the computing starts
    JButton btn = new JButton("Compute...");
    btn.addActionListener(new BtnListener());
    add(btn);
}

我将添加到 JTabbedPane 的 JPanel 示例

public class Window() {

    private JFrame frame;

    public Window() {
        init();
    }

    private void init() {
        frame = new JFrame();
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

        JPanel tab1 = new Tab1();
        JPanel tab2 = new Tab2();
        JPanel tab3 = new Tab3();

        tabbedPane.addTab("tab 1", null, tab1, "tab1");
        tabbedPane.addTab("tab2", null, tab2, "tab2");
        tabbedPane.addTab("tab3", null, tab3, "tab3");

        //add() all components
    }
}

我的例子 Window

public class BtnListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        //make the computing
        //then I need to change to next tab (tab2)
    }
}

还有我的听众

希望您能理解我的问题。 请帮助我,我真的不知道我该怎么做才能让它发挥作用,我需要这样做。

当您切换标签时,您不需要使用 revalidate()/repaint() 只需 setSelectedIndex()

这是切换到下一个标签的示例:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class TestFrame extends JFrame {

    public static void main(String... s) {
        new TestFrame();
    }

    private JTabbedPane tabs;

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        tabs = new JTabbedPane();

        tabs.addTab("1", new Tab(1));
        tabs.addTab("2", new Tab(2));
        tabs.addTab("3", new Tab(3));
        add(tabs);
    }

    private class Tab extends JPanel {

        public Tab(int i) {
            JButton next = new JButton("next");
            next.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    int selectedIndex = tabs.getSelectedIndex();
                    int nextIndex = selectedIndex == tabs.getTabCount()-1 ? 0 : selectedIndex+1;
                    tabs.setSelectedIndex(nextIndex);
                }
            });
            add(new JLabel("tab " + i));
            add(next);
        }
    }

}

感谢您的回答。

我检查了我的代码,发现了一个实例问题。我正在更改 Window 的另一个实例,而不是可见的。所以,当我解决这个问题时,更新就起作用了。

再次感谢。

更改侦听器类似于属性更改侦听器。更改侦听器注册在一个对象上——通常是一个组件,但它也可以是另一个对象,比如一个模型——当对象发生变化时,侦听器会收到通知。与 属性 更改侦听器的最大区别在于,更改侦听器不会收到更改内容的通知,而只会收到源对象已更改的通知。因此,当只需要知道对象何时以任何方式发生更改时,更改侦听器最有用。

同样,您需要在按钮(或带有状态的切换按钮)上注册一个更改侦听器,以便在计算停止和按钮的值更改时收到通知。

//use some flag to determine when calculation process is actually finished
volatile boolean isCalculationEnds = true;  

button.addChangeListener((ChangeEvent e) -> {
 if (tabbedPane.getSelectedComponent() instanceof CustomPanel && isCalculationEnds) {
     //do something 
     }

 if (tabbedPane.getSelectedIndex()==1 && isCalculationEnds){
   //do something
 });