嗨,我尝试使用 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);
}
发生这种情况是因为您使用null
LayoutManager
.因此,setLayout(null);
删除该行,所有行都将正常工作,或将该行更改为非空值,例如:
setLayout(new BorderLayout());
.
我建议您不要使用空布局。检查LayoutManager
教程。例如尝试BorderLayout
或其他。
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 扩展您的课程。