在Java中,是否可以获取jButton的变量名?

huangapple go评论90阅读模式
英文:

Java Is it possible to get the Variable name of a jButton?

问题

我想不出其他的做法了我正在尝试获取已经声明的 Java 按钮的变量名以便我可以循环遍历数据库该数据库具有一个屏幕字段用于访问正确的产品),并将所需的文本设置为那个 jButton我可以通过手动设置每个 jButton 的文本或通过 Netbeans GUI 设置硬编码文本来实现这一点但我想知道是否有更有效的方法来做到这一点

# 可以实现这个的代码 #

        ArrayList<Products> myList = myProductsDataHandler.getAllProducts();
        myList.forEach((var i) -> {
            if (i.getScreen() == 0) {
                btnProduct1.setText(i.getProductName());
            } else if (i.getScreen() == 1) {
                btnProduct2.setText(i.getProductName());
            } else if (i.getScreen() == 2) {
                btnProduct3.setText(i.getProductName());
            } else if (i.getScreen() == 3) {
                btnProduct4.setText(i.getProductName());
            } else if (i.getScreen() == 4) {
                btnProduct5.setText(i.getProductName());
            } else if (i.getScreen() == 5) {
                btnProduct6.setText(i.getProductName());
            } else if (i.getScreen() == 6) {
                btnProduct7.setText(i.getProductName());
            } else if (i.getScreen() == 7) {
                btnProduct8.setText(i.getProductName());
            } else if (i.getScreen() == 8) {
                btnProduct9.setText(i.getProductName());
            }
        });

# 我需要帮助的代码 #

        int btnInt = 1;
        String btnStr = "btnProduct";
        btnCatagory1.setText(i.getCategory());
        Component[] components = pnlOrder.getComponents();
        for (Component component : components) {
            if (component instanceof JButton) {
                System.out.println(btnStr + btnInt);
                JButton button = (JButton) component;
                System.out.println(button.getName());
                if (button.getName().contains(btnStr + btnInt)) {
                    btnProduct1.setText(i.getProductName());
                    btnInt++;
                }
            }
        }
    });

这是关于你提供的代码的翻译,只包含了翻译后的内容,没有其他附加内容。如果你还有其他需要翻译的内容或问题,请继续提问。

英文:

I can't come up with any other way of doing this. I'm trying to get the variable name of a java button which I've already declared so I can loop through a Database (Which has a screen field to access the correct product) and set the desired text to that jButton. I can do this by manually setting each jButtons text or setting a hard codded text through the Netbeans GUI. But I was looking to see if there's a more efficient way of doing this.

Code that can achieve this

    ArrayList&lt;Products&gt; myList = myProductsDataHandler.getAllProducts();
myList.forEach((var i) -&gt;
{
if (i.getScreen() == 0)
{
btnProduct1.setText(i.getProductName());
}
else if (i.getScreen() == 1)
{
btnProduct2.setText(i.getProductName());
}
else if (i.getScreen() == 2)
{
btnProduct3.setText(i.getProductName());
}
else if (i.getScreen() == 3)
{
btnProduct4.setText(i.getProductName());
}
else if (i.getScreen() == 4)
{
btnProduct5.setText(i.getProductName());
}
else if (i.getScreen() == 5)
{
btnProduct6.setText(i.getProductName());
}
else if (i.getScreen() == 6)
{
btnProduct7.setText(i.getProductName());
}
else if (i.getScreen() == 7)
{
btnProduct8.setText(i.getProductName());
}
else if (i.getScreen() == 8)
{
btnProduct9.setText(i.getProductName());
}

Code I need help with

        int btnInt = 1;
String btnStr = &quot;btnProduct&quot;;
btnCatagory1.setText(i.getCategory());
Component[] components = pnlOrder.getComponents();
for(Component component : components)
{
if(component instanceof JButton)
{
System.out.println(btnStr+btnInt);
JButton button = (JButton) component;
System.out.println(button.getName());
if (button.getName().contains(btnStr+btnInt))
{
btnProduct1.setText(i.getProductName());
btnInt++;
}
}
}
});

It's an Epos system and when the program is executed I want to get texts from the Database and set it to desired buttons. There are only 9 buttons. I have tried getName() but it returns null. Is there any other I can achieve this. Thanks

答案1

得分: 2

当您创建按钮时,还可以将每个按钮添加到一个ArrayList中:

List<JButton> buttons = new ArrayList<JButton>();
buttons.add(btnProduct1);
buttons.add(btnProduct2);

然后,当您想要更新按钮上的文本时,只需使用以下代码:

JButton button = buttons.get(i.getScreen());
button.setText(i.getProductName());
英文:

When you create your buttons you can also add each button to an ArrayList:

List&lt;JButton&gt; buttons = new ArrayList&lt;JButton&gt;();
buttons.add(btnProduct1);
buttons.add(btnProduct2);

Then when you want to update the text on the button you simply use:

JButton button = buttons.get( i.getScreen() );
button.setText( i.getProductName() );

答案2

得分: 1

你使用了setText(),因此应该使用getText()。

如果你使用了setName(),你将会使用getName()。

https://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/AbstractButton.html#getText()

英文:

You used setText() therefore should be using getText().

Had you used setName() you would use getName().

https://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/AbstractButton.html#getText()

huangapple
  • 本文由 发表于 2020年4月6日 06:11:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61050210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定