GUI中的java温度转换

时间:2014-03-13 04:41:23

标签: java swing computer-science

我再次挣扎,在java中执行一个程序,将温度从摄氏温度转换为fahrenhiet,但必须在GUI中完成,这样用户可以输入一个摄氏数字,然后单击一个按钮进行转换。我已经在我书中的一个例子中对它进行了建模,但在书中它表明它正在使用静态main(),但我的IDE给了我一个错误,说它是必需的,所以我添加了一个静态main()和香港专业教育学院尝试调用tempwindow(),看看是否会工作但仍然没有,即使我注释掉调用它不会给我一个错误但没有任何反应。 我跳了一个人可以帮助告诉我我做错了什么以及我应该怎么做。

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

    }
}

3 个答案:

答案 0 :(得分:3)

你做错了一些事:

  • 您的构造函数不能与您的类名称具有不同的名称
  • 您需要实例化对象以调用构造函数,而实例化对象会调用构造函数,但您不能像方法一样访问它。
  • 您应该对类名使用Java命名约定。

班级:

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

主要班级:

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

答案 1 :(得分:2)

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

你永远不会初始化一个模板。您的构造函数必须与类具有相同的名称,因此我建议您进行以下更改:

  1. public tempwindow()替换为public tempcon()以更正构造函数。

    public tempcon()
    {
       setTitle("Temurture convertion");
       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       buildPanel();
       add(panel);
       setVisible(true);
    }
    
  2. 使用new创建一个tempcon实例,它调用构造函数:

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

答案 2 :(得分:2)

请写回复类型你的函数。

    public  void tempwindow()

创建类的对象并调用方法。

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

}

学习java对象java object oriented programming.