![](/img/trans.png)
[英]How would I create methods that have access to the variables in main without parameters? - Java
[英]How would I access multiple variables as a whole or surplus? (Java)
我在制作多個變量(例如按鈕)以及必須分別定義,訪問或修改它們時遇到了問題。 我已經多次搜索如何訪問多個變量而又不浪費太多空間。 如您所料,我可能只是在說錯這句話。
我所指的示例:
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以及按鈕6-button9? 我希望這有幫助
嗨,我想您可以將所有這些按鈕放在一個數組中,然后遍歷整個數組,就像您想考慮數組的索引一樣,您可以對不同的對象進行操作;)
像這樣的東西:
JButton buttons[] = {new JButton(), new JButton(), new JButton(),new JButton(), new JButton(), new JButton(),new JButton(), new JButton(), new JButton()};
for(int i = 0; i < buttons.length; i++)
{
if( i < 5 )
buttons[i].setToolTipText("Click here to check for the missing Flight 307");
else
buttons[i].setToolTipText("Is it here?");
}
您只需要在選項卡中放置自己的JButton
。
正如@LostKatana所說,我認為布局是設置按鈕位置的更好方法。 在這里您將找到如何使用它們(如果您還不知道)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.