If I have many JLabels on the screen and I know their names how would I go about selecting finding them?
如果我在屏幕上有很多JLabel并且我知道他们的名字我将如何选择找到它们?
For example I know that I previously (dynamically) created a new JLabel with the name 2340. Is there something like
例如,我知道我之前(动态地)创建了一个名为2340的新JLabel。是否有类似的东西
JLabel image = findJlabel ("2340");
In order to select the JLabel component?
为了选择JLabel组件?
Thanks, Neco
谢谢,Neco
EDIT: Just want to show how I am creating these JLabels
编辑:只想展示我如何创建这些JLabel
// Initially creates all the imagelabels
public static void createImageLabels (){
int xcord, ycord;
JLabel image;
String imageLabelName;
xcord = 0;
ycord = yOffset;
for (int row = 0 ; row < map.length; row++) {
xcord = 0;
for (int col = 0 ; col < map[0].length; col++) {
imageLabelName = Integer.toString(row);
imageLabelName += Integer.toString(col);
image = new JLabel(new ImageIcon(space));
image.setName(imageLabelName);
image.setLocation(xcord, ycord);
image.setSize(24, 24);
imagePanel.add(image);
System.out.println ("Created a Jlabel Named "+imageLabelName);
xcord += 24;
}
ycord += 24;
}
}
I create a tile of imageIcons on the screen and then later on if I want to change the image displayed by them I want to select it and change it.
我在屏幕上创建了一个imageIcons图块,然后如果我想要更改它们显示的图像,我想选择它并进行更改。
3 个解决方案
#1
3
Well, assuming you have different names for all labels, i would recommend you using HashMaps.
好吧,假设你有所有标签的不同名称,我建议你使用HashMaps。
HashMap<String, JLabel> labels = new HashMap<String,JLabel>();
Inside you "for" sicle, use:
在你里面“为”sicle,使用:
labels.put("1233", new JLabel(new ImageIcon(space)));
To use your the label you want, use:
要使用您想要的标签,请使用:
labels.get("1233");
For more information, check:
有关更多信息,请检查:
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
#2
2
If you only want to find the component with the concept of the current container it easy, if you want to search child or parent containers, it becomes little more complicated...
如果你只想找到具有当前容器概念的组件,那么如果你想搜索子容器或父容器,它就会变得更加复杂......
public JLabel findLabelByName(Container parent, String name) {
JLabel found = null;
if (name != null) {
for (Component child : parent.getComponents()) {
if (child instanceof JLabel) {
JLabel label = (JLabel)child;
if (name.equals(label.getName()) {
found = label;
break;
}
}
}
}
return found;
}
Now, if you want to search up or down the container hierarchy, you would need to perform a recursive call to this method, passing in the new parent, but I'm sure you can figure that out, don't want to rob you of all the fun ;)
现在,如果要在容器层次结构中向上或向下搜索,则需要对此方法执行递归调用,传入新父级,但我确信您可以解决这个问题,不想抢劫您所有的乐趣;)
#3
0
In order to select the JLabel component?
为了选择JLabel组件?
how are you 'selecting' the label?
你是如何'选择'标签的?
if it's via the mouse, just add a mouseListener and use getSource()
如果它是通过鼠标,只需添加一个mouseListener并使用getSource()
#1
3
Well, assuming you have different names for all labels, i would recommend you using HashMaps.
好吧,假设你有所有标签的不同名称,我建议你使用HashMaps。
HashMap<String, JLabel> labels = new HashMap<String,JLabel>();
Inside you "for" sicle, use:
在你里面“为”sicle,使用:
labels.put("1233", new JLabel(new ImageIcon(space)));
To use your the label you want, use:
要使用您想要的标签,请使用:
labels.get("1233");
For more information, check:
有关更多信息,请检查:
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
#2
2
If you only want to find the component with the concept of the current container it easy, if you want to search child or parent containers, it becomes little more complicated...
如果你只想找到具有当前容器概念的组件,那么如果你想搜索子容器或父容器,它就会变得更加复杂......
public JLabel findLabelByName(Container parent, String name) {
JLabel found = null;
if (name != null) {
for (Component child : parent.getComponents()) {
if (child instanceof JLabel) {
JLabel label = (JLabel)child;
if (name.equals(label.getName()) {
found = label;
break;
}
}
}
}
return found;
}
Now, if you want to search up or down the container hierarchy, you would need to perform a recursive call to this method, passing in the new parent, but I'm sure you can figure that out, don't want to rob you of all the fun ;)
现在,如果要在容器层次结构中向上或向下搜索,则需要对此方法执行递归调用,传入新父级,但我确信您可以解决这个问题,不想抢劫您所有的乐趣;)
#3
0
In order to select the JLabel component?
为了选择JLabel组件?
how are you 'selecting' the label?
你是如何'选择'标签的?
if it's via the mouse, just add a mouseListener and use getSource()
如果它是通过鼠标,只需添加一个mouseListener并使用getSource()