2014-11-05 180 views
-1

我無法弄清楚爲什麼我的代碼不工作。我對GUI編程和Java也很陌生,在創建GUI程序的格式上我仍然有點粗糙。在代碼中,我試圖將攝氏溫度轉換爲華氏溫度,反之亦然。任何幫助,將不勝感激。謝謝!攝氏溫度到華氏度轉換器GUI程序

import java.awt.Container; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 

@SuppressWarnings("serial") 
public class myGUIClass<FahrenheitButtonHandler> extends JFrame{ 

    private JLabel msgCelsius; 
    private JLabel msgFahrenheit; 
    private JButton btnCelsius; 
    private JButton btnFahrenheit; 
    private static JTextField fldCelsius; 
    private static JTextField fldFahrenheit; 
    Container contain; 


    public myGUIClass(String myGUIWindow){ 
     super("myGui"); 
     contain = getContentPane(); 
     contain.setLayout(new FlowLayout()); 

     msgCelsius = new JLabel("Degrees in Celsius"); 
     btnCelsius = new JButton("Convert From Celsius to Fahrenheit"); 
     fldCelsius = new JTextField(15); 

     msgFahrenheit = new JLabel("Degrees in Fahrenheit "); 
     btnFahrenheit = new JButton("Convert From Fahrenheit to Celsius"); 
     fldFahrenheit = new JTextField(15); 

     contain.add(msgCelsius); 
     contain.add(fldCelsius); 
     contain.add(btnCelsius); 

     contain.add(msgFahrenheit); 
     contain.add(fldFahrenheit); 
     contain.add(btnFahrenheit); 

     CelsiusButtonHandler btnHandlerCelsius = new CelsiusButtonHandler(); 
     btnCelsius.addActionListener(btnHandlerCelsius); 

     FahrenheitButtonHandler btnHandlerFahrenheit = new FahrenheitButtonHandler(); 
     btnFahrenheit.addActionListener(btnHandlerFahrenheit); 

     setSize(400,200); 
     setVisible(true); 


    }//end method 

    private class CelsiusButtonHandler implements ActionListener{ 
     //@Override 

     //implement the listener interface methods to process the events 
     public void actionPerformed(ActionEvent ae){ 

      Integer celsius; 
      Integer fahrenheit; 

      try{ 
       if (ae.getSource() == btnCelsius){ 
        celsius = Integer.parseInt(fldCelsius.getText()); 
        fahrenheit = Math.round((9 /(float)5)) * (celsius + 32); 
        fldFahrenheit.setText(fahrenheit.toString()); 
       }//end if 
      }//end try 

      catch (Exception e){ 
       fldFahrenheit.setText(""); 
      }//end catch 
    }//end inner class 

     }//end class 

    private class FahrenheitButtonHandler implements ActionListener{ 
     public void actionPerformed(ActionEvent a){ 
      Integer fahrenheit1; 
      Integer celsius1; 

      try{ 
       if(a.getSource()== btnFahrenheit){ 
        fahrenheit1 = Integer.parseInt(fldFahrenheit.getText()); 
        celsius1 = Math.round((5/(float)9)) * (fahrenheit1 - 32); 
        fldCelsius.setText(celsius1.toString()); 
        }//end if 
      }//end try 

      catch (Exception e){ 
       fldCelsius.setText(""); 
      }//end catch 

     }//end method 
     }//end private class 


    public static void main (String[] args){ 
     @SuppressWarnings("rawtypes") 
     myGUIClass guiClass = new myGUIClass(null); 
     guiClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    }//end main 
    }//end outer class 

     //theres a problem with the math in these lines: 
     //am i not casting these correctly? whenever i input 50 i'm supposed to get 122 but i get 164. 
     //fahrenheit = Math.round((9 /(float)5)) * (celsius + 32); 
     //celsius1 = Math.round((5/(float)9)) * (fahrenheit1 - 32); 
+5

而問題是....? – MadProgrammer 2014-11-05 23:37:55

+0

當我運行它並嘗試輸入華氏溫度時,它不會在攝氏度的文本字段中顯示任何內容。相反的問題相反 – ekep23 2014-11-05 23:42:05

+0

究竟是什麼問題?它不轉換嗎?它不會編譯?爲什麼你的類在聲明中有一個通用的,爲什麼你在事後抑制原始類型的警告? – Zymus 2014-11-05 23:42:14

回答

1

忽略代碼問題,使uncompliable的例子...

你有一個整數除法問題...

celsius = (5/9) * (fahrenheit - 32); 

如果5/9 = 0所得到的值轉換爲一個整數。

嘗試使用更多的東西一樣......

celsius = Math.round((5/(float)9)) * (fahrenheit - 32); 

現在,就個人而言,我會用doublefloat而不是int和格式化的結果,但就是我。你需要做同樣的事情FahrenheitButtonHandler

在你FahrenheitButtonHandler類,你也將錯誤值文本字段...

celsius1 = Integer.parseInt(fldFahrenheit.getText()); 
fahrenheit1 = celsius1*(9/5)+32; 
fldCelsius.setText(celsius1.toString()); 

您應用celsius1值,這是值從fldFahrenheit領域的精華,而不是計算結果,應該是

fldCelsius.setText(fahrenheit1.toString()); 

...但是請記住,還有你需要糾正這種情況的整數除法問題... ...

最後,你註冊了錯誤的ActionListenerbtnFahrenheit

FahrenheitButtonHandler btnHandlerFahrenheit = new FahrenheitButtonHandler(); 
btnFahrenheit.addActionListener(btnHandlerCelsius); // <-- Wrong listener... 

它應該是...

FahrenheitButtonHandler btnHandlerFahrenheit = new FahrenheitButtonHandler(); 
btnFahrenheit.addActionListener(btnHandlerFahrenheit);