【问题标题】:How would I access multiple variables as a whole or surplus? (Java)我将如何访问多个变量作为一个整体或盈余? (爪哇)
【发布时间】: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 然后执行您想要对该列表执行的操作,例如将工具提示设置为“列按钮”。希望你能明白。

标签: java variables button


【解决方案1】:

您好,我认为您可以将所有这些按钮放在一个数组中,然后像您想要的那样遍历您的数组,考虑到您的数组的索引,您可以对不同的对象执行您想要的操作;)

类似这样的:

    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

【讨论】:

  • 这是一个绝妙的主意。所以,如果我想修改一个特定的按钮,我会通过buttons[x]访问它吗?按钮的布局,每个都通过单独的 x 和 y 坐标进行更改,会不会只是另一个 for 循环?即对于 int x,另一个对于 int y 并相应地更改?
  • 是的,你可以通过buttons[index]访问你所有的按钮;)对于布局我真的不知道你想做什么,我让你试试;)
  • @Munkeeface 根据您在问题中的设置,您不需要 for 循环中的 for 循环。您只需要在循环按钮的循环中更新您的 x 和 y 即可。例如for(int i = 0; i &lt; buttons.length; i++, x++)for(int i = 0; i &lt; buttons.length; i++) { buttonstuff; x++}
  • @Asthor,作为x++i++,我假设x 将是先前建立的变量(int x = 0),因为for 循环建立i。话虽如此,如果xi 都单独增加1,那对坐标有何影响?比如第一个例子button1.setBounds...
  • @Munkeeface 是的。我只是以 x++ 为例。根据您的问题,x 开始时可能是 25,y 可能是 50,然后是 x += 147y += 122,当 i % 3 = 0 时。当i % 3 = 0 时,您还将 x 重置为初始值 25。(我是您在按钮上循环的计数器)
【解决方案2】:

正如@LostKatana 所说,我认为布局将是设置按钮位置的更好方法。 here 你会发现如何使用它们(如果你还不知道的话)。

【讨论】:

  • 我不一定指的是按钮的布局,而是如何修改它们中的每一个,或者一次性修改它们而不必单独引用它们......即如果我想编辑按钮 1-5 的 toolTipText 而不是其余按钮,一组不起作用,不是吗?我将如何修改那些特定的集合但有剩余?这件事的问题不是布局本身,而是或多或少的对倍数的修改。我将在问题中展示另一个示例以澄清
猜你喜欢
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多