实现与ActionListener交互的方法?

Jre*_*emy 2 java swing jbutton actionlistener

我想连接方法,returnedConversion以便ActionListener在用户选择转换为和来自哪个临时比例之后将结果返回给.我知道代码有点脱节,至少可以说,所以请忽略所有注释掉的区域(除非你能指出我应该注意的区域).

如何将该方法returnedConversion中的代码连接到代码才能ActionListener正常运行?我是否正确地将JTextField盒子中的输入转换为双倍然后适当地将其转换回a String以将其传递回第二个JTextField盒子?

package temperatureConverter;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class TempConverter extends JFrame {
    private JComboBox firstComboBox;
    private JComboBox secondComboBox;
    private JTextField initialTemp;
    private JTextField convertedTemp;
    private JButton convertButton;
    // private enum TempType { FAHRENHEIT, CELSIUS, KELVIN};
    private static final String[] tempType = { "Fahrenheit", "Celsius",
            "Kelvin" };
    public static final String theInitialTempType = null;
    public static final String theTempTypeToConvertTo = null;
    public static final String theChosenTemp = null;
    public static final String theNewTemp = null;

    public TempConverter() {
        super("Temperature Converter");
        setLayout(new FlowLayout());

        firstComboBox = new JComboBox(tempType);
        firstComboBox.setMaximumRowCount(3);
        firstComboBox.addActionListener(null);
        add(firstComboBox);
        secondComboBox = new JComboBox(tempType);
        secondComboBox.setMaximumRowCount(3);
        secondComboBox.addActionListener(null);
        add(secondComboBox);
        initialTemp = new JTextField("", 10);
        initialTemp.addActionListener(null);
        add(initialTemp);
        convertedTemp = new JTextField("", 10);
        convertedTemp.addActionListener(null);
        add(convertedTemp);
        convertButton = new JButton("Convert");
        convertButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String applyIt = returnedConversion(initialTemp.getText());
                System.out.println(applyIt);
//              convertedTemp.returnedConversion();
                // ???????????????????????????????????????????
            }
        });
        add(convertButton);
        // String theInitialTempType = (String) firstComboBox.getSelectedItem();
        // String theTempTypeToConvertTo = (String)
        // secondComboBox.getSelectedItem();
        // String theChosenTemp = initialTemp.getSelectedText();
        // String theNewTemp = convertedTemp.getSelectedText();
    }

    // public class textHandler implements ActionListener
    // {
    // public void itemStateChanged (ActionEvent event)
    // {
    // double convertedNumberForTheChosenTemp =
    // Double.parseDouble(theChosenTemp);
    // double convertedNumberForTheNewTemp = Double.parseDouble(theNewTemp);
    // String string1 = "";
    // String string2 = "";
    //
    // if ( theInitialTempType == tempType[0] && theTempTypeToConvertTo ==
    // tempType[1] )
    //
    // convertedNumberForTheNewTemp = (convertedNumberForTheChosenTemp - 32) * 5
    // / 9;
    // String result = String.valueOf(convertedNumberForTheNewTemp);
    // convertedTemp.getSelectedText();
    // }

    // @Override
    // public void actionPerformed (ActionEvent e) {
    //
    // }
    // }
    public String returnedConversion(String toConvert) {
        double convertedNumberForTheChosenTemp = Double.parseDouble(theChosenTemp);
        double convertedNumberForTheNewTemp = Double.parseDouble(theNewTemp);

        if (theInitialTempType == tempType[0] && theTempTypeToConvertTo == tempType[1]) {
            convertedNumberForTheNewTemp = (convertedNumberForTheChosenTemp - 32) * 5 / 9;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        } else if (theInitialTempType == tempType[0] && theTempTypeToConvertTo == tempType[2]) {
            convertedNumberForTheChosenTemp = (convertedNumberForTheChosenTemp + 459.67) / 1.8;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;

        } else if (theInitialTempType == tempType[1] && theTempTypeToConvertTo == tempType[0]) {
            convertedNumberForTheChosenTemp = (convertedNumberForTheChosenTemp * 1.8) + 32;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        } else if (theInitialTempType == tempType[1] && theTempTypeToConvertTo == tempType[2]) {
            convertedNumberForTheChosenTemp = convertedNumberForTheChosenTemp + 273.15;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        } else if (theInitialTempType == tempType[2] && theTempTypeToConvertTo == tempType[0]) {
            convertedNumberForTheChosenTemp = (convertedNumberForTheChosenTemp * 1.8) - 459.67;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        } else if (theInitialTempType == tempType[2] && theTempTypeToConvertTo == tempType[1]) {
            convertedNumberForTheChosenTemp -= 273.15;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        }

        return null;
    }

    public static void main(String[] args) {
        TempConverter tempTest = new TempConverter();
        tempTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tempTest.setSize(300, 200);
        tempTest.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

Suj*_*jay 6

作为第一步,请阅读"为常见处理事件实现监听器".这将使您更好地了解如何使用基本事件处理Swing

如果我理解正确,这里想要你想实现:

  • 用户使用JComboBox您提供的选项选择转换选项.
  • 用户输入您第一次JTextField调用的值initialTemp
  • 用户按下Convert JButton然后您想捕获该事件,隐藏第一个文本JTextField并在第二个中显示转换结果JTextField.

因此,作为第一步,您希望实现一个执行转换的方法,即当用户按下Convert按钮时,将调用此方法,它将从第一个中获取值JTextField,执行转换并将其更新为文本值第二个JTextField.你有一个叫做的方法public String returnedConversion(String toConvert),我建议对此进行一些修改:

public void returnedConversion(String initialValue){
    //Step 1. Validate the input
    //Step 2. Convert the value. You write your own logic taking into account the initialValue
    //        and the JComboBox conversion options
    //Step 3. Set the text of the second JTextField to the converted value, using the method convertedTemp.setText(...)
}
Run Code Online (Sandbox Code Playgroud)

现在,您希望在调用此方法时调用此方法Convert JButton.所以,正如你所做的那样,你会希望ActionListener与它相关联.那你在那边做什么?这样的事情:

convertButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        returnedConversion(initialTemp.getText());
    }
});
Run Code Online (Sandbox Code Playgroud)

我希望这能为您提供正确的指针,帮助您解决代码问题.

另外作为最后一点,你可能想要阅读"Threads and Swing""Threading with Swing"来了解如何启动你的Swing应用程序