gpt4 book ai didi

java - JTabbedPane 执行上方和下方的操作

转载 作者:行者123 更新时间:2023-12-02 07:52:06 28 4
gpt4 key购买 nike

我想将 JTabbedPane 放在中间,单击我想要更改的任何选项卡将反射(reflect)在选项卡 Pane 的上方和下方面板中。

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

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

提前致谢。

这是我的代码:

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

最佳答案

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

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

关于java - JTabbedPane 执行上方和下方的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10119167/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com