2014-02-10 1 views
1

내 JLabel 내의 텍스트 색상을 설정할 수 없습니다. 내 프로그램은 Jukebox입니다.비 정적 JLabel의 텍스트 색상을 변경하는 방법은 무엇입니까? 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의 넷빈즈 IDE에 대한 전경색을 설정하려고

는 나에게 내가 정적 컨텍스트에서 비 정적 방법을 참조 할 수없는 오전 자세히 오류를 제공합니다.

내 JLabel의 텍스트 색상을 빨간색으로 변경하려면 어떻게해야합니까?

도움 주셔서 감사합니다.

답변

1

오류로 알 수 있듯이 클래스 (정적 컨텍스트)에 비 정적 메서드를 호출 할 수 없습니다. 그래서이 허용되지 않습니다 :

JLabel.setForeground(Color.red); 

JLabel는 클래스가 아니라 그것의 특정 인스턴스를 나타냅니다. 이 오류는 setForeground가 JLabel 유형의 객체에서 호출되어야한다고 알려줍니다. 따라서 JLabel 개체를 만든 다음 메서드를 사용하여 포 그라운드를 설정합니다.

0

오류가 정확히 무슨 뜻 WHICH 레이블을 지정해야합니다 라인

JLabel.setForeground(Color.red); 

을 의미합니다. 예 :

class Jukebox 
{ 
    private JLabel someLabel; 
    ... 

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