【发布时间】:2012-04-09 09:56:32
【问题描述】:
我正在使用 java swing 库将 KenKen 作为我的学期项目。为了对齐,我使用了 gridbag 和 gridlayout,但现在我想在 UI 中再添加一个 JPanel 组件。这些截图会让问题更清楚:
现在我在最左边的面板中选择要添加相应候选者的网格单元格。
它会扰乱网格和面板的相邻对齐方式。
以下是具有各自布局的面板:
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中。
【问题讨论】:
-
“我将在下一篇文章中分享代码。” 为了尽快获得更好的帮助,请发布SSCCE(作为对this的编辑邮政)。顺便说一句 - 从未听说过“作物”? Alt-Print Screen 将屏幕截图裁剪为仅活动的 GUI。请参阅How do I create screenshots?(有关制作出色屏幕截图的提示)。
-
我不知道你为什么回复我告诉我你的编辑。我还在等待SSCCE。
-
如果您提供完整的源代码以帮助调试和更好地了解情况,将会有更多人愿意提供帮助。
标签: java swing grid-layout