Hello @ all,
wie muss ich mein Programm schreiben damit es auch Kommazahlen einlesen kann? Hier mein Quellcode:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
*
* Beschreibung
*
* @version 1.0 vom 06.10.2010
* @author
*/
public class BMI extends JFrame {
// Anfang Attribute
static double zahl1;
static double zahl2;
static double ergebnis;
private JLabel icon = new JLabel(new ImageIcon(„icon.jpg“));
private JLabel label1 = new JLabel();
private JLabel label2 = new JLabel();
private JLabel label3 = new JLabel();
private JButton button1 = new JButton();
private JTextField textField1 = new JTextField();
private JTextField textField2 = new JTextField();
private JTextField textField3 = new JTextField();
// Ende Attribute
public BMI(String title) {
// Frame-Initialisierung
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 275;
int frameHeight = 153;
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);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
icon.setBounds(237, 89, 32, 32);
cp.add(icon);
label1.setBounds(8, 8, 90, 16);
label1.setText(„Gewicht: „);
label1.setFont(new Font(„MS Sans Serif“, Font.PLAIN, 13));
cp.add(label1);
label2.setBounds(8, 32, 107, 16);
label2.setText(„Größe: „);
label2.setFont(new Font(„MS Sans Serif“, Font.PLAIN, 13));
cp.add(label2);
label3.setBounds(8, 88, 100, 16);
label3.setText(„Benötigte Liter: „);
label3.setFont(new Font(„MS Sans Serif“, Font.PLAIN, 13));
cp.add(label3);
button1.setBounds(64, 56, 100, 25);
button1.setLabel(„Berechnen“);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1_ActionPerformed(evt);
}
});
cp.add(button1);
textField1.setBounds(120, 8, 126, 24);
textField1.setText(““);
cp.add(textField1);
textField2.setBounds(120, 32, 126, 24);
textField2.setText(““);
cp.add(textField2);
textField3.setBounds(120, 88, 126, 24);
textField3.setText(““);
cp.add(textField3);
// Ende Komponenten
setResizable(false);
setVisible(true);
}
// Anfang Methoden
public void button1_ActionPerformed(ActionEvent evt) {
zahl1 = Integer.parseInt(textField1.getText());
zahl2 = Integer.parseInt(textField2.getText());
ergebnis=zahl1/(zahl2*zahl2);
textField3.setText(""+ergebnis);
}
// Ende Methoden
public static void main(String[] args) {
new BMI(„BMI“);
}
}
Ich bedanke mich schon mal für die Hilfe
LG Denni