使用布局管理器将选项卡式窗格添加到面板

时间:2017-03-23 13:05:27

标签: java swing layout-manager jtabbedpane

我只是尝试在面板上添加带有5个标签的标签式窗格,但只显示最终标签(标签e )。

我显然在这里做了一些根本性的错误,我尝试更改Panel的布局管理器,标签窗格正在添加,但我不认为这是问题所在。任何adivce都会有所帮助,谢谢!

主类代码:

public static void main(String[] args) {
    JFrame frame = new JFrame("Data Structures Program");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 600);
    GraphicPanel G = new GraphicPanel();
    frame.add(G.getPanel());
    frame.setVisible(true);
}

图形类

public class GraphicPanel {

    public JPanel topPanel;

    public GraphicPanel() {
        JPanel Panel = new JPanel();
        Panel.setLayout(new GridLayout(1, 1));

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("a", Panel);
        tabbedPane.addTab("b", Panel);
        tabbedPane.addTab("c", Panel);
        tabbedPane.addTab("d", Panel );
        tabbedPane.addTab("e", Panel  );

        topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(1, 1));
        topPanel.add(tabbedPane);
    }

    public JPanel getPanel(){
        return topPanel;
    }
}

1 个答案:

答案 0 :(得分:5)

如果要在JPanel

中显示,则必须创建JTabbedPane的新实例

试试这段代码:

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("a", new Panel());
tabbedPane.addTab("b", new Panel());
tabbedPane.addTab("c", new Panel());
tabbedPane.addTab("d", new Panel());
tabbedPane.addTab("e", new Panel());