所以我目前正在用一本大学的java书自学GUI编码,我明白这一切,但我遇到了这个错误,我被难住了。
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();
现在这个程序的基本功能是摄氏温度和摄氏温度之间的转换。我知道这很简单,但我不知道这个错误是什么,我正在从书中准确地输入这个错误。 如果有人可以提供帮助,我们将不胜感激!谢谢。
最佳答案
public class gui
{
....
}
以上3条语句是不需要的。源文件的名称应该是ConvertwithGUI,因为它应该是文件中唯一的公共(public)类。保持源文件简单,以便它们只包含一个公共(public)类。
关于java - 非静态变量 this 不能从静态内容 Java 中引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20579677/