0
/*
* 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);
}
}
Вот беда:температуры GUI Проблема преобразования
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)
'/ * * Чтобы изменить этот шаблон, выберите« Инструменты »| Шаблоны * и откройте шаблон в редакторе. */'Если вы не собираетесь редактировать это до более разумного, пожалуйста, обрезайте его из источника, предназначенного для SO. Это шум. –
См. Также ['Конвертер'] (http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html#eg). – trashgod