我無法爲JLabel中的文本設置顏色,我的程序是Jukebox。如何更改非靜態JLabel的文本顏色? 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);
}
當我嘗試設置前景色爲JLabel的NetBeans IDE中給了我一個錯誤的細節,我無法從靜態上下文引用非靜態方法。
如何將我的JLabel的文本顏色更改爲紅色?
感謝您的幫助。