简体   繁体   English

一个JLabel的setIcon与一个JLabel数组之间的区别

[英]Differences between setIcon for one JLabel vs an array of JLabels

I'm trying to design a very simple graphic interface where I will be adding some images to using a JLabel loaded with an Icon: 我正在尝试设计一个非常简单的图形界面,在其中我将使用加载有Icon的JLabel向其中添加一些图像:

JFrame Table = new JFrame("Transporter Room");
Table.setSize(600, 600);
Table.setLocationRelativeTo(null);
Table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Table.setLayout(new GridLayout(2, 2, 20, 20));

My problem is loading the icons for an array of JLabels... Something like this works: 我的问题是为JLabels数组加载图标...像这样的东西:

JLabel playingCard = new JLabel(CardGUI.getIcon(myCard));
JLabel playingCard2 = new JLabel(CardGUI.getIcon(myCard2));
Table.add(playingCard);
Table.add(playingCard2);

Just a note, the CardGUI.getIcon(ob) is just a static method that sets up the directory correctly and loads up an Icon with a given card object. 请注意, CardGUI.getIcon(ob)只是一个静态方法,可以正确设置目录并使用给定的卡对象加载Icon。

Something like this also works (although the icon is not centered in the layout.. why?) : 这样的事情也可以工作(尽管图标不在布局的中心。为什么?)

JLabel playingCard = new JLabel();
JLabel playingCard2 = new JLabel();
playingCard.setIcon(CardGUI.getIcon(myCard));
playingCard2.setIcon(CardGUI.getIcon(myCard));
Table.add(playingCard);
Table.add(playingCard2);

Using arrays of JLabel also works as long as I don't use setIcon() but dup my code to declare twice: 只要不使用setIcon()但使用JLabel数组也可以,但是将我的代码setIcon()两次以声明:

JLabel[] myLabel = new JLabel[2];
myLabel[0] = new JLabel(CardGUI.getIcon(myCard));
myLabel[1] = new JLabel(CardGUI.getIcon(myCard2));
Table.add(myLabel[0]);
Table.add(myLabel[1]);

However, the problem arises when I try to do this (and I need to do this per spec): 但是,当我尝试执行此操作时会出现问题(并且我需要按照规格进行操作):

JLabel[] myLabel = new JLabel[2];
myLabel[0].setIcon(CardGUI.getIcon(myCard));
myLabel[1].setIcon(CardGUI.getIcon(myCard2));
Table.add(myLabel[0]);
Table.add(myLabel[1]);

I get a Execption in thread "main" java.lang.NullPointerException . Execption in thread "main" java.lang.NullPointerException得到了执行Execption in thread "main" java.lang.NullPointerException I have no idea why. 我不知道为什么。 I'm pretty sure the directory to the image is perfectly fine (that's taken care of by another class). 我很确定图像的目录是完美的(由另一个类负责)。 Why does this only come up if a JLabel within an array is attempted to be set up? 为什么只有在尝试在数组中设置JLabel时才会出现这种情况?

This is an assignment I'm working on, and I prefer not using any new code that might be potentially be longer as a work around. 这是我正在从事的一项工作,我不希望使用任何新代码,因为这些新代码可能会更长一些。 It should be rather straightforward, if I can get pass this point, I literally use a for loop to load up Icons to JLabels, then another for loop to load up all the JLabels onto a table top (that's another Class I have already set up). 这应该很简单,如果我能通过这一点,我会使用一个for循环将图标加载到JLabels,然后使用另一个for循环将所有JLabel加载到桌面上(这是我已经设置的另一个Class )。 Any help would be greatly appreciated! 任何帮助将不胜感激!

When you create a JLabel[] , you're creating the array of labels. 创建JLabel[] ,将创建标签数组 NOT the labels inside the array. 不是数组的标签。 In other words, the array elements are still null after you create the array. 换句话说,创建数组后,数组元素仍为null That's why your second to last bit of code works, but your last didn't -- in the last bit of code, you didn't initialize the elements in the array, so when you try to call a method on them, you get a NullPointerException . 这就是为什么倒数第二个代码有效的原因,但倒数第二个代码却无效—在最后的代码中,您没有初始化数组中的元素,因此当您尝试对它们调用方法时, NullPointerException

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

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