JTabbedPane组件的选项卡未显示

时间:2017-12-13 14:30:20

标签: java swing layout-manager jtabbedpane

给定一个JFrame,其contentPane的布局设置为null,我想为Publisher添加一个标签,另一个为Subscriber,例如:

public class PubSubGUI extends JFrame{
   private JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
   private JPanel pubPanel = new JPanel();
   private JPanel subPanel = new JPanel();  
   public PubSubGUI(Controller controller) {
         getContentPane().setLayout(null);
         getContentPane().add(tabbedPane);
         //add Publisher components to pubPanel
         tabbedPane.addTab("Publlisher", pubPanel);         
         //add Subscriber components to pubPanel
         tabbedPane.addTab("Subscriber", subPanel);
         //Rest of the constructor's source code is omitted
   }
   //Rest of the class' source code is omitted
}

运行应用程序时,既不显示组件也不显示选项卡。我得到的只是一个空JFrame。我尝试为LayoutManagerpubPanel中的每一个设置不同的subPanel,问题仍然存在。请提供任何提示或建议。

1 个答案:

答案 0 :(得分:1)

参考:

import javax.swing.*;  
public class TabbedPaneExample {  
JFrame f;  
TabbedPaneExample(){  
    f=new JFrame();  
    JTextArea ta=new JTextArea(200,200);  
    JPanel p1=new JPanel();  
    p1.add(ta);  
    JPanel p2=new JPanel();  
    JPanel p3=new JPanel();  
    JTabbedPane tp=new JTabbedPane();  
    tp.setBounds(50,50,200,200);  
    tp.add("main",p1);  
    tp.add("visit",p2);  
    tp.add("help",p3);    
    f.add(tp);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);  
}  
public static void main(String[] args) {  
    new TabbedPaneExample();  
}}