编写动作侦听器以关闭程序Java

CVYC CVYC

我已经编写了将华氏温度转换为摄氏温度的代码。单击窗口右上角的X时,我的动作侦听器没有关闭程序。任何指导表示赞赏。似乎主类无法识别defaultcloseopertion的代码。

 import java.awt.*;
import java.awt.event.*;

public class TemperatureConversion extends Frame implements ActionListener
{
    private final Label lblInput;
    private final Label lblOutput;
    private final TextField tfInput;
    private final TextField tfOutput;
    private double farenheit;
    private double celcius;

    public TemperatureConversion()
    {
        setLayout(new FlowLayout());



        lblInput = new Label ("Enter degrees in Farenheit: ");
        add(lblInput);

        tfInput = new TextField(10);
        add(tfInput);
        tfInput.addActionListener(this);


        lblOutput = new Label("Degrees in celcius:");
        add(lblOutput);

        tfOutput = new TextField(10);
        tfOutput.setEditable(false);
        add(tfOutput);

        setTitle("Farenheit to Celcius Conversion");
        setSize(350, 120);
        setVisible(true);
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        farenheit = Integer.parseInt(tfInput.getText());
        celcius = (5.0/9)*(farenheit -32);
        tfInput.setText("");
        tfOutput.setText(celcius + "");


    }
}

第二档包括主班

import javax.swing.JFrame;

public class TemperatureConversionTest {

    public static void main(String[] args)
    {
        TemperatureConversion textFieldFrame = new TemperatureConversion();
        textFieldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textFieldFrame.setSize(350, 100);
        textFieldFrame.setVisible(true);
    }
}
雷米乌斯

setDefaultCloseOperation是其成员的方法JFramejava.awt.Frame

public class TemperatureConversion extends JFrame implements ActionListener

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章