简体   繁体   English

在Java变量名的末尾附加一个数字

[英]Append a number in the end of a variable name in java

Suppose that I have a lot of variables defined in my code with names such as this 假设我在代码中定义了很多这样的变量

public javax.swing.JPanel BenderPanel1;
public javax.swing.JPanel BenderPanel2;
public javax.swing.JPanel BenderPanel3;
etc...

So their general type is like this: BenderPanel"NUMBER" . 因此,它们的一般类型是这样的: BenderPanel"NUMBER"

I want to access some of them and set their visibility with .setVisible(false); 我想访问其中的一些,并使用.setVisible(false);设置它们的可见性.setVisible(false); but the number of those panels which I want to access is user-defined on run time. 但是我要访问的面板数是用户在运行时定义的。

Is there any possible way through a library to append a number to the end of each variable in order to access it in a for loop, like this: 是否有可能通过库将数字添加到每个变量的末尾,以便在for循环中访问它,如下所示:

for (int i=1; i<=UserInput; i++)
{
     BenderPanel"i".setVisible(false); // Watch this "i" right there.
}

WITHOUT the need to add them on ArrayList first and do it with the obvious way? 无需先将它们添加到ArrayList ,并以明显的方式进行操作?

You can't create members dynamically in Java (you can access them dynamically via reflection, but there's no need for it here). 您无法在Java中动态创建成员( 可以通过反射来动态访问成员,但是这里不需要它)。

Rather than having 而不是拥有

public javax.swing.JPanel BenderPanel1;
public javax.swing.JPanel BenderPanel2;
public javax.swing.JPanel BenderPanel3;

have

public javax.swing.JPanel[] BenderPanels;

or 要么

public List<javax.swing.JPanel> BenderPanels;

Then you can loop through them with an enhanced for loop. 然后,您可以使用增强的for循环遍历它们。

for (javax.swing.JPanel panel : BenderPanels) {
    // ...
}

If you really do not want to store your objects in a data structure like eg an ArrayList, I would recommend to use the Reflection API . 如果您确实不想将对象存储在ArrayList之类的数据结构中,则建议使用Reflection API

Especially interesting for you should be the fields . 对您来说特别有趣的应该是田野

Btw: According to the Java Naming Conventions , variable names shouldn't start with capital letters. 顺便说一句:根据Java命名约定 ,变量名不应以大写字母开头。

I'll provide you an idea about using Reflection : 我将为您提供有关使用Reflection的想法:

public class YourClassContainer extends ... {

public javax.swing.JPanel BenderPanel1;
public javax.swing.JPanel BenderPanel2;
public javax.swing.JPanel BenderPanel3;
....
public javax.swing.JPanel BenderPanelXXX;

....

//to access all of them:

for (Field field : YourClassContainer.class.getFields()) {
    if (field.getName().startsWith("BenderPanel")) {
        ((javax.swing.JPanel)field.get(YourClassContainer.this)).setVisible(false);
    }
}

}

Use Reflection in that Way: 以这种方式使用反射:

YourClasss instance; // this instance has those all JPanel fields
Method setVisible = JPanel.class.getMethod("setVisible", new Class[] {Boolean.class});

List<String> numberOfFieldList = getNumbersOfFieldsToSetInvisible(); 

for (String number : numerOfFieldList) {
  Field benderPanelField = YourClass.getField("BenderPanel" + number);
  Object fieldInYourInstance = benderPanelField.get(instance);
  setVisible.invoke(fieldInYourInstance, Boolean.FALSE);
}

Maybe it will help. 也许会有所帮助。

Simple example using your setup: 使用设置的简单示例:

    List<Integer> visibleItems = new ArrayList<Integer>();

    JPanel[] myPanels = new JPanel[]{BenderPanel1, BenderPanel2, BenderPanel3};

    for (int i = 0; i < myPanels .length; i++) {
        myPanels[i].setVisible(false);
        if(visibleItems.contains(i) ){
            myPanels[i].setVisible(true);
        } 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM