2011-03-01 8 views
0

클릭하면 웹 사이트로 직접 이동하는 버튼을 만듭니다. 하지만 내 코드에 오류가 있습니다. 이 오류는 비 정적 변수를 정적 컨텍스트에서 참조 할 수 없다고 말합니다.정적 컨텍스트에서 비 정적 변수를 참조 할 수 없다는 메시지가 나타나는 이유는 무엇입니까?

public static void main(String[] args) throws Exception { 
    JFrame frame = new JFrame("JLinkButton"); 
    frame.getContentPane().setLayout(new BorderLayout()); 
    frame.getContentPane().add("Center", new AnotherLinkButton("www.google.com")); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setLocation(100, 100); 
    frame.setVisible(true); 
    } 
+2

항상 오류 메시지와 전체 메시지를 제공하는 줄을 게시하십시오. – jzd

+0

죄송합니다 ... 라인이 있습니다 : frame.getContentPane(). add ("Center", new AnotherLinkButton ("www.google.com")); – steph22

답변

2

AnotherLinkButton이 올바르게 정의되었다고 가정하면 코드가 올바르게 보입니다.

당신은 아마 다음과 같은 코드가 있습니다

class Main { 
    class AnotherLinkButton { 
    } 
} 

AnotherLinkButton의 인스턴스를 만들려면 Main의 인스턴스가 필요합니다. 대신이 시도 :

class Main { 
    static class AnotherLinkButton { 
    } 
} 

Main의 독립적 인 AnotherLinkButton하게 그.

+0

+1 그는 아마도'static class' 대신에'class'로'AnotherLinkButton'을 정의했습니다. –

+0

@Aaron, 가능하지만 다른 오류 메시지가 나타납니다. AnotherLinkButton에는 정말로 문제를 일으키는 생성자 또는 다른 것이 있다고 생각합니다. – jzd

+0

클래스가 주 클래스의 내부 클래스 인 경우 정적 클래스에서 사용할 수없는 기본 클래스에 대한 참조가있는 비 정적 필드를 가져옵니다. 따라서 오류. –

관련 문제