2013-12-14 2 views
0

그래서 현재 저는 대학용 자바 서적으로 GUI 코딩을 가르치고 있습니다. 그러나이 오류는 모두 이해할 수 있습니다.정적이 아닌 변수 정적 콘텐츠에서 참조 할 수 없습니다. Java

import javax.swing.*; 
import BreezySwing.*; 
public class gui 
{ 
    public class ConvertWithGUI extends GBFrame 
    { 
     private JLabel   fahrenheitLabel; 
     private JLabel   celsiusLabel; 
     private DoubleField  fahrenheitField; 
     private DoubleField  celsiusField; 
     private JButton   fahrenheitButton; 
     private JButton   celsiusButton; 

      public ConvertWithGUI() 
      { 
       fahrenheitLabel = addLabel  ("Fahrenheit" ,1,1,1,1); 
       celsiusLabel = addLabel  ("Celsius" ,1,2,1,1); 

       fahrenheitField = addDoubleField (32.0   ,2,1,1,1); 
       celsiusField = addDoubleField (0.0   ,2,2,1,1); 
       fahrenheitButton= addButton  (">>>>>>"  ,3,1,1,1); 
       celsiusButton = addButton  ("<<<<<<"  ,3,2,1,1); 
      } 

      public void buttonClicked (JButton buttonObj) 
      { 
       Thermometer thermo = new Thermometer(); 

       if (buttonObj == fahrenheitButton) 
       { 
        thermo.setFahrenheit(fahrenheitField.getNumber()); 
        celsiusField.setNumber (thermo.getCelsius()); 
       } 
       else 
       { 
        thermo.setCelsius(celsiusField.getNumber()); 
        fahrenheitField.setNumber (thermo.getFahrenheit()); 
       } 
      } 
      public static void main(String[] args) 
      { 
       ConvertWithGUI theGUI = new ConvertWithGUI(); 
       theGUI.setSize(250, 100); 
       theGUI.setVisible(true); 

      } 
    } 
} 

좋아 그렇게 표시되어있는 부분까지 모든 것을 : 오류에

ConvertWithGUI theGUI = new ConvertWithGUI(); 

결과. 더 구체적으로이 부분에 강조 표시되어 있습니다 :

new ConvertWithGUI(); 

이제이 프로그램의 기본 기능은 섭씨로 변환하고 섭씨로 변환하는 것입니다. 그것은 간단하지만 나는이 오류가 무엇인지 모르겠다. 나는이 책에서 정확히 입력하고있다. 누구든지 도움을받을 수 있다면 감사하겠습니다! 고맙습니다.

답변

2
public class gui 
{ 
    .... 
} 

위의 세 문장은 필요하지 않습니다. 소스 파일의 이름은 파일의 유일한 공용 클래스 여야하므로 ConvertwithGUI이어야합니다. 소스 파일을 단순하게 유지하여 공용 클래스가 하나만 포함되도록하십시오.

+0

고마워요! 어리석은 작은 실수는 간과 했어. 다시 한 번 감사드립니다! –

0

클래스 ConvertWithGUI은 정적이 아니므로이를 포함하는 클래스의 인스턴스가 필요합니다. 바로

public static /*<-- Add this*/ class ConvertWithGUI extends GBFrame 
관련 문제