Java 使用GridLayout垂直对齐JPanel组件
我正在使用JavaSwing库将KenKen作为我的学期项目。对于对齐,我已经使用了gridbag和gridlayout,但是现在我想在UI中再添加一个JPanel组件。这些屏幕截图将使问题更加清楚: 现在,我在最左侧的面板中选择要向其中添加相应候选对象的网格单元 它会干扰栅格和配电盘的相邻对齐 以下是面板及其各自的布局:Java 使用GridLayout垂直对齐JPanel组件,java,swing,grid-layout,Java,Swing,Grid Layout,我正在使用JavaSwing库将KenKen作为我的学期项目。对于对齐,我已经使用了gridbag和gridlayout,但是现在我想在UI中再添加一个JPanel组件。这些屏幕截图将使问题更加清楚: 现在,我在最左侧的面板中选择要向其中添加相应候选对象的网格单元 它会干扰栅格和配电盘的相邻对齐 以下是面板及其各自的布局: JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(1, 4, 5,
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 4, 5, 5));
buttonPanel.setPreferredSize(new Dimension(20,40));
buttonPanel.add(undoButton);
buttonPanel.add(redoButton);
buttonPanel.add(eraseButton);
buttonPanel.add(hintButton);
JPanel cellPanel = new JPanel();
cellPanel.setName("cellPanel");
cellPanel.setLayout(new GridLayout(pSize, pSize, 0, 0));
JPanel numPanel = new JPanel();
numPanel.setName("numPanel");
numPanel.setLayout(new GridLayout(1,1,5,5));
numPanel.setPreferredSize((new Dimension(50,60)));
JPanel candPanel = new JPanel();
candPanel.setName("candidatesPanel");
JLabel candidates = new JLabel("Candidates");
candidates.setFont(new Font("Courier New", Font.ITALIC, 14));
candidates.setForeground(Color.GRAY);
candPanel.setLayout(new GridLayout(0,1));
candPanel.add(candidates);
然后全部进入内容面板:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(candPanel, pos.nextRow());
content.add(new Gap(GAP) , pos.nextRow()); // Add a gap below
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());
这些按钮都是在运行时生成的,它们被添加到动作侦听器中的candPanel中。您似乎使用了我不知道的GridBagConstraints子类(变量
pos
),尽管我可以从上下文猜出它的功能
假设您的问题是希望候选者
面板位于单元格面板的左侧,而不是上方,则需要按如下方式交换添加candPanel
和新间隙(间隙)
的行:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(new Gap(GAP), pos.nextRow()); // These two lines
content.add(candPanel, pos.nextRow()); // swapped over
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());
“我将在下一篇文章中分享代码。”为了更快地获得更好的帮助,请发布一篇文章(作为对本文的编辑)。顺便说一句-从未听说过“作物”?Alt Print Screen将屏幕截图裁剪为仅活动GUI。请参阅(有关制作优秀截图的提示)。我不知道你为什么回复我,告诉我你的编辑。我仍在等待SSCCE。如果您提供完整的源代码来帮助调试和更好地了解情况,更多的人将愿意提供帮助。