【发布时间】:2016-12-20 14:24:18
【问题描述】:
StackOverFlow 问题
您好今天第二次专用堆栈溢出用户! XD
因此,我尝试使用 .setBounds 以网格格式设置这 9 个按钮的位置,该格式接受 (xCoordinate、yCoordinate、#ofPixelsWide、#ofPixelsTall)
有人知道更有效/更紧凑的方法吗?就算不用.setBounds我也想知道,毕竟我是来学的XD
感谢您的任何建议
for (int i = 0; i < groupOfButtons.length; i++) {
int x = 0, y = 0;
if (i == 1 || i == 4 || i == 7) {
x = 110;
}
if (i == 2 || i == 5 || i == 8) {
x= 220;
}
if (i > 2 && i < 6) {
y = 110;
}
if (i > 5 && i < 9) {
y = 220;
}
groupOfButtons[i].setBounds(x, y, 100, 100);
}
这不是写这个顺便说一句(这种方式实际上更短但看起来更凌乱):
groupOfButtons[0].setBounds(0, 0, 100, 100);
groupOfButtons[1].setBounds(110, 0, 100, 100);
groupOfButtons[2].setBounds(220, 0, 100, 100);
groupOfButtons[3].setBounds(0, 110, 100, 100);
groupOfButtons[4].setBounds(110, 110, 100, 100);
groupOfButtons[5].setBounds(220, 110, 100, 100);
groupOfButtons[6].setBounds(0, 220, 100, 100);
groupOfButtons[7].setBounds(110, 220, 100, 100);
groupOfButtons[8].setBounds(220, 220, 100, 100);
【问题讨论】:
-
这正是布局管理器的用途——你让自己的事情变得太难了。
-
GridLayout 在这里会很完美。
-
@HovercraftFullOfEels 哇 2 个问题,它们都被标记为重复,对不起,当我搜索我使用的术语时,我使用的术语是如此间接,正确的相关文章不会弹出 XD
-
阿奇,太阳底下很少有新鲜事。至于您对空布局的使用,请了解虽然空布局和
setBounds()对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,在使用它们时遇到的困难就越严重.当 GUI 调整大小时,它们不会调整您的组件大小,它们是增强或维护的皇家女巫,放置在滚动窗格中时它们完全失败,在与原始不同的所有平台或屏幕分辨率上查看时它们看起来很糟糕.
标签: java