Pregunta

So i am currently teaching myself the GUI coding with a java book for college, and i understand it all but i ge this error and i am stumped.

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);

            }
    }
}

Ok so everything up to the part that is labeled:

ConvertWithGUI theGUI = new ConvertWithGUI();

results in the error. more specifically it is highlighted on this part:

new ConvertWithGUI();

Now the basic function of this program is to convert from and to Celsius. It is simple i know but i do not know what this error is, and i am typing this exact from the book. If anyone could help it will be appreciated! Thank you.

¿Fue útil?

Solución

public class gui
{
    ....
}

The above 3 statements are not needed. The name of your source file should be ConvertwithGUI since that should be the only public class in the file. Keep your source files simple so that they only contain one public class.

Otros consejos

Your class ConvertWithGUI isn't static, so you need an instance of the containing class to construct it. Just

public static /*<-- Add this*/ class ConvertWithGUI extends GBFrame
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top