【发布时间】:2014-05-03 19:22:27
【问题描述】:
我在制作多个变量(例如按钮)时遇到了问题,并且必须分别定义、访问或修改它们。我已经多次搜索如何访问多个变量而不浪费这么多空间。如您所料,我可能只是措辞错误。
我所指的示例:
button1.setBounds(25 + insets.left, 50 + insets.top,
size.width + 75, size.height + 75);
button2.setBounds(172 + insets.left, 50 + insets.top,
size.width + 75, size.height + 75);
button3.setBounds(319 + insets.left, 50 + insets.top,
size.width + 75, size.height + 75);
button4.setBounds(25 + insets.left, 172 + insets.top,
size.width + 75, size.height + 75);
button5.setBounds(172 + insets.left, 172 + insets.top,
size.width + 75, size.height + 75);
button6.setBounds(319 + insets.left, 172 + insets.top,
size.width + 75, size.height + 75);
button7.setBounds(25 + insets.left, 294 + insets.top,
size.width + 75, size.height + 75);
button8.setBounds(172 + insets.left, 294 + insets.top,
size.width + 75, size.height + 75);
button9.setBounds(319 + insets.left, 294 + insets.top,
size.width + 75, size.height + 75);
我正在考虑一个 for 循环,让 int x 跟随变量,但它不会被正确引用。
* 编辑 ** 我不是专门指按钮的布局...我指的是如何一次修改多个变量,即 Button1-9 而不必单独引用它们。
button1.setToolTipText("Click here to check for the missing Flight 307");
button2.setToolTipText("Click here to check for the missing Flight 307");
button3.setToolTipText("Click here to check for the missing Flight 307");
button4.setToolTipText("Click here to check for the missing Flight 307");
button5.setToolTipText("Click here to check for the missing Flight 307");
button6.setToolTipText("Is it here?");
button7.setToolTipText("Is it here?");
button8.setToolTipText("Is it here?");
button9.setToolTipText("Is it here?");
如何修改 button1-button5 和 button6-button9?我希望这会有所帮助
【问题讨论】:
-
好吧,从外观上看,您的数字似乎每次跳跃的数量都相同。您可以轻松地将它们变成一个循环,然后根据需要更新它们。第一个数字是 147,第二个是 122。这取决于您如何创建 button1-9 变量。
-
我不太确定我是否理解正确,但是包含按钮的布局不是调整位置的更好方法吗?
-
@Asthor,至于循环,我怎么能这样表达?我想如果我只是使用 (x 作为变量),buttonx 本身会作为一个单独的、未实例化的变量引用?
-
@LostKatana,我正在考虑使用基本的 gridLayout 或 gridBagLayout,但我也参考了如果我想通过单个数字(button1 和 button3)定义多个变量并在其名称中分隔并成为能够定义它们,或共同修改它们,而不必一遍又一遍地镜像同一条线,浪费空间。我要问的不仅仅是按钮的位置,而是任何东西。也许设置 toolTipText 或其他基本属性。谢谢,如果我需要澄清更多,我可以提供其他紧迫的例子
-
我的意思是您根据列表的用途将按钮添加到列表中。就像您想为 3x3 有序按钮字段的第一列设置边框一样,您可以拥有一个名为例如的列表。
firstColumnButtons然后执行您想要对该列表执行的操作,例如将工具提示设置为“列按钮”。希望你能明白。