gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:29:28 25 4
gpt4 key购买 nike

我收到一个 Windows 提示框,需要在 2 个字段中将英里转换为公里,反之亦然。当用户填写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);
}

最佳答案

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 并执行类似的操作

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

关于java - 如何使用 Java 中的 2 个 JtextField 将英里转换为公里,反之亦然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20206534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com