简体   繁体   English

如何更改非静态JLabel的文本颜色? Java的

[英]How to change the text color of a non-static JLabel? Java

I can't set the color for the text within my JLabel, my program is a Jukebox. 我无法在JLabel中设置文本的颜色,我的程序是自动存储塔。

The code is as follows. 代码如下。 I'm fairly new to Java. 我是Java的新手。

public Jukebox() {
    setLayout(new BorderLayout());
    setSize(800, 350);
    setTitle("Jukebox");


    // close application only by clicking the quit button
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

    JPanel top = new JPanel();
    top.add(new JLabel("Select an option by clicking one of the buttons below"));
    add("North", top);
    top.setForeground(Color.RED);
    JPanel bottom = new JPanel();
    check.setBackground(Color.black);
    playlist.setBackground(Color.black);
    update.setBackground(Color.black);
    quit.setBackground(Color.black);
    check.setForeground(Color.red);
    playlist.setForeground(Color.red);
    update.setForeground(Color.red);
    quit.setForeground(Color.red);
    JLabel.setForeground(Color.red);
    bottom.add(check); check.addActionListener(this);
    bottom.add(playlist); playlist.addActionListener(this);
    bottom.add(update); update.addActionListener(this);
    bottom.add(quit); quit.addActionListener(this);
    bottom.setBackground(Color.darkGray);
    top.setBackground(Color.darkGray);
    add("South", bottom);

    JPanel middle = new JPanel();
    // This line creates a JPannel at the middle of the JFrame. 
    information.setText(LibraryData.listAll());
    // This line will set text with the information entity using code from the Library data import.
    middle.add(information);
    // This line adds the 'information' entity to the middle of the JFrame.
    add("Center", middle);

    setResizable(false);
    setVisible(true);
}

When I try to set the foreground color for the JLabel NetBeans IDE gives me an error detailing that I am unable to reference a non-static method from a static context. 当我尝试为JLabel NetBeans IDE设置前景色时,出现错误,详细说明了我无法从静态上下文中引用非静态方法。

What must I do to change the text color of my JLabel to red? 如何将JLabel的文本颜色更改为红色?

Thanks for the help. 谢谢您的帮助。

As the error tells you, you can't call a non-static method on a class (which is the "static context"). 如错误所示,您不能在类(即“静态上下文”)上调用非静态方法。 So this is not allowed: 因此,这是不允许的:

JLabel.setForeground(Color.red);

JLabel refers to the class and not to a particular instance of it. JLabel引用该类,而不是它的特定实例。 The error is telling you that setForeground needs to be called on an object of type JLabel . 该错误告诉您需要在JLabel类型的对象上调用setForeground。 So make a JLabel object and then set its foreground with the method. 因此,创建一个JLabel对象,然后使用方法设置其前景。

The error refers to the line 错误是指行

JLabel.setForeground(Color.red);

You have to specify WHICH label exactly you mean. 您必须准确地指定WHICH标签。 For example 例如

class Jukebox
{
    private JLabel someLabel;
    ...

    public Jukebox() 
    {
        ...
        // JLabel.setForeground(Color.red); // Remove this
        someLabel.setForeground(Color.RED); // Add this
        ...
    }
}

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

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