我想将JTabbedPane
放在中间,点击我想要更改的任何标签都会反映在tabbedpane的上方和下方。
我尝试了它,但它仅适用于下面的面板。
如何克服这个问题?请帮帮我。
提前致谢。
这是我的代码:
jTabbedPane1 = new javax.swing.JTabbedPane();
jTabbedPane1.addTab("Daily Market", jScrollPane1);
jTabbedPane1.addTab("Weekly Market", jScrollPane2);
答案 0 :(得分: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();
}
}