我有一个JFrame
,主要由左侧和右侧组成。左边有两个组件1在另一个上方,右边有9个,另外一个在另一个上面。由于左边有2,我希望1个分量与右边的6相同。我使用的是gridBagConstraints
布局,并且每个JPanel
都位于主JFrame
中。一切看起来都不错(除了我刚才所说的)。要对此问题进行排序,请在左侧的JPanel上使用c.gridHeight = 6
。然而,这会将6 JPanels
放在彼此的右上方,忽略它们的格式。我该如何纠正这个问题?我的代码的Snipets仍然会导致同样的问题:
void create()
{
JFrame screen = new JFrame("Dungeon of Doom");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
screen.setSize(new Dimension(screenWidth, screenHeight));
screen.setPreferredSize(new Dimension(screenWidth, screenHeight));
//Add all JPanes to screen
c.gridy = 0;
c.gridx = 0;
c.gridheight = 6;
screen.add(lookReply(), c);
c.gridy = 0;
c.gridx = 1;
screen.add(title(), c);
c.gridy = 1;
c.gridx = 1;
screen.add(space(), c);
c.gridy = 2;
c.gridx = 1;
screen.add(commands(), c);
//...So on, same for others
}
示例方法 - JPanel - 所有方法都具有相同的内容,只是不同的标题和更少/更多的按钮/标签
private JPanel title()
{
JPanel titlePanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
titlePanel.setBackground(Color.red);
titlePanel.setOpaque(true);
JLabel title = new JLabel("DOD");
titlePanel.add(title, c);
return titlePanel;
}
我在JPanels上添加了一个背景,以帮助他们了解它们在窗口中的位置
所有帮助表示赞赏!谢谢:))