import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
*
* Temperaturumrechner
*
* @version 1.0 vom 19.12.2012
* @
*/
public class temperaturumrechner extends JFrame {
// Anfang Attribute
private JLabel lb_Bild = new JLabel();
private ImageIcon lb_BildIcon = new ImageIcon("images/thermometer-clip-art-7.gif");
private JLabel lb_Ueberschrift = new JLabel();
private JCheckBox cb_Celsius = new JCheckBox();
private JCheckBox cb_Fahrenheit = new JCheckBox();
private JCheckBox cb_Kelvin = new JCheckBox();
private JLabel lb_Celsius = new JLabel();
private JNumberField zf_Celsius = new JNumberField();
private JLabel lb_Fahrenheit = new JLabel();
private JNumberField zf_Fahrenheit = new JNumberField();
private JLabel lb_Kelvin = new JLabel();
private JNumberField zf_Kelvin = new JNumberField();
private JLabel lb_Ausgangstemperatur = new JLabel();
private JNumberField zf_Ausgangstemperatur = new JNumberField();
private JButton sf_Berechnen = new JButton();
private JButton sf_Felderleeren = new JButton();
// Ende Attribute
public temperaturumrechner(String title) {
// Frame-Initialisierung
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 608;
int frameHeight = 618;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
lb_Bild.setBounds(368, 320, 203, 185);
lb_Bild.setText("");
lb_Bild.setIcon(lb_BildIcon);
cp.add(lb_Bild);
lb_Ueberschrift.setBounds(110, 16, 340, 57);
lb_Ueberschrift.setText("Temperaturumrechner");
lb_Ueberschrift.setForeground(Color.RED);
lb_Ueberschrift.setHorizontalAlignment(SwingConstants.CENTER);
lb_Ueberschrift.setFont(new Font("Arial", Font.BOLD, 28));
cp.add(lb_Ueberschrift);
cb_Celsius.setBounds(408, 104, 145, 25);
cb_Celsius.setText("Celsius");
cb_Celsius.setOpaque(false);
cb_Celsius.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(cb_Celsius);
cb_Fahrenheit.setBounds(408, 144, 153, 25);
cb_Fahrenheit.setText("Fahrenheit");
cb_Fahrenheit.setOpaque(false);
cb_Fahrenheit.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(cb_Fahrenheit);
cb_Kelvin.setBounds(408, 184, 145, 25);
cb_Kelvin.setText("Kelvin");
cb_Kelvin.setOpaque(false);
cb_Kelvin.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(cb_Kelvin);
lb_Celsius.setBounds(8, 464, 115, 41);
lb_Celsius.setText("Celsius in °C:");
lb_Celsius.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(lb_Celsius);
zf_Celsius.setBounds(168, 464, 169, 41);
zf_Celsius.setText("");
zf_Celsius.setFont(new Font("Arial", Font.BOLD, 14));
zf_Celsius.setEditable(false);
cp.add(zf_Celsius);
lb_Fahrenheit.setBounds(8, 352, 131, 41);
lb_Fahrenheit.setText("Fahrenheit in °F:");
lb_Fahrenheit.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(lb_Fahrenheit);
zf_Fahrenheit.setBounds(168, 352, 169, 41);
zf_Fahrenheit.setText("");
zf_Fahrenheit.setFont(new Font("Arial", Font.BOLD, 14));
zf_Fahrenheit.setEditable(false);
cp.add(zf_Fahrenheit);
lb_Kelvin.setBounds(8, 408, 131, 41);
lb_Kelvin.setText("Kelvin in °K:");
lb_Kelvin.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(lb_Kelvin);
zf_Kelvin.setBounds(168, 408, 169, 41);
zf_Kelvin.setText("");
zf_Kelvin.setFont(new Font("Arial", Font.BOLD, 14));
zf_Kelvin.setEditable(false);
cp.add(zf_Kelvin);
lb_Ausgangstemperatur.setBounds(8, 144, 197, 33);
lb_Ausgangstemperatur.setText("Bitte Temperatur eingeben:");
lb_Ausgangstemperatur.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(lb_Ausgangstemperatur);
zf_Ausgangstemperatur.setBounds(232, 136, 161, 41);
zf_Ausgangstemperatur.setText("");
zf_Ausgangstemperatur.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(zf_Ausgangstemperatur);
sf_Berechnen.setBounds(152, 248, 209, 49);
sf_Berechnen.setText("berechnen!");
sf_Berechnen.setMargin(new Insets(2, 2, 2, 2));
sf_Berechnen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
sf_Berechnen_ActionPerformed(evt);
}
});
sf_Berechnen.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(sf_Berechnen);
sf_Felderleeren.setBounds(152, 536, 201, 41);
sf_Felderleeren.setText("Felder leeren!");
sf_Felderleeren.setMargin(new Insets(2, 2, 2, 2));
sf_Felderleeren.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
sf_Felderleeren_ActionPerformed(evt);
}
});
sf_Felderleeren.setFont(new Font("Arial", Font.BOLD, 14));
cp.add(sf_Felderleeren);
setTitle("Temperaturumrechner");
// Ende Komponenten
setVisible(true);
} // end of public temperaturumrechner
// Anfang Methoden
public void sf_Berechnen_ActionPerformed(ActionEvent evt) {
//Benutzerangaben erfassen
double ausgangstemperatur= zf_Ausgangstemperatur.getDouble();
//Temperatur umrechnen
if (cb_Celsius.isSelected() && cb_Fahrenheit.isSelected()) {
JOptionPane.showMessageDialog(this,"Bitte nur eine Ausgangstemperatur auswählen!");
zf_Ausgangstemperatur.clear();
zf_Fahrenheit.clear();
zf_Kelvin.clear();
zf_Celsius.clear();
} // end of if
if (cb_Celsius.isSelected() && cb_Kelvin.isSelected()) {
JOptionPane.showMessageDialog(this,"Bitte nur eine Ausgangstemperatur auswählen!");
zf_Ausgangstemperatur.clear();
zf_Fahrenheit.clear();
zf_Kelvin.clear();
zf_Celsius.clear();
} // end of if
if (cb_Kelvin.isSelected() && cb_Fahrenheit.isSelected()) {
JOptionPane.showMessageDialog(this,"Bitte nur eine Ausgangstemperatur auswählen!");
zf_Ausgangstemperatur.clear();
zf_Fahrenheit.clear();
zf_Kelvin.clear();
zf_Celsius.clear();
} // end of if
else {
if (cb_Celsius.isSelected()); {
double fahrenheit= ((ausgangstemperatur*1.8)+32);
zf_Fahrenheit.setDouble(fahrenheit,2);
double kelvin= (ausgangstemperatur +273.15 );
zf_Kelvin.setDouble(kelvin,2);
zf_Celsius.setDouble(ausgangstemperatur,2);
} // end of if
if (cb_Kelvin.isSelected()) {
double celsius= (ausgangstemperatur -273.15);
zf_Celsius.setDouble(celsius,2);
double fahrenheit= ((ausgangstemperatur * 1.8) - 459.67);
zf_Fahrenheit.setDouble(fahrenheit,2);
zf_Kelvin.setDouble(ausgangstemperatur,2);
} // end of if
if (cb_Fahrenheit.isSelected()) {
double celsius= ((ausgangstemperatur-32)/ 1.8);
zf_Celsius.setDouble(celsius,2);
double kelvin= ((ausgangstemperatur + 459.67)/1.8);
zf_Kelvin.setDouble(kelvin,2);
zf_Fahrenheit.setDouble(ausgangstemperatur,2);
} // end of if
} // end of if-else
} // end of sf_Berechnen_ActionPerformed
public void sf_Felderleeren_ActionPerformed(ActionEvent evt) {
//Felder leeren
zf_Ausgangstemperatur.clear();
zf_Fahrenheit.clear();
zf_Kelvin.clear();
zf_Celsius.clear();
} // end of sf_Felderleeren_ActionPerformed
// Ende Methoden
public static void main(String[] args) {
new temperaturumrechner("Temperaturrechner");
} // end of main
} // end of class temperaturumrechner