如何使用动作监听器的方法?

时间:2014-07-20 19:45:04

标签: java swing user-interface actionlistener

我的程序运行并弹出窗口,但是当他们点击converToF按钮的convertoC时,它不会评估将用户输入转换为* 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 个答案:

答案 0 :(得分: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)你不要将动作监听器绑在你的按钮上。

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)在你的actionlistener中,你的文字总是"",它会给你一个NumberFormatException 从你的文本框中获取文本,你应该很高兴(尽管你会想要做更多的错误检查...)

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");
        }
    }
}

我还应该指出,ActionListener并非正在侦听其他小部件对您放置的对象所做的操作,它正在侦听 上的操作小部件。您已将其放在文本框中,这将使听众听到文本框上的事件,而不是按钮。您的方法可能对于捕获"输入"如果用户倾向于这样做,那就是关键或其他事情。