2014-07-20 3 views
1

내 프로그램이 실행되고 팝업창이 뜨지 만 convertoF 변환 버튼을 클릭 할 때 사용자 입력을 * c 또는 * f로 변환하는 계산을 평가하지 않습니다. 별도의 액션 리스너에 코드를 넣으려고했지만 오류가 발생합니다. 그것은 논리적으로 올바르게 보이지만 어쩌면 잘못된 코드에서 일부 코드를 잘못 배치했을 수도 있습니다. 누구든지 나를 도울 수 있습니까?액션 리스너와 함께 메소드를 사용하는 방법은 무엇입니까?

package edu.westga.TemperatureConverter.controller; 

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; 

/** 
* 
* @author 
* 
*/ 
public class Temperature extends JFrame { 
    private double fahrenheit; 
    private double celsius; 
    private JButton fahrenheitButton; 
    private JButton celsiusBUtton; 
    private JTextField textBox; 
    private JLabel instructions; 
    private FlowLayout layout; 
    private Container container; 
    private JButton clearButton; 
    private JLabel results; 

    /** 
    * Temperature constructor 
    */ 
    public Temperature() { 
     super("temperature converter"); 
     this.layout = new FlowLayout(); 
     this.container = getContentPane(); 
     setLayout(this.layout); 

     TemperatureConverter convertemp = new TemperatureConverter(); 
     this.instructions = new JLabel("please enter the Temperture:"); 
     add(this.instructions); 
     this.textBox = new JTextField(10); 
     add(this.textBox); 
     this.textBox.addActionListener(convertemp); 

     this.fahrenheitButton = new JButton(" convert to Fahrenheit"); 
     add(this.fahrenheitButton); 
     // fahrenheitButton.addActionListener(new ActionListener() { 
     // public void actionPerformed(ActionEvent event) { 
     // 
     // convertToC(); 
     // } 
     // }); 

     this.celsiusBUtton = new JButton("convert to Celsius "); 
     add(this.celsiusBUtton); 
     // celsiusBUtton.addActionListener(new ActionListener() { 
     // public void actionPerformed(ActionEvent event) { 
     // String text = ""; 
     // double num = 0; 
     // if (event.getSource() == celsiusBUtton) { 
     // num = Double.parseDouble(text); 
     // convertToC(num); 
     // results.setText("your aswer was converted to:" + num); 
     // }} 
     // }); 

     this.clearButton = new JButton(" clear"); 
     add(this.clearButton); 
     // clearButton.addActionListener(new ActionListener() { 
     // public void actionPerformed(ActionEvent event) { 
     // 
     // convertToC(); 
     // } 
     // }); 

     this.results = new JLabel("your aswer was converted to: "); 
     add(this.results); 
    } 

    /** 
    * convert to celsius 
    * 
    * @param num 
    *   user input number 
    * @return converted answer the answer 
    */ 
    public double convertToC(double num) { 
     this.fahrenheit = num; 
     double convertedAnswer; 
     convertedAnswer = 5.0/9.0 * (this.fahrenheit - 32); 
     return convertedAnswer; 
    } 

    /** 
    * Convert to fahrenheit 
    * 
    * @param num 
    *   user input number 
    * @return converted answer the answer 
    */ 
    public double convertToF(double num) { 
     this.celsius = num; 
     double convertedAnswer; 
     convertedAnswer = (32 + 5/9) * this.celsius; 
     return convertedAnswer; 
    } 

    /** 
    * clears the window of previous numbers 
    */ 
    public void clear() { 

    } 

    /** 
    * anonymous class that activates the conversion of the users input 
    * 
    * @author 
    * @version 
    * 
    */ 
    public class TemperatureConverter implements ActionListener { 

     public void actionPerformed(ActionEvent event) { 
      String text = ""; 
      double num = 0; 
      if (event.getSource() == celsiusBUtton) { 
       num = Double.parseDouble(text); 
       convertToC(num); 
       results.setText("your answer was converted to:" + num + "C"); 
      } else { 
       if (event.getSource() == fahrenheitButton) { 
        num = Double.parseDouble(text); 
        convertToF(num); 
        results.setText("your answer was converted to:" + num + "F"); 
       } 
      } 
     } 

    } 
} 

답변

4

당신은 잘못 여기에 몇 가지가 있습니다

1) 당신은 당신의 ActionListener

if (event.getSource() == celsiusBUtton) { 
     num = Double.parseDouble(text); 
     num = convertToC(num); 
     results.setText("your answer was converted to:" + num + "C"); 
} else { 
     if (event.getSource() == fahrenheitButton) { 
      num = Double.parseDouble(text); 
      num = convertToF(num); 
      results.setText("your answer was converted to:" + num + "F"); 
     } 
} 

2)에서 계산 된 값을 저장하지 않는 당신은 액션 청취자에 넥타이를하지 않습니다 귀하의 버튼. 당신의 ActionListener에서

this.fahrenheitButton = new JButton(" convert to Fahrenheit"); 
this.fahrenheitButton.addActionListener(convertemp); // make them do something!! 
add(this.fahrenheitButton); 


this.celsiusBUtton = new JButton("convert to Celsius "); 
this.celsiusBUtton.addActionListener(convertemp); // make them do something!! 
add(this.celsiusBUtton); 

3), 텍스트는 더 많은 오류를 수행 할 것이지만 (텍스트 상자에서 텍스트를 가져옵니다 당신에게 NumberFormatException 을 줄 것이다 당신이 갈 수 있어야하는 ","항상 아마 또한 ActionListener 당신이 그것을에 배치 개체에 대한 다른 위젯에 의해 만들어진 작업을 수신되지 않는다는 것을 지적해야

public void actionPerformed(ActionEvent event) { 
    String text = textBox.getText(); // you need to get the text from the text box 
    double num = 0; 
    if (event.getSource() == celsiusBUtton) { 
      num = Double.parseDouble(text); 
      num = convertToC(num); 
      results.setText("your answer was converted to:" + num + "C"); 
    } else { 
     if (event.getSource() == fahrenheitButton) { 
       num = Double.parseDouble(text); 
       num = convertToF(num); 
       results.setText("your answer was converted to:" + num + "F"); 
     } 
    } 
} 

)이보다 확인, 그것은 행동 위젯에 을 수신합니다. 텍스트 상자에 놓으면 청취자가 단추가 아닌 텍스트 상자에서 이벤트를 수신합니다. 사용자가 "Enter"키 또는 사용자가 그렇게 할 경향이있는 경우 사용자의 메서드가 "Enter"키를 잡는 데 유용 할 수 있습니다.

+1

내 것보다 훨씬 더 완전한 대답입니다. 1 + –

+0

오케이, 피드 백을 보내 주셔서 감사합니다. 사용자가 텍스트 상자에 입력 한 내용은 convertToC/F를 눌렀을 때 변환해야하는 내용입니다. 그래서 당신의 말은 버튼에 액션 리스너 만 사용해야합니다. 무엇을 개종해야할지 어떻게 알 수 있습니까? 텍스트 상자 오른쪽에 어떤 관계가 있어야합니까? – user3797341

+0

이벤트 원본을 확인하는 논리를 볼 수 있습니까? 그것이 그 방법을 알고 있습니다. 당신은 이미 그것을하고 있고 아마 그것을 깨닫지 못합니다. –

관련 문제