非静态变量不能从静态内容Java引用此变量
因此,我目前正在用一本大学用的java书籍自学GUI编码,我完全理解它,但我发现了这个错误,我被难住了非静态变量不能从静态内容Java引用此变量,java,swing,class,breeze,non-static,Java,Swing,Class,Breeze,Non Static,因此,我目前正在用一本大学用的java书籍自学GUI编码,我完全理解它,但我发现了这个错误,我被难住了 import javax.swing.*; import BreezySwing.*; public class gui { public class ConvertWithGUI extends GBFrame { private JLabel fahrenheitLabel; private JLabel
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);
}
}
}
导致错误。更具体地说,这一部分强调:
new ConvertWithGUI();
现在这个程序的基本功能是从摄氏度转换为摄氏度。这很简单,我知道,但我不知道这个错误是什么,我正是从书中输入的。
如果有人能帮忙,我们将不胜感激!多谢各位
public class gui
{
....
}
不需要上述3种陈述。源文件的名称应该是
ConvertwithGUI
,因为它应该是文件中唯一的公共类。保持源文件简单,以便它们只包含一个公共类。您的类ConvertWithGUI
不是静态的,因此您需要包含类的实例来构造它。只是
public static /*<-- Add this*/ class ConvertWithGUI extends GBFrame
publicstatic/*谢谢!我忽略了一个愚蠢的小错误。再次感谢!
public static /*<-- Add this*/ class ConvertWithGUI extends GBFrame