【问题标题】:JTabbedPane perform action above and belowJTabbedPane 执行上下动作
【发布时间】:2012-04-12 07:20:39
【问题描述】:

我想把JTabbedPane放在中间,点击我想更改的任何标签都会反映在标签窗格的上方和下方。

我试过了,但它只能在下面的面板上工作。

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

提前致谢。

这是我的代码:

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

【问题讨论】:

  • 没有真正理解您的意思...您能否更详细地解释您的问题并添加更多代码来呈现实际问题?

标签: java swing jpanel layout-manager jtabbedpane


【解决方案1】:

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

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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2013-07-23
    • 2020-02-12
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多