如何让我的GUI Degree Converter正常工作?

时间:2013-11-15 03:41:15

标签: java user-interface

我想要做的是使我在文本字段中输入的值可以与之交互,但它总是涉及我尝试从String转换为double。我是Comp Sci I的HS新生,这非常令人沮丧,因为我在这里考虑了很多。请看一下:

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class degConverter extends JFrame implements ActionListener {

    private JLabel welcome;
    private JTextField f, c;
    private JButton fI, cI;
    private JButton reset, convertF, convertC;

    public degConverter() {
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.red.darker());

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Celsius-Fahrenheit/Fahrenheit-Celsius Converter.");
        setSize(900, 750);
        setResizable(false);
        setLocation(500, 200);

        welcome = new JLabel("Welcome to the amazing degree converter!");
        welcome.setBounds(320, 50, 300, 40);
        contentPane.add(welcome);

        fI = new JButton("Convert from Fahrenheit to Celsius");
        fI.setBounds(100, 150, 275, 40);
        fI.addActionListener(this);
        contentPane.add(fI);

        cI = new JButton("Convert from Celsius to Fahrenheit");
        cI.setBounds(525, 150, 275, 40);
        cI.addActionListener(this);
        contentPane.add(cI);

        f = new JTextField("Give me a value here");
        f.addActionListener(this);
        contentPane.add(f);

        c = new JTextField("Give me a value here");
        c.addActionListener(this);
        contentPane.add(c);

        reset = new JButton("RESET DIS MOFUCKIN SHIT");
        reset.setBounds(325, 550, 220, 40);
        reset.addActionListener(this);
        contentPane.add(reset);

        convertF = new JButton("CONVERT!!!");
        convertF.addActionListener(this);
        contentPane.add(convertF);

        convertC = new JButton("CONVERT!!!");
        convertC.addActionListener(this);
        contentPane.add(convertC);

        String s = c.getText();
        double cIn = Double.parseDouble(s);
        double fOut = ((1.8) * (cIn)) + 32;
        String s2 = f.getText();
        double fIn = Double.parseDouble(s2);
        double cOut = ((fIn - 32) / 1.8);

    }

    public static void main(String args[]) {
        degConverter frame = new degConverter();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton) {
            JButton clicked = (JButton) e.getSource();
            {
                if (fI == clicked) {
                    fI.setText("Insert value in box below");
                    f.setBounds(100, 275, 250, 40);
                    convertF.setBounds(100, 400, 150, 40);
                }
            }
            if (e.getSource() instanceof JButton) {
                if (cI == clicked) {
                    cI.setText("Insert value in the box below");
                    c.setBounds(525, 275, 250, 40);
                    convertC.setBounds(525, 400, 150, 40);
                }
            }
            if (reset == clicked) {
                fI.setText("Convert from Fahrenheit to Celsius");
                cI.setText("Convert from Celsius to Fahrenheit");
                f.setBounds(1000, 1000, 0, 0);
                c.setBounds(1000, 1000, 0, 0);
            }
            if (convertF == clicked) {
                f.setText(cOut);
            }
            if (convertC == clicked) {
                f.setText(fOut);
            }
        }
    }
}

我在这里尝试做的似乎不起作用。救命?提前致谢。很抱歉这个长程序。

1 个答案:

答案 0 :(得分:0)

代码

  String s = c.getText();
    double cIn = Double.parseDouble(s);
    double fOut = ((1.8)*(cIn))+32;
    String s2 = f.getText();
    double fIn = Double.parseDouble(s2);
    double cOut = ((fIn-32)/1.8);

在用户有时间输入任何内容之前调用。您需要更改内容,以便按“转换”按钮调用此代码 - 如果初始值为c,则仅调用第一位(c到f),如果初始值为华氏,则仅调用第二位(f到c)。 / p>