Java 方程式答案为';如果状态为,则使用CoomboBox将t输出到JTextField

Java 方程式答案为';如果状态为,则使用CoomboBox将t输出到JTextField,java,swing,if-statement,jtextfield,jcombobox,Java,Swing,If Statement,Jtextfield,Jcombobox,我对Java非常陌生,正在上Java1课程。我试图在if语句中使用JComboBox选择,然后根据选择在JTextField中显示答案。这一切编译正确,但答案将不会显示,我不知道我需要改变什么。我搜索并尝试了几种不同的方法,但似乎都不管用 import javax.swing.*; import java.awt.event.*; public class HayDyGuiTempConv extends JFrame { public static void main(String[] a

我对Java非常陌生,正在上Java1课程。我试图在if语句中使用JComboBox选择,然后根据选择在JTextField中显示答案。这一切编译正确,但答案将不会显示,我不知道我需要改变什么。我搜索并尝试了几种不同的方法,但似乎都不管用

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);
        }
    }
}
}
import javax.swing.*;
导入java.awt.event.*;
公共类HayDyGuiTempConv扩展了JFrame
{
公共静态void main(字符串[]args)
{
新HayDyGuiTempConv();
}
私人JButton按钮转换;
私有JButton exitButton;
私有JTextField textmount;
私有字符串字段文本;
私有JTextField字段;
公共HayDyGuiTempConv()
{
这个。设置大小(440150);
此设置位置(350420);
本文件为setTitle(“温度转换”);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonListener bl=新ButtonListener();
JPanel panel1=新的JPanel();
面板1.添加(新的JLabel(“输入要转换的温度:”);
textAmount=新的JTextField(6);
面板1.添加(文本金额);
JComboBox组合框=新的JComboBox(新字符串[]{“C到F”,“F到C”});
comboBox.addActionListener(bl);
面板1.添加(组合框);
buttonConvert=新的JButton(“转换”);
按钮Convert.addActionListener(bl);
按钮Convert.setToolTipText(“转换温度”);
面板1.添加(按钮转换);
面板1.添加(新的JLabel(“临时”);
字段=新的JTextField(6);
字段设置为可编辑(false);
1.添加(字段);
exitButton=新JButton(“退出”);
exitButton.addActionListener(bl);
setToolTipText(“退出程序”);
面板1.添加(退出按钮);
本条增补(第1组);
此.setVisible(true);
}
私有类ButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
如果(例如getSource()==按钮转换)
{
如果(e.getSource()==(“C到F”))
{
double tempEntered=double.parseDouble(textmount.getText());
双tempConverted=tempcentered-32*(5/9);
字符串tempAmount=(Double.toString(tempConverted));
field.setText(tempAmount);
}
else if(e.getSource()=(“F到C”))
{
double tempEntered=double.parseDouble(textmount.getText());
双温转换=温输入*(9/5)+32;
字符串tempAmount=(String.format(“%.2f”,(tempConverted));
field.setText(tempAmount);
}
}
else if(例如getSource()==exitButton)
{
系统出口(0);
}
}
}
}
编辑:我尝试了这两个建议,当我键入一个数字并尝试转换时,我在交互窗格中得到了这两个建议:线程“AWT-EventQueue-0”java.lang.ClassCastException中的异常:javax.swing.JButton不能转换为javax.swing.JComboBox

if(e.getSource() == ("C to F"))
事件的“源”是一个组件,而不是字符串。在本例中,它是
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添加到组合框中。然后,当从组合框中选择项目时,您可以自动进行转换。不需要这个按钮


如果希望保留该按钮,则需要将组合框定义为类中的实例变量。然后,您可以按名称访问组合框。

在比较字符串时不要使用==因为这不会比较值,而是比较实例

使用comboBox.getSelectedItem().toString()获取comboBox的字符串值; 然后将它们与.equals()方法进行比较

大多数情况下==将用于比较数字或实例

String selected = comboBox.getSelectedItem().toString();

if(selected.equals("C to F")){
//  do C to F conversion
}else{
//  do F to C conversion
}

我根据您的建议编辑了我的代码,现在当我尝试转换时,我得到了很多红色文本,表示JButton不能转换为javax.swing.JComboBox。这里有一个异常:线程“AWT-EventQueue-0”java.lang.ClassCastException中的异常:javax.swing.JButton无法转换为javax.swing.jcombox非常感谢。我需要保留按钮,所以我添加了实例变量,它起作用了。我根据您的建议编辑了代码,现在我在尝试转换时收到了很多红色文本,表示JButton不能转换为javax.swing.JComboBox。以下是异常:线程“AWT-EventQueue-0”java.lang.ClassCastException中的异常:javax.swing.JButton不能转换为javax.swing.JComboBox–