我在创建转换器的项目上遇到了困难。问题是无法从中获得任何输出。我认为动作监听器有问题吗?我尝试调试代码,但发现问题不关乎输入或输出字段(我将这两个链接在一起,然后进行了尝试。我得到了正确的答案)。其次,我尝试将打印内容添加到代码的不同部分。这就是我发现该软件永远不会进入
的原因if(from.getSelectedItem()=="meter") {
meterConvertor();..."
我将不胜感激。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.applet.*;
public class Converter extends JFrame implements ActionListener, ItemListener {
JTextField input1_field;
JTextField answer;
double input1 = 0;
double result = 0;
String[] pituudet = new String[] { "Choose to", "meter", "kilometer", "feet" };
String[] pituudet1 = new String[] { "Choose from", "meter", "kilometer", "feet" };
JButton calculate;
JComboBox<String> from;
JComboBox<String> to;
Converter() {
JFrame frame = new JFrame("");
frame.setSize(1000, 200);
frame.setLayout(new BorderLayout());
// Body
JPanel body = new JPanel();
body.setLayout(new GridLayout(1, 1, 50, 50));
body.setPreferredSize(new Dimension(300, 400));
body.setBackground(Color.lightGray);
// from JComboBox1
JComboBox<String> from = new JComboBox<>(pituudet);
from.setSize(200, 2000);
from.setFont(new Font("Serif", Font.PLAIN, 35));
add(from);
from.addActionListener(from);
// to JComboBox2
JComboBox<String> to = new JComboBox<>(pituudet1);
to.setFont(new Font("Serif", Font.PLAIN, 35));
to.addActionListener(to);
// input1 field
input1_field = new JTextField(15);
input1_field.setFont(new Font("Serif", Font.PLAIN, 35));
input1_field.setBounds(650, 300, 400, 100);
add(input1_field);
input1_field.addActionListener(from);
// output field
answer = new JTextField("");
answer.setFont(new Font("Serif", Font.PLAIN, 35));
answer.setBounds(650, 300, 400, 100);
add(answer);
answer.setEditable(false);
// Calculate button
JButton calculate = new JButton("Calculate");
calculate.setFont(new Font("Serif", Font.PLAIN, 20));
calculate.addActionListener(this);
// add staff to body panel
body.add(from);
body.add(to);
body.add(input1_field);
body.add(calculate);
body.add(answer);
// add to frame
frame.add(body);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void meterConvertor() {
if (to.getSelectedItem() == "meter") {
result = (input1);
answer.setText("" + result);
} else if (to.getSelectedItem() == "kilometer") {
result = (input1 * 0.001);
answer.setText("" + result);
} else if (to.getSelectedItem() == "feet") {
result = (input1 * 0.3048);
answer.setText("" + result);
} else {
result = (input1 * 29.7);
answer.setText("" + result);
}
}
public void kilometerConvertor() {
if (to.getSelectedItem() == "meter") {
result = (input1 * 1000);
answer.setText("" + result);
} else if (to.getSelectedItem() == "kilometer") {
result = (input1);
answer.setText("" + result);
} else if (to.getSelectedItem() == "feet") {
result = (input1 * 3280.8399);
answer.setText("" + result);
} else {
result = (input1 * 0.681);
answer.setText("" + result);
}
}
public void feetConvertor() {
if (to.getSelectedItem() == "meter") {
result = (input1 * 0.3048);
answer.setText("" + result);
} else if (to.getSelectedItem() == "kilometer") {
result = (input1 * 0.0003048);
answer.setText("" + result);
} else if (to.getSelectedItem() == "feet") {
result = (input1);
answer.setText("" + result);
} else {
result = (input1 * 0.388);
answer.setText("" + result);
}
}
public void actionPerformed(ActionEvent e) {
input1 = Double.parseDouble(input1_field.getText());
String a = input1_field.getText();
if (e.getSource() == calculate)
if (from.getSelectedItem() == "meter") {
meterConvertor();
} else if (from.getSelectedItem() == "kilometer") {
kilometerConvertor();
} else if (from.getSelectedItem() == "feet") {
feetConvertor();
} else {
kilometerConvertor();
}
}
public void itemStateChanged(ItemEvent e) {
}
public static void main(String args[]) {
Converter window = new Converter();
window.setVisible(true);
}
}