0

嗨,我尝试使用 for 循环运行 JTabbedPanel。我的程序运行成功。但它没有像我预期的那样创建任何 Tab 文件。请帮我。

public void GenerateGUI() {

    jPanel = new JPanel[3];

    jSplitPane1 = new JSplitPane();
    jTabbedPane1 = new JTabbedPane();

    GroupLayout[] jPanelLayout = new GroupLayout[3];

    for (int i = 0; i <= noOfTerminals; i++) {

        jPanel[i] = new JPanel();

        jPanelLayout[i] = new GroupLayout(jPanel[i]);
        jPanel[i].setLayout(jPanelLayout[i]);

        jPanelLayout[i].setHorizontalGroup(jPanelLayout[i]
                .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING).addGap(
                        0, 449, Short.MAX_VALUE));
        jPanelLayout[i].setVerticalGroup(jPanelLayout[i]
                .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING).addGap(
                        0, 250, Short.MAX_VALUE));

        jPanel[i].add(lblOverView);

        jTabbedPane1.addTab("OverView", jPanel[i]);
    }

    setLayout(null);
    add(jTabbedPane1);

    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    pack();
    setVisible(true);
    setLocationRelativeTo(null);
}
4

2 回答 2

3

发生这种情况是因为您使用null LayoutManager. 因此,删除该行setLayout(null);,一切都会起作用,或者将该行更改为非空值,例如:

setLayout(new BorderLayout());.

我建议你不要使用空布局。检查LayoutManager教程。尝试例如BorderLayout或其他。

于 2014-01-10T10:47:17.687 回答
0
    public GenerateGUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBounds(10, 11, 414, 240);
    contentPane.add(tabbedPane);

    JPanel[] panels = new JPanel[3];

    for (int i = 0; i < 3; i++){
        panels[i] = new JPanel();
        panels[i].setVisible(true);
        tabbedPane.addTab("Tab " + Integer.toString(i), null, panels[i], null);
    }
}

使用 JFrame 扩展您的课程。

于 2014-01-10T11:05:33.803 回答