[英]Implementing method to interact with ActionListener?
我想連接方法returnedConversion
以便在用戶選擇要轉換和轉換的臨時比例后,將結果返回給ActionListener
。 我知道代碼有點脫節,至少可以說,所以請忽略所有注釋掉的區域(除非你能指出我應該注意的區域)。
如何將該方法從returnedConversion
方法連接到ActionListener
以便代碼正常運行? 我是否正確地將JTextField
框中的輸入轉換為double,然后將其適當地轉換回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);
}
}
作為第一步,請閱讀“為常見處理事件實現監聽器” 。 這將使您了解如何在Swing
使用基本事件處理
如果我理解正確,這里想要你想實現:
JComboBox
選擇轉換選項。 initialTemp
JTextField
輸入值 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(...)
}
現在,您希望在調用Convert
JButton
時調用此方法。 因此,正如您所做的那樣,您需要將ActionListener
與其關聯。 那你在那邊做什么? 這樣的事情:
convertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
returnedConversion(initialTemp.getText());
}
});
我希望這能為您提供正確的指針,幫助您解決代碼問題。
另外作為最后一點,你可能想要閱讀“Threads and Swing”和“Threading with Swing”來了解如何啟動你的Swing應用程序
第49行:
String applyIt = returnedConversion(toConvert);
在這里你應該將一個字符串傳遞給returnedConversion
方法,但是你沒有聲明並初始化toConvert
變量作為字符串。
第50行:
convertedTemp.returnedConversion();
convertedTemp
的類型為JTextField。 所以你不能在這里訪問undefined方法returnedConversion()
。 如果您嘗試在convertedTemp
JTextField中顯示文本,則應使用convertedTemp.setText(applyIt)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.