java - 非静态变量 this 不能从静态内容 Java 中引用

标签 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          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/

相关文章:

java - Netbeans JSR-296、Swing 和 JavaFX,我们现在打算使用什么?

java - 将多个 Jtextarea 添加到同一个 JScrollPane 中

php - __METHOD__ 和 __FUNCTION__

java - 通过 EventObject 的 Getter 和 Setter - ClassCastException 同一类 (Java)

c++ - 我可以使用类模板返回类型的函数吗?

java - JOOQ 从 selectCount 检索 map

java - 无法在 Hibernate 中保存多对多关系

java - 设置 JButton 的宽度以容纳一定数量的字符

Java 和 Selenium - 按文本选择列表中的单选按钮

java - 将嵌入式 Derby 数据库整个表放入字符串数组中