在Java中设置JLabel数组中的颜色值。

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

setting a color value in the jlabel[] in java

问题

I found out that you can't do setColor to the Label I assigned. It tells me to put the length and put a variableDeclaration. However, I do not know ho to do that.. How do you assign a font type or a color for "JLabel[] answerLabels;"

我发现无法为我分配的标签设置颜色。它告诉我要放置长度并放置一个变量声明。但是,我不知道该如何做。如何为 "JLabel[] answerLabels;" 分配字体类型或颜色?

英文:

While I was writing a code, I found out that you can't do setColor to the Label I assigned. It tells me to put the length and put a variableDeclaration. However, I do not know ho to do that.. How do you assign a font type or a color for "JLabel[] answerLabels;"?

    JLabel[] answerLabels;
    answerLabels = new JLabel[question.getAnswers().length]; 
    answerLabels.length[new Font("PT Serif",Font.BOLD,16)];

答案1

得分: 2

我建议你学习更多关于数组的知识。

看,你有一个JLabel的数组(将其视为一个列表),而不是单个JLabel

JLabel[] answerLabels;

JLabel之后的两个方括号[]表示你正在创建一个数组,而不是单个JLabel

因此,当你调用以下代码时:

answerLabels = new JLabel[question.getAnswers().length];
answerLabels[i].setForeground(Color.RED);

你不是在引用一个JLabel,而是在引用一个数组。由于数组没有诸如setForeground()setFont()之类的方法,你的程序会出现错误。

所以,如果你想设置特定JLabel的颜色或字体,你必须引用它。
要引用该JLabel,你需要编写如下代码:

answerLabels[i].setForeground(Color.RED);

这里,'i'表示你想要访问的JLabel的编号。假设你想要访问第三个JLabel,那么你需要在上面的代码中的'i'的位置写入2。

为什么是2,而不是3?因为数组从0开始。

请注意,你尝试访问的JLabel在设置其颜色或字体之前应该进行初始化,例如:

answerLabels[i] = new JLabel();

否则,你的程序将抛出NullPointerException异常。所以,请确保在设置其颜色或字体之前初始化从数组中访问的JLabel

英文:

I recommend you to learn more about arrays.

Look mate, you have an array(think of it as a list) of JLabel. Not a single JLabel

JLabel[] answerLabels;

The two square brackets[] after JLabel represent that you are creating an array, not a single JLabel.

Therefore, when you call,

answerLabels = new JLabel[question.getAnswers().length]; 
answerLabels.length[new Font("PT Serif",Font.BOLD,16)];

you are not referring to a JLabel but you are referring to an array. Since, array doesn't have any methods such as, setForeground() or setFont(), your program will run into an error.

So if you want to set the color or font of a particular JLabel, you have to refer to it.
To refer to that JLabel, you have to write code as follows :

answerLabels[i].setForeground(Color.RED); 

Here, 'i' represents the number of JLabel you want to access. Suppose, you want to access the third JLabel, then you have to write 2 in the place of 'i' in the above line.

Why 2, why not 3? Because arrays start at 0.

Note that, the JLabel you are trying to access should be initialized before setting it's color or font by something like,

answerLabels[i] = new JLabel();

Otherwise, your program will throw NullPointerException. So, make sure to initialize the JLabel you are accessing from the array before setting it's color or font.

答案2

得分: 1

看起来你正在尝试做类似这样的事情:

JLabel[] answerLabels = new JLabel[question.getAnswers().length];
// 这将循环遍历所有的JLabel并设置字体和颜色
for(int i=0; i<answerLabels.length; i++) {
    answerLabels[i] = new JLabel("来自问题/答案的文本");
    answerLabels[i].setFont(new Font("PT Serif",Font.BOLD,16));
    answerLabels[i].setForeground(Color.red);
}
英文:

It looks like your trying to do something like this:

    JLabel[] answerLabels = new JLabel[question.getAnswers().length];
    // This will loop through all of the JLabels and set the font and color
    for(int i=0; i&lt;answerLabels.length; i++) {
        answerLabels[i] = new JLabel(&quot;Text from question/answer&quot;);
        answerLabels[i].setFont(new Font(&quot;PT Serif&quot;,Font.BOLD,16));
        answerLabels[i].setForeground(Color.red);
    }

huangapple
  • 本文由 发表于 2020年7月29日 11:10:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63145768.html
匿名

发表评论

匿名网友

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

确定