使用 ComboBox if 语句,方程答案不会输出到 JTextField
Equation answer won't output to JTextField with using CoomboBox if statments
我是 Java 的新手,正在参加 Java 1 class。我试图在 if 语句中使用 JComboBox 选择,然后根据 JTextField 中的选择显示答案。这一切都正确编译,但答案不会显示,我不知道我需要改变什么。我搜索并尝试了几种不同的方法,但 none 似乎有效。
import javax.swing.*;
import java.awt.event.*;
public class HayDyGuiTempConv extends JFrame
{
public static void main(String[] args)
{
new HayDyGuiTempConv();
}
private JButton buttonConvert;
private JButton exitButton;
private JTextField textAmount;
private String fieldText;
private JTextField field;
public HayDyGuiTempConv()
{
this.setSize(440,150);
this.setLocation(350,420);
this.setTitle("Temperature Conversion");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonListener bl = new ButtonListener();
JPanel panel1 = new JPanel();
panel1.add(new JLabel("Enter temperature to convert: "));
textAmount = new JTextField(6);
panel1.add(textAmount);
JComboBox<String> comboBox = new JComboBox<> (new String[] {"C to F", "F to C"});
comboBox.addActionListener(bl);
panel1.add(comboBox);
buttonConvert = new JButton("Convert");
buttonConvert.addActionListener(bl);
buttonConvert.setToolTipText("Convert the temperature.");
panel1.add(buttonConvert);
panel1.add(new JLabel("Temp: "));
field = new JTextField(6);
field.setEditable(false);
panel1.add(field);
exitButton = new JButton("Exit");
exitButton.addActionListener(bl);
exitButton.setToolTipText("Exit the program.");
panel1.add(exitButton);
this.add(panel1);
this.setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttonConvert)
{
if(e.getSource() == ("C to F"))
{
double tempEntered = Double.parseDouble(textAmount.getText());
double tempConverted = tempEntered - 32 * (5/9);
String tempAmount = (Double.toString(tempConverted));
field.setText(tempAmount);
}
else if(e.getSource() == ("F to C"))
{
double tempEntered = Double.parseDouble(textAmount.getText());
double tempConverted = tempEntered * (9/5) + 32;
String tempAmount = (String.format("%.2f",(tempConverted)));
field.setText(tempAmount);
}
}
else if(e.getSource() == exitButton)
{
System.exit(0);
}
}
}
}
编辑:我尝试了这两个建议,当我输入一个数字并尝试转换时,我尝试了这两个建议我在交互窗格中得到了这个:线程异常"AWT-EventQueue-0" java.lang.ClassCastException:javax.swing.JButton 无法转换为 javax.swing.JComboBox
if(e.getSource() == ("C to F"))
事件的"source"是一个组件,不是String。在这种情况下,它是 JComboBox
.
您想测试组合框的选定值,因此代码应类似于:
JComboBox comboBox = (JComboBox)e.getSource();
int index = comboBox.getSelectedIndex();
If (index == 0)
// do C to F conversion
else
// do F to C conversion
此外,不要使用“==”来比较字符串值。在将来进行字符串比较时,您可以使用字符串的 equals(...)
方法。
编辑:
我以为您要将 ActionListener 添加到组合框中。然后您可以在从组合框中选择项目时自动进行转换。不需要按钮。
如果您想保留按钮,则需要将组合框定义为 class 中的实例变量。然后您可以通过名称访问组合框。
比较字符串时不要使用 ==,因为这不会比较值,而是比较实例。
通过comboBox.getSelectedItem().toString()获取comboBox的String值;
然后将它们与 .equals() 方法进行比较。
大多数情况下,== 将用于比较数字或实例;
String selected = comboBox.getSelectedItem().toString();
if(selected.equals("C to F")){
// do C to F conversion
}else{
// do F to C conversion
}
我是 Java 的新手,正在参加 Java 1 class。我试图在 if 语句中使用 JComboBox 选择,然后根据 JTextField 中的选择显示答案。这一切都正确编译,但答案不会显示,我不知道我需要改变什么。我搜索并尝试了几种不同的方法,但 none 似乎有效。
import javax.swing.*;
import java.awt.event.*;
public class HayDyGuiTempConv extends JFrame
{
public static void main(String[] args)
{
new HayDyGuiTempConv();
}
private JButton buttonConvert;
private JButton exitButton;
private JTextField textAmount;
private String fieldText;
private JTextField field;
public HayDyGuiTempConv()
{
this.setSize(440,150);
this.setLocation(350,420);
this.setTitle("Temperature Conversion");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonListener bl = new ButtonListener();
JPanel panel1 = new JPanel();
panel1.add(new JLabel("Enter temperature to convert: "));
textAmount = new JTextField(6);
panel1.add(textAmount);
JComboBox<String> comboBox = new JComboBox<> (new String[] {"C to F", "F to C"});
comboBox.addActionListener(bl);
panel1.add(comboBox);
buttonConvert = new JButton("Convert");
buttonConvert.addActionListener(bl);
buttonConvert.setToolTipText("Convert the temperature.");
panel1.add(buttonConvert);
panel1.add(new JLabel("Temp: "));
field = new JTextField(6);
field.setEditable(false);
panel1.add(field);
exitButton = new JButton("Exit");
exitButton.addActionListener(bl);
exitButton.setToolTipText("Exit the program.");
panel1.add(exitButton);
this.add(panel1);
this.setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttonConvert)
{
if(e.getSource() == ("C to F"))
{
double tempEntered = Double.parseDouble(textAmount.getText());
double tempConverted = tempEntered - 32 * (5/9);
String tempAmount = (Double.toString(tempConverted));
field.setText(tempAmount);
}
else if(e.getSource() == ("F to C"))
{
double tempEntered = Double.parseDouble(textAmount.getText());
double tempConverted = tempEntered * (9/5) + 32;
String tempAmount = (String.format("%.2f",(tempConverted)));
field.setText(tempAmount);
}
}
else if(e.getSource() == exitButton)
{
System.exit(0);
}
}
}
}
编辑:我尝试了这两个建议,当我输入一个数字并尝试转换时,我尝试了这两个建议我在交互窗格中得到了这个:线程异常"AWT-EventQueue-0" java.lang.ClassCastException:javax.swing.JButton 无法转换为 javax.swing.JComboBox
if(e.getSource() == ("C to F"))
事件的"source"是一个组件,不是String。在这种情况下,它是 JComboBox
.
您想测试组合框的选定值,因此代码应类似于:
JComboBox comboBox = (JComboBox)e.getSource();
int index = comboBox.getSelectedIndex();
If (index == 0)
// do C to F conversion
else
// do F to C conversion
此外,不要使用“==”来比较字符串值。在将来进行字符串比较时,您可以使用字符串的 equals(...)
方法。
编辑:
我以为您要将 ActionListener 添加到组合框中。然后您可以在从组合框中选择项目时自动进行转换。不需要按钮。
如果您想保留按钮,则需要将组合框定义为 class 中的实例变量。然后您可以通过名称访问组合框。
比较字符串时不要使用 ==,因为这不会比较值,而是比较实例。
通过comboBox.getSelectedItem().toString()获取comboBox的String值; 然后将它们与 .equals() 方法进行比较。
大多数情况下,== 将用于比较数字或实例;
String selected = comboBox.getSelectedItem().toString();
if(selected.equals("C to F")){
// do C to F conversion
}else{
// do F to C conversion
}