2012-09-01 45 views
2

我想要連接的方法returnedConversion,使返回結果給ActionListener用戶選擇後轉換爲和從溫度刻度。我意識到代碼至少有點脫節,所以請忽略所有註釋掉的區域(除非您可以指出一個我應該注意的區域。)實現與ActionListener交互的方法?

如何將代碼從方法returnedConversionActionListener因此代碼正確地運行?此外,有予正確轉換的輸入從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); 
    } 
} 
+0

我不是故意要在網站上創建了editers任何額外的工作,我沒有編輯我的代碼,所以它是從左邊的所有4個空格,它表示草稿已保存... – Jremy

+0

要發送結果的位置? – MadProgrammer

+0

的actionListener,因爲那是我認爲它需要去爲了第二個JTextField顯示轉換後的值...是這樣嗎? – Jremy

回答

6

作爲第一步,閱讀有關"Implementing Listeners for Commonly Handled Events"。這會給你的如何在Swing

基本事件處理如果我理解正確的工作是個好主意,這裏的想你想實現:

  • 用戶選擇轉換選項使用JComboBox你」已經提供。
  • 用戶輸入您的第一個JTextField稱爲initialTemp
  • 用戶價值按ConvertJButton,然後你要拍攝的事件,隱蔽在第一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(...) 
} 

現在你想調用此方法時,ConvertJButton被調用。所以,如果你做對了,你會想要一個ActionListener與它關聯。現在你在那邊做什麼?那麼這樣的事情:

convertButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent event) { 
     returnedConversion(initialTemp.getText()); 
    } 
}); 

我希望這給你正確的指針,以幫助你與你的代碼。

而且作爲最後一點,你可能想了解"Threads and Swing""Threading with Swing"瞭解有關如何啓動你的Swing應用程序

+1

很棒的回答。 :) –

+0

@AndrewThompson:謝謝:) – Sujay

+0

是的我完全同意這是一個非常明確的迴應。我現在可以更加確定地解決我的其餘問題。謝謝Sujay。 – Jremy

2

在管線49:

String applyIt = returnedConversion(toConvert); 

這裏你應該字符串傳遞給returnedConversion方法,但是你有沒有聲明和初始化toConvert變量作爲字符串。

在管線50:

convertedTemp.returnedConversion(); 

convertedTemp是類型JTextField的。所以你不能在這裏訪問未定義的方法returnedConversion()。如果您嘗試在convertedTemp JTextField中顯示文本,則應使用convertedTemp.setText(applyIt)

+0

你是什麼意思「未定義的方法」? – Jremy

+0

在您的代碼中,您只定義了returnedConversion(string)not returnedConversion()。 – Vinesh

+0

爲什麼returnedConversion的方法頭文件在eclipse中指出我需要返回字符串類型的結果......是不是每個塊中的每個retun語句都在做什麼? – Jremy