TemperatureConverter (english)
Today we want to program a very simple user interface in
Java that converts the conversion of the temperatures from degrees Fahrenheit
to degrees Celsius and vice versa.
For programming or developing the program, I have used a
special tool which we will share with you on Friday and will publish an example
about it.
In the following program we have designed a graphical
user interface with buttons and text areas. If the user enters the temperature
to be converted in the respective position, this is converted into the desired
unit and outputted. However, if the user does not enter a number but a
character, an error message is triggered. In this error message, the user is
informed that they should pay attention to entering numerical values on the
next input.
The source file needed for this program looks like this:
I have also uploaded it for you here.
public TemperaturUmrechner() {
setResizable(false);
setFont(new Font("Arial", Font.PLAIN, 12));
setTitle("Temperature Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,
100, 325, 215);
contentPane = new JPanel();
contentPane.setBackground(Color.YELLOW);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txtGradCelsius = new JTextField();
txtGradCelsius.setText("0");
txtGradCelsius.setBounds(10, 30, 140, 20);
contentPane.add(txtGradCelsius);
txtGradCelsius.setColumns(10);
txtAntwortFahrenheit = new JTextField();
txtAntwortFahrenheit.setColumns(10);
txtAntwortFahrenheit.setBounds(160, 30, 140, 20);
contentPane.add(txtAntwortFahrenheit);
txtGradFahrenheit = new JTextField();
txtGradFahrenheit.setText("0");
txtGradFahrenheit.setColumns(10);
txtGradFahrenheit.setBounds(10, 92, 140, 20);
contentPane.add(txtGradFahrenheit);
txtAntwortCelsius = new JTextField();
txtAntwortCelsius.setColumns(10);
txtAntwortCelsius.setBounds(160, 92, 140, 20);
contentPane.add(txtAntwortCelsius);
JLabel lblNewLabel = new JLabel("Temperature in \u00B0C");
lblNewLabel.setBounds(10, 11, 140,
14);
contentPane.add(lblNewLabel);
JLabel lblTemperaturInf = new JLabel("Temperature in \u00B0F");
lblTemperaturInf.setBounds(160, 11,
140, 14);
contentPane.add(lblTemperaturInf);
JLabel
label = new JLabel("Temperature in \u00B0F");
label.setBounds(10,
72, 140, 14);
contentPane.add(label);
JLabel
label_1 = new JLabel("Temperatur in \u00B0C");
label_1.setBounds(160,
72, 140, 14);
contentPane.add(label_1);
JButton
btnBerechnen = new JButton("Calculate");
btnBerechnen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
e) {
try {
// Calculate from Degree Celsius to Degree
Fahrenheit
double celsius1, fahrenheit1;
celsius1
= Double.parseDouble(txtGradCelsius.getText());
fahrenheit1 = (1.5 * celsius1) + 32;
txtAntwortFahrenheit.setText(""+fahrenheit1);
// Calculate from Degree Fahrenheit to Degree
Celsius
double celsius2, fahrenheit2;
fahrenheit2 = Double.parseDouble(txtGradFahrenheit.getText());
celsius2 = (1.5 * fahrenheit2)
- (160/9);
txtAntwortCelsius.setText(""+celsius2);
}
catch(NumberFormatException keineGueltigeZahl) {
JOptionPane.showMessageDialog(null, "You have not entered a numerical value!");
}
}
});
btnBerechnen.setBounds(10, 135, 140,
25);
contentPane.add(btnBerechnen);
JButton btnEnde = new JButton("Ende");
btnEnde.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
e) {
System.exit(0);
}
});
btnEnde.setBounds(160,
136, 140, 25);
contentPane.add(btnEnde);
}
Kommentare
Kommentar veröffentlichen