lundi 21 avril 2014

Java - moissonneuses nom de peut JLable données int ? -Débordement de pile


i know that first code is correct, and the second code is not correct. if i want to insert i into JLabel. how can it be achieve?


JLabel label_1 = new JLabel();
int i = 1; JLabel label_i = new JLabel();



This can't be done in Java. You could use arrays:


JLabel[] array = new JLabel[2];
array[0] = new JLabel();
array[1] = new JLabel();

or Collections, for example an ArrayList:


List<JLabel> arrayList = new ArrayList<JLabel>();
arrayList.add(new JLabel());
// ...
arrayList.get(0).setText("...");



int i = 1;    
JLabel label_i = new JLabel(String.valueOf(i));



If your question refers to "dynamic variable names" where variable names can be specified by using other variables such as :


String x1 = "stuff";

int w = 2;

String x"w" = "stuff 2";

where "w" refers to the variable value of w (2) then that cannot be done directly in java.


However, arrays and collections can be used instead of this:


//JLabel Array of 5 elements
JLabel[] labels = new JLabel[5];

int index = 3;

labels[index] = ... //Accesses and assigns index 3

Similarly, this can be done with an ArrayList if you want an growable array:


ArrayList<JLabel> labels = new ArrayList<>(); //New empty arraylist of JLabels

//Populate arraylist
labels.add(new JLabel());

int index = 0;

labels.get(index) = ...

If you live on the bleeding-edge, you may want to consider reading this which shows Java 8's somewhat limited (but still possible) ability to get individual variable names via reflection.



i know that first code is correct, and the second code is not correct. if i want to insert i into JLabel. how can it be achieve?


JLabel label_1 = new JLabel();
int i = 1; JLabel label_i = new JLabel();


This can't be done in Java. You could use arrays:


JLabel[] array = new JLabel[2];
array[0] = new JLabel();
array[1] = new JLabel();

or Collections, for example an ArrayList:


List<JLabel> arrayList = new ArrayList<JLabel>();
arrayList.add(new JLabel());
// ...
arrayList.get(0).setText("...");


int i = 1;    
JLabel label_i = new JLabel(String.valueOf(i));


If your question refers to "dynamic variable names" where variable names can be specified by using other variables such as :


String x1 = "stuff";

int w = 2;

String x"w" = "stuff 2";

where "w" refers to the variable value of w (2) then that cannot be done directly in java.


However, arrays and collections can be used instead of this:


//JLabel Array of 5 elements
JLabel[] labels = new JLabel[5];

int index = 3;

labels[index] = ... //Accesses and assigns index 3

Similarly, this can be done with an ArrayList if you want an growable array:


ArrayList<JLabel> labels = new ArrayList<>(); //New empty arraylist of JLabels

//Populate arraylist
labels.add(new JLabel());

int index = 0;

labels.get(index) = ...

If you live on the bleeding-edge, you may want to consider reading this which shows Java 8's somewhat limited (but still possible) ability to get individual variable names via reflection.


0 commentaires:

Enregistrer un commentaire