如何使用Java中的2个JtextField将英里转换为km,反之亦然?

时间:2013-11-26 00:50:05

标签: java swing parsing double jtextfield

我有一个Windows提示框需要将里程转换为km,反之亦然。 当用户填充field1时,它会在Field2中对其进行转换。当用户更改字段的值时,它会重新计算字段。

我无法通过第一个字段,它只计算第一个字段。

这是“计算”按钮的操作:

 if (jTextField1.getText() != null ) {
       jTextField2.setText("" + Double.parseDouble(jTextField1.getText()) * 0.62137);
    } 

    if (jTextField2.getText() != null) {

        jTextField1.setText("" + Double.parseDouble(jTextField2.getText()) * 1.62137);
    }

1 个答案:

答案 0 :(得分:2)

if (jTextField1.getText() != null ) {
   jTextField2.setText("" + Double.parseDouble(jTextField1.getText()) * 0.62137);
} 

if (jTextField2.getText() != null) {

    jTextField1.setText("" + Double.parseDouble(jTextField2.getText()) * 1.62137);
}

第一个if语句将文字设置为jTextField2,不再使null成为下一个if

也许拿出第二个如果并做这样的事情

if (jTextField1.getText() != null ) {
   double value = Double.parseDouble(jTextField1.getText());
   String text1 = (value * 0.62137) + "";
   String text2 = (value * 1.62137) + "";

   jTextField2.setText(text1);
   jTextField1.setText(text2);
} 

编辑:经过多次黑客攻击

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class MilesKiloConverter {

private JTextField jtfMiles = new JTextField(15);
private JTextField jtfKilos = new JTextField(15);

private JLabel jlbMiles = new JLabel("miles");
private JLabel jlbKilos = new JLabel("km");

private JButton compute = new JButton("Compute");
private JFrame frame = new JFrame();

double km = 0;
double miles = 0;

public void createAndSHowGui() {
    JPanel p1 = new JPanel(new GridLayout(2, 2, 5, 5));
    p1.add(jtfMiles);
    p1.add(jlbMiles);
    p1.add(jtfKilos);
    p1.add(jlbKilos);

    frame.add(p1, BorderLayout.CENTER);
    frame.add(compute, BorderLayout.SOUTH);

    jtfMiles.setHorizontalAlignment(SwingConstants.RIGHT);
    jtfKilos.setHorizontalAlignment(SwingConstants.RIGHT);

    jtfKilos.setFocusable(true);


    compute.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            double newKm = 0;
            double newMiles = 0;

            if ("".equals(jtfMiles.getText())) {
                newKm = Double.parseDouble(jtfKilos.getText());
                jtfMiles.setText(String.valueOf(newKm * 1.62137));
                km = newKm;
            } else if ("".equals(jtfKilos.getText())) {
                newMiles = Double.parseDouble(jtfMiles.getText());
                jtfKilos.setText(String.valueOf(newMiles * 0.62137));

            } else {
                newMiles = Double.parseDouble(jtfMiles.getText());
                newKm = Double.parseDouble(jtfKilos.getText());

                double milesDiff = miles - newMiles;
                double kiloDiff = km - newKm;

                if (Math.abs(milesDiff) > Math.abs(kiloDiff)) {
                    jtfKilos.setText(String.valueOf(newMiles * 1.62137));
                    km = newKm;
                    miles = newMiles;
                } else {
                    jtfMiles.setText(String.valueOf(newKm * 0.62137));
                    km = newKm;
                    miles = newMiles;
                }
            }
        }
    });

    frame.setTitle("Miles/Kilometers Converter");
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run(){
            new MilesKiloConverter().createAndSHowGui();
        }
    });
}
}