Domanda

im struggling again, with doing a program in java that converts the temperature from celsius to fahrenhiet, but it must be done in a GUI so that the user may enter in a number for celsius and click a botton to convert it. ive been modeling it off of a example in my book however in the book it shows it working with out having a static main(), but my IDE gave me an error saying that it was needed, so ive added a static main() and ive tried calling the tempwindow() to see if that would work but still nothing and even if i comment out the call it doesnt give me a error but nothing happens. im hopping someone can help show me what im doing wrong and how i should go about this.

import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.event.*;

public class tempcon extends JFrame
{
    private JPanel panel;
    private JLabel messageLabel;
    private JTextField tempC;
//    private JRadioButton tempF;
//    private ButtonGroup radioButtonGroup;
    private JButton calcButton;
    private final int WINDOW_WIDTH = 400;
    private final int WINDOW_HEIGHT = 100;

    public tempwindow()
    {
       setTitle("Temurture convertion");
       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       buildPanel();
       add(panel);
       setVisible(true);
    }

    private void buildPanel()
    {
        messageLabel = new JLabel("enter tempurture in celsius");
        tempC = new JTextField(10);
        calcButton = new JButton("convert");

        calcButton.addActionListener(new CalcButtonListener());
        panel = new JPanel();

        panel.add(messageLabel);
        panel.add(tempC);
        panel.add(calcButton);

    }
    private class CalcButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String input;
            double temp;
            input = tempC.getText();
            temp = Double.parseDouble(input) * 1.8 + 32;
            JOptionPane.showMessageDialog(null, "that is " + temp + "degrees fehrenhiet");
        }
    }
    public static void main(String[] args)
    {
        tempwindow();

    }
}
È stato utile?

Soluzione 2

public static void main(String[] args)
{
    tempcon myTempWindowInstance = new tempcon();
    myTempWindowInstance.tempwindow();
}

You never initialize a tempcon. Your constructor must have the same name as the class, so I recommend the following changes instead:

  1. Replace public tempwindow() with public tempcon() to correct the constructor.

    public tempcon()
    {
       setTitle("Temurture convertion");
       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       buildPanel();
       add(panel);
       setVisible(true);
    }
    
  2. create an instance of tempcon using new, which calls the constructor:

    public static void main(String[] args)
    {
        tempcon myTempWindowInstance = new tempcon();
    }
    

Altri suggerimenti

You are doing a few things wrong:

  • Your constructor can't have a different name from your class name
  • You need to instantiate the object to call the constructor, rather instantiating an object does call the constructor but you can't just access it like a method.
  • You should use the Java naming conventions for class names.

The class:

public class TempCon extends JFrame
{
// Variable declarations
    public TempCon()  // Constructor should match the Class name
    {
    }
}

The main class:

public static void main(String[] args)
    {
        TempCon converter = new TempCon();
    }

please write return type your function like.

    public  void tempwindow()

create object of class and call method.

public static void main(String[] args)
{
    tempcon t=new tempcon();
    t.tempwindow();

}

and learn java object java object oriented programming.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top