我运行的是下面的代码,两个标签都能显示。楼主能不能贴出来有问题的代码? JTextArea ta1 = new JTextArea(); JTextArea ta2 = new JTextArea(); JTabbedPane tp = new JTabbedPane(); ta1.append("show me the money"); ta1.append("please show me why"); ta2.append("showme why"); tp.insertTab("标签一",null,ta1,"标签一",0); tp.insertTab("标签二",null,ta2,"标签二",1); add(tp); setLayout(new FlowLayout()); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100,100,400,300);
对这样的话,是都能显示,但当你两个标签都用同一个容器,如: JTextArea ta1 = new JTextArea(); JTabbedPane tp = new JTabbedPane(); ta1.append("show me the money"); ta1.append("please show me why"); tp.insertTab("标签一",null,ta1,"标签一",0); tp.insertTab("标签二",null,ta1,"标签二",1); add(tp); setLayout(new FlowLayout()); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100,100,400,300); 这样就会出问题了,只能显示标签二,而标签一似乎不见了。
解决了 你看一下 应该还是你的容器放置的顺序有问题 哈哈 结贴给分吧 import javax.swing.*;public class JTabbedTest extends JFrame { JTabbedPane tp = new JTabbedPane(JTabbedPane.TOP); JPanel p1 = new JPanel();
JTextArea ta1 = new JTextArea(); JTextArea ta2 = new JTextArea();
public JTabbedTest(){ ta1.append("show me the money"); ta2.append("show me the money");
JTextArea ta1 = new JTextArea();
JTextArea ta2 = new JTextArea();
JTabbedPane tp = new JTabbedPane(); ta1.append("show me the money");
ta1.append("please show me why");
ta2.append("showme why");
tp.insertTab("标签一",null,ta1,"标签一",0);
tp.insertTab("标签二",null,ta2,"标签二",1);
add(tp); setLayout(new FlowLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,400,300);
JTextArea ta1 = new JTextArea();
JTabbedPane tp = new JTabbedPane();
ta1.append("show me the money");
ta1.append("please show me why");
tp.insertTab("标签一",null,ta1,"标签一",0);
tp.insertTab("标签二",null,ta1,"标签二",1);
add(tp);
setLayout(new FlowLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,400,300);
这样就会出问题了,只能显示标签二,而标签一似乎不见了。
JTabbedPane tp = new JTabbedPane(JTabbedPane.TOP);
JPanel p1 = new JPanel();
JTextArea ta1 = new JTextArea();
JTextArea ta2 = new JTextArea();
public JTabbedTest(){
ta1.append("show me the money");
ta2.append("show me the money");
tp.add("Tab1", ta1);
tp.add("Tab2", ta2);
p1.add(tp);
getContentPane().add(p1);
}
public static void main(String[] args) {
JTabbedTest a = new JTabbedTest();
a.setSize(300, 300);
a.setVisible(true);
}
}