2013-11-15 51 views
0

我想要做的是讓它在文本字段中輸入的值可以互動,但它總是涉及到我試圖從字符串轉換爲雙倍。我是Comp Sci I的HS大一新生,這非常令人沮喪,因爲我在這方面做了很多想法。請看看這個:如何讓我的GUI度量轉換器正常工作?

import java.awt.Color; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import static javax.swing.JFrame.EXIT_ON_CLOSE; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 

public class degConverter extends JFrame implements ActionListener { 

    private JLabel welcome; 
    private JTextField f, c; 
    private JButton fI, cI; 
    private JButton reset, convertF, convertC; 

    public degConverter() { 
     Container contentPane = getContentPane(); 
     contentPane.setLayout(null); 
     contentPane.setBackground(Color.red.darker()); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setTitle("Celsius-Fahrenheit/Fahrenheit-Celsius Converter."); 
     setSize(900, 750); 
     setResizable(false); 
     setLocation(500, 200); 

     welcome = new JLabel("Welcome to the amazing degree converter!"); 
     welcome.setBounds(320, 50, 300, 40); 
     contentPane.add(welcome); 

     fI = new JButton("Convert from Fahrenheit to Celsius"); 
     fI.setBounds(100, 150, 275, 40); 
     fI.addActionListener(this); 
     contentPane.add(fI); 

     cI = new JButton("Convert from Celsius to Fahrenheit"); 
     cI.setBounds(525, 150, 275, 40); 
     cI.addActionListener(this); 
     contentPane.add(cI); 

     f = new JTextField("Give me a value here"); 
     f.addActionListener(this); 
     contentPane.add(f); 

     c = new JTextField("Give me a value here"); 
     c.addActionListener(this); 
     contentPane.add(c); 

     reset = new JButton("RESET DIS MOFUCKIN SHIT"); 
     reset.setBounds(325, 550, 220, 40); 
     reset.addActionListener(this); 
     contentPane.add(reset); 

     convertF = new JButton("CONVERT!!!"); 
     convertF.addActionListener(this); 
     contentPane.add(convertF); 

     convertC = new JButton("CONVERT!!!"); 
     convertC.addActionListener(this); 
     contentPane.add(convertC); 

     String s = c.getText(); 
     double cIn = Double.parseDouble(s); 
     double fOut = ((1.8) * (cIn)) + 32; 
     String s2 = f.getText(); 
     double fIn = Double.parseDouble(s2); 
     double cOut = ((fIn - 32)/1.8); 

    } 

    public static void main(String args[]) { 
     degConverter frame = new degConverter(); 
     frame.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() instanceof JButton) { 
      JButton clicked = (JButton) e.getSource(); 
      { 
       if (fI == clicked) { 
        fI.setText("Insert value in box below"); 
        f.setBounds(100, 275, 250, 40); 
        convertF.setBounds(100, 400, 150, 40); 
       } 
      } 
      if (e.getSource() instanceof JButton) { 
       if (cI == clicked) { 
        cI.setText("Insert value in the box below"); 
        c.setBounds(525, 275, 250, 40); 
        convertC.setBounds(525, 400, 150, 40); 
       } 
      } 
      if (reset == clicked) { 
       fI.setText("Convert from Fahrenheit to Celsius"); 
       cI.setText("Convert from Celsius to Fahrenheit"); 
       f.setBounds(1000, 1000, 0, 0); 
       c.setBounds(1000, 1000, 0, 0); 
      } 
      if (convertF == clicked) { 
       f.setText(cOut); 
      } 
      if (convertC == clicked) { 
       f.setText(fOut); 
      } 
     } 
    } 
} 

我在這裏試圖做的似乎不工作。幫幫我?提前致謝。很抱歉,很長的節目。

+2

嘗試將您的代碼縮小爲顯示問題的最小示例。並改變你的按鈕的標題... – Floris

+2

'null'佈局不是你的朋友 – MadProgrammer

+0

不要把你所有的代碼放在一個動作監聽器中。爲每個組件創建一個'ActionListener'或'Action'實例並在每個實例中放置相應的代碼 – vandale

回答

0

代碼

String s = c.getText(); 
    double cIn = Double.parseDouble(s); 
    double fOut = ((1.8)*(cIn))+32; 
    String s2 = f.getText(); 
    double fIn = Double.parseDouble(s2); 
    double cOut = ((fIn-32)/1.8); 

用戶來得及輸入任何內容之前被調用。你需要改變的東西,以便按下「轉換」按鈕調用此代碼 - 如果初始值爲c,則只有第一位(c到f),如果初始值是華氏度,則只有第二位(f到c)。

+1

是的,'actionPerformed()'和'degConverter'構造函數之間有區別 – clwhisk