我想要連接的方法returnedConversion
,使返回結果給ActionListener
用戶選擇後轉換爲和從溫度刻度。我意識到代碼至少有點脫節,所以請忽略所有註釋掉的區域(除非您可以指出一個我應該注意的區域。)實現與ActionListener交互的方法?
如何將代碼從方法returnedConversion
到ActionListener
因此代碼正確地運行?此外,有予正確轉換的輸入從JTextField
框成一個雙然後適當地將它轉換回一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);
}
}
我不是故意要在網站上創建了editers任何額外的工作,我沒有編輯我的代碼,所以它是從左邊的所有4個空格,它表示草稿已保存... – Jremy
要發送結果的位置? – MadProgrammer
的actionListener,因爲那是我認爲它需要去爲了第二個JTextField顯示轉換後的值...是這樣嗎? – Jremy