【问题标题】:non-static variable this cannot be referenced from a static content Java [duplicate]非静态变量不能从静态内容Java中引用[重复]
【发布时间】:2013-12-14 04:28:09
【问题描述】:

所以我目前正在用一本大学的 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();

现在这个程序的基本功能是转换和转换摄氏温度。我知道这很简单,但我不知道这个错误是什么,我正在从书中准确地输入这个错误。 如果有人可以提供帮助,我们将不胜感激!谢谢。

【问题讨论】:

    标签: java swing class breeze non-static


    【解决方案1】:
    public class gui
    {
        ....
    }
    

    以上 3 条语句不需要。源文件的名称应该是ConvertwithGUI,因为它应该是文件中唯一的公共类。保持源文件简单,以便它们只包含一个公共类。

    【讨论】:

    • 谢谢!我忽略了愚蠢的小错误。再次感谢!
    【解决方案2】:

    您的类ConvertWithGUI 不是静态的,因此您需要一个包含类的实例来构造它。只是

    public static /*<-- Add this*/ class ConvertWithGUI extends GBFrame
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 2017-02-06
      • 1970-01-01
      • 2011-05-12
      • 2013-12-02
      • 2013-12-16
      • 1970-01-01
      相关资源
      最近更新 更多