0

私がやろうとしているのは、テキスト フィールドに入力した値を操作できるようにすることですが、常に String から double に変換しようとする必要があります。私は Comp Sci I の HS 1 年生ですが、これには多くのことを考えていたので、かなりイライラしています。これを見てください:

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

ここでやろうとしていることはうまくいかないようです。ヘルプ?前もって感謝します。長い番組ですみません。

4

1 に答える 1

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) のみ、初期値が華氏の場合は 2 番目のビット (f から c) のみが呼び出されます。

于 2013-11-15T03:46:43.203 に答える