내 프로그램이 실행되고 팝업창이 뜨지 만 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");
}
}
}
}
}
내 것보다 훨씬 더 완전한 대답입니다. 1 + –
오케이, 피드 백을 보내 주셔서 감사합니다. 사용자가 텍스트 상자에 입력 한 내용은 convertToC/F를 눌렀을 때 변환해야하는 내용입니다. 그래서 당신의 말은 버튼에 액션 리스너 만 사용해야합니다. 무엇을 개종해야할지 어떻게 알 수 있습니까? 텍스트 상자 오른쪽에 어떤 관계가 있어야합니까? – user3797341
이벤트 원본을 확인하는 논리를 볼 수 있습니까? 그것이 그 방법을 알고 있습니다. 당신은 이미 그것을하고 있고 아마 그것을 깨닫지 못합니다. –