温度转换GUI问题

时间:2013-11-28 03:38:09

标签: java swing classcastexception

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package temperature.conversion;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.JFrame;

import java.text.DecimalFormat;

/**
 *
 * @author Charles
 */
public class TemperatureConversion extends JFrame {

   private JTextField inputField;
   private JTextField outputField;
   private JRadioButton celstofahr;
   private JRadioButton fahrtocels;
   private JRadioButton celstokelv;
   private JRadioButton kelvtocels;
   private JRadioButton fahrtokelv;
   private JRadioButton kelvtofahr;
   private JButton converter;
   private ButtonGroup tempGroup;

   public TemperatureConversion() {
       super("Temperature Conversion Table");
       setLayout(new FlowLayout());

        celstofahr = new JRadioButton("Celsius to Fahrenheit", true);
       fahrtocels = new JRadioButton("Fahrenheit to Celsius", false);
       celstokelv = new JRadioButton("Celsius to Kelvin", false);
       kelvtocels = new JRadioButton("Kelvin to Celsius", false);
       fahrtokelv = new JRadioButton("Fahrenheit to Kelvin", false);
       kelvtofahr = new JRadioButton("Kelvin to Fahrenheit", false);

       add(celstofahr);
       add(fahrtocels);
       add(celstokelv);
       add(kelvtocels);
       add(fahrtokelv);
       add(kelvtofahr);

       tempGroup = new ButtonGroup();
       tempGroup.add(celstofahr);
       tempGroup.add(fahrtocels);
       tempGroup.add(celstokelv);
       tempGroup.add(kelvtocels);
       tempGroup.add(fahrtokelv);
       tempGroup.add(kelvtofahr); 

       inputField = new JTextField(" ", 4);
       inputField.addActionListener((ActionListener) this);
       converter = new JButton("Convert");
       converter.addActionListener(
         new ActionListener()
         {
    public void actionPerformed(ActionEvent event)
    {
        Object source = event.getSource();

      if(source ==  inputField) {
        double init_temp = 0.000;
        double final_temp = 0.00;
           DecimalFormat three = new DecimalFormat("0.000");

     String string_temp = inputField.getText();
     init_temp = Double.parseDouble(string_temp);

     if(celstofahr == source){
         final_temp = ((5/9) * init_temp) - 32;     
         outputField.setText(String.valueOf(final_temp));
     } 
     else if(fahrtocels == source){
         final_temp = ((1.8) * init_temp) + 32;
         outputField.setText(String.valueOf(final_temp));
     }
     else if(celstokelv == source){
         final_temp = init_temp + 273.16;    
         outputField.setText(String.valueOf(final_temp));
     }
     else if(kelvtocels == source){
         final_temp = init_temp - 273.16;    
         outputField.setText(String.valueOf(final_temp));
     }
     else if(fahrtokelv == source){
         final_temp = ((5/9) * (init_temp-32)) + 273.16; 
         outputField.setText(String.valueOf(final_temp));
     }
     else if(kelvtofahr != source){
     } else {
            final_temp = ((1.8) * (init_temp+32)) - 273.16;
            outputField.setText(String.valueOf(final_temp));
        }
     outputField.setText(" " + three.format(final_temp)); 
               }
             }
         }
      );
     add(converter);
   }
}

package temperature.conversion;

import javax.swing.JFrame;

/**
 *
 * @author Charles
 */
public class MainClass2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TemperatureConversion tempconversion = new TemperatureConversion();
        tempconversion.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tempconversion.setSize(600, 600);
        tempconversion.setVisible(true);
    }
}

这是麻烦:

run:
Exception in thread "main" java.lang.ClassCastException: temperature.conversion.TemperatureConversion cannot be cast to java.awt.event.ActionListener
    at temperature.conversion.TemperatureConversion.<init>(TemperatureConversion.java:61)
    at temperature.conversion.MainClass2.main(MainClass2.java:19)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

1 个答案:

答案 0 :(得分:1)

您使用了以下行,这没有任何意义

inputField.addActionListener((ActionListener) this);
         //This is the result of ClassCastException

但是,您没有使用ActionListener实施JFrame。您应该使用ActionListener实施JFrame并覆盖actionPerformed。您可以使用event.getSoruce收听特定的操作组件,以执行特定的操作。