简体   繁体   English

Java更改颜色JLabel

[英]Java change color JLabel

I know how to change a color in java from a JLabel object if you do it like this 我知道如何通过JLabel对象在Java中更改颜色

JLabel label = new JLabel("label text");
label.setForeground(Color.red);

but:: 但::

i create my JLabes dynamic like this 我像这样创建我的JLabes动态

center.add(new JLabel("Antwoord Vraag"+ (i +1) +": "+antwoord.get(i),SwingConstants.LEFT));

How could i change the color of a JLabel object without giving the object a name. 我如何在不给对象命名的情况下更改JLabel对象的颜色。

What you are looking for is a builder pattern for JLabel , which AFAIK does not exist. 您正在寻找的是JLabel构建器模式 ,而AFAIK不存在。 You can either create your own builder class, or extend JLabel with your own class that takes color as constructor argument (which is weird since JLabel has many properties, imagine what happens if each of them ahd it's own special constructor). 您可以创建自己的构建器类,也可以使用自己的以颜色作为构造器参数的类扩展JLabel (这很奇怪,因为JLabel具有许多属性,请想象如果每个属性都具有自己的特殊构造函数会发生什么情况)。

JLabelBuilder example: JLabelBuilder示例:

public class JLabelBuilder {
    private Color fColor;
    private String text;

    public void setForegroundColor(Color c) {
        fColor = c;
    }

    public void setText(String t) {
        text = t;
    }

    public JLabel build() {
        if (text != null && fColor != null) {
            JLabel label = new JLabel(text);
            label.setForeground(fColor);
            return label;
        } else {
            ...
        }
    }
}

Usage: 用法:

center.add(new JLabelBuilder()
                   .setText("Antwoord Vraag" + (i + 1) + ": " + antwoord.get(i))
                   .setForegroundColor(Color.red)
                   .build());

Anyway, I don't see the harm in two more lines of code. 无论如何,我再看不到两行代码的危害。 Explicitly declaring your object variable is also a good practice for readability reasons. 出于可读性原因,明确声明对象变量也是一种好习惯。 This will also work inside a loop: 这也将在循环内工作:

for(int i = 0 ; i < maxCounter ; i++) {
    String text = "Antwoord Vraag" + (i + 1) + ": " + antwoord.get(i);
    JLabel label = new JLabel(text, SwingConstants.LEFT);
    label.setForeground(Color.red);
    center.add(label);
} 

There are a couple of ways you can do this, but not with the standard JLabel - you'll need your own subclass or helper. 有两种方法可以执行此操作,但标准JLabel不行-您将需要自己的子类或帮助程序。 For example: 例如:

public class CLabel extends JLabel
{
    public CLabel(String text)
    {
        super(text);
    }

    public CLabel(String text, Color color)
    {
        super(text);
        setForeground(color);
    }

    public CLabel withColor(Color color)
    {
        setForeground(color);
        return this;
    }
}

Then you can do either of the following: 然后,您可以执行以下任一操作:

    new CLabel("Hello", Color.RED);
    new CLabel("Hello").withColor(Color.RED);

You're going to have to split this up into multiple lines: 您将不得不将其分成多行:

JLabel label = new JLabel("Antwoord Vraag"+ (i +1) +": "+antwoord.get(i),SwingConstants.LEFT);
label.setBackground(Color.GREEN);
label.setOpaque(true);
center.add(label);

PS- I'm going to start calling them "JLabes" too- much trendier! PS-我将开始称它们为“ JLabes”-更加时髦! :p :p

This line: 这行:

center.add(new JLabel("Antwoord Vraag"+ (i +1) +": "+antwoord.get(i),SwingConstants.LEFT)); 

is in a for loop for creating more JLabels 在for循环中用于创建更多JLabel

    for(int i = 0; i<maxCounter; i++)
    {

        // Goede Antwoorden toevoegen aan de center
       center.add(new JLabel("Antwoord Vraag"+ (i +1) +": "+antwoord.get(i),SwingConstants.LEFT));                      
    } 

Because this is not working in the for loop: 因为这在for循环中不起作用:

    JLabel label[i] = new JLabel("Antwoord Vraag"+ (i +1) +": "+antwoord.get(i),SwingConstants.LEFT);

Is there a sollution to change the color 有解决办法改变颜色

I have solved the problem. 我已经解决了问题。 Before i start the loop i creat a array: 在开始循环之前,我创建了一个数组:

JLabel[] labels = new JLabel[maxCounter];

Then in the forloop: 然后在forloop中:

           labels[i] = new JLabel("Antwoord Vraag"+ (i +1) +": "+antwoord.get(i),SwingConstants.LEFT);
           // verander de kleur van de labels
           labels[i].setForeground(rood);
           center.add(labels[i]);

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

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