Running JTabbedPanel on for loop in java
-
21-12-2019 - |
質問
Hi I tried to run JTabbedPanel using for loop.. My program is running successfully. But its not creating any Tab file as what i expected. please help me.
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);
}
解決
That happens because you use null
LayoutManager
. So, remove that line setLayout(null);
and all will work, or change that line to not null value, for example:
setLayout(new BorderLayout());
.
I recommend you don't use null Layout. Examine LayoutManager
tutorial. Try for example BorderLayout
or other.
他のヒント
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);
}
}
Extend your class with JFrame.
所属していません StackOverflow