0

我想放在JTabbedPane中间,单击我想更改的任何选项卡将反映在选项卡窗格的上方和下方面板中。

我试过了,但它只适用于下面的面板。

如何克服这个问题?请帮我。

提前致谢。

这是我的代码:

    jTabbedPane1 = new javax.swing.JTabbedPane();
jTabbedPane1.addTab("Daily Market", jScrollPane1);
    jTabbedPane1.addTab("Weekly Market", jScrollPane2);
4

1 回答 1

2

假设您要更改选项卡式窗格上方和下方面板中的某些内容。在下面的顶部和底部面板中更改标签文本的示例代码:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TestJTabbedPane extends JFrame {

    /**
     * 
     */
    private static final long   serialVersionUID    = 1L;

    private void init(){
        this.setLayout(new BorderLayout());
        JPanel topPanel = new JPanel();
        final JLabel topLabel = new JLabel("North");
        topPanel.add(topLabel);
        this.add(topPanel, BorderLayout.NORTH);

        JTabbedPane tabbedPane = new JTabbedPane();
        JPanel firstTabCont = new JPanel();
        firstTabCont.add(new JLabel("First"));
        tabbedPane.addTab("First", firstTabCont);

        JPanel secondTabCont = new JPanel();
        secondTabCont.add(new JLabel("Second"));
        tabbedPane.addTab("Second", secondTabCont);

        this.add(tabbedPane, BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel();
        final JLabel bottomLabel = new JLabel("South");
        bottomPanel.add(bottomLabel);
        this.add(bottomPanel, BorderLayout.SOUTH);

        tabbedPane.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent evt) {
                JTabbedPane pane = (JTabbedPane)evt.getSource();
                int selectedIndex = pane.getSelectedIndex();
                if(selectedIndex == 0){
                    topLabel.setText("");
                    topLabel.setText("Hi");

                    bottomLabel.setText("");
                    bottomLabel.setText("Bye");
                } else {
                    topLabel.setText("");
                    topLabel.setText("Bye");

                    bottomLabel.setText("");
                    bottomLabel.setText("Hi");
                }

            }
        });
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new TestJTabbedPane().init();
    }
}
于 2012-04-12T08:23:45.543 回答