Halli Hallo ihr lieben,
ich will einen BMI rechner programmieren, hab lange gegoogelt hat mit aber nichts gebracht. Ich will einen Slider (Schieberegler) programmieren der das Gewicht verändert bitte kann mir da jemand helfen, ich habe nicht mehr so viel Zeit. Hier ist der Quellcode
ich danke schon im vorraus
SUSI
ich will einen BMI rechner programmieren, hab lange gegoogelt hat mit aber nichts gebracht. Ich will einen Slider (Schieberegler) programmieren der das Gewicht verändert bitte kann mir da jemand helfen, ich habe nicht mehr so viel Zeit. Hier ist der Quellcode
Code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* Beschreibung
*
* @version 1.0 vom 22.06.2007
* @author
*/
public class NewCreation extends JFrame {
// Anfang Variablen
private JLabel jlblTitel1 = new JLabel();
private JLabel jlblTitel2 = new JLabel();
private JLabel jlblDate = new JLabel();
private JTextField jtxtDate = new JTextField();
private JLabel jlblWeight = new JLabel();
private JTextField jtxtWeight = new JTextField();
private JLabel jlblHeight = new JLabel();
private JTextField jtxtHeight = new JTextField();
private JLabel jlblBMI = new JLabel();
private JLabel jlblBMIA = new JLabel();
private JLabel jlblComment = new JLabel();
private JLabel jlblCommentA = new JLabel();
private JButton jbtnCalculate = new JButton();
private JButton jbtnReset = new JButton();
private JButton jbtnSpeichern = new JButton();
private DlgNochmal dlg=new DlgNochmal(this,"Nochmal?",true);
private JPanel jPanel1 = new JPanel(null);
private double height = 0.0;
private double weight = 0.0;
private double bmi = 0.0;
private JLabel jlblAnweisung = new JLabel();
static JSlider jSlider1 = new JSlider();
private String Datum = this.jtxtDate.getText();
// Ende Variablen
public NewCreation(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); }
});
int frameWidth = 417;
int frameHeight = 471;
setSize(frameWidth, frameHeight);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
jlblTitel1.setBounds(16, 8, 39, 16);
jlblTitel1.setText("BMI");
jlblTitel1.setFont (new Font("Arial", Font.BOLD, 17));
jlblTitel1.setForeground(Color.RED);
cp.add(jlblTitel1);
jlblTitel2.setBounds(56, 8, 143, 16);
jlblTitel2.setText("Body Mass Index");
jlblTitel2.setFont (new Font("Arial", Font.BOLD, 17));
cp.add(jlblTitel2);
jlblDate.setBounds(16, 40, 77, 15);
jlblDate.setText("Datum");
jlblDate.setFont (new Font("Arial", Font.PLAIN, 12));
cp.add(jlblDate);
jtxtDate.setBounds(16, 72, 81, 24);
jtxtDate.setText("");
cp.add(jtxtDate);
jlblWeight.setBounds(16, 112, 68, 15);
jlblWeight.setText("Gewicht (kg)");
jlblWeight.setFont (new Font("Arial", Font.PLAIN, 12));
cp.add(jlblWeight);
jtxtWeight.setBounds(56, 168, 33, 24);
jtxtWeight.setText("50");
cp.add(jtxtWeight);
jlblHeight.setBounds(16, 208, 80, 15);
jlblHeight.setText("Height (cm)");
jlblHeight.setFont (new Font("Arial", Font.PLAIN, 12));
cp.add(jlblHeight);
jtxtHeight.setBounds(16, 232, 81, 24);
jtxtHeight.setText("");
cp.add(jtxtHeight);
jlblBMI.setBounds(16, 328, 20, 15);
jlblBMI.setText("BMI");
jlblBMI.setFont (new Font("Arial", Font.PLAIN, 12));
cp.add(jlblBMI);
jlblBMIA.setBounds(107, 328, 149, 16);
jlblBMIA.setText("");
jlblBMIA.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
cp.add(jlblBMIA);
jlblComment.setBounds(16, 352, 65, 15);
jlblComment.setText("Kommentar");
jlblComment.setFont (new Font("Arial", Font.PLAIN, 12));
cp.add(jlblComment);
jlblCommentA.setBounds(105, 352, 149, 16);
jlblCommentA.setText("");
jlblCommentA.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
cp.add(jlblCommentA);
jbtnCalculate.setBounds(16, 400, 113, 33);
jbtnCalculate.setFont (new Font("Arial", Font.PLAIN, 12));
jbtnCalculate.setText("Berechnen");
cp.add(jbtnCalculate);
jPanel1.setBounds(304, 216, 185, 177);
cp.add(jPanel1);
jlblAnweisung.setBounds(155, 64, 341, 24);
jlblAnweisung.setText("");
jlblAnweisung.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
cp.add(jlblAnweisung);
jSlider1.setBounds(16, 136, 113, 25);
cp.add(jSlider1);
jbtnReset.setBounds(288, 400, 113, 33);
jbtnReset.setFont (new Font("Arial", Font.PLAIN, 12));
jbtnReset.setText("Löschen");
cp.add(jbtnReset);
jbtnSpeichern.setBounds(150, 400, 113, 33);
jbtnSpeichern.setFont (new Font("Arial", Font.PLAIN, 12));
jbtnSpeichern.setText("Speichern");
cp.add(jbtnSpeichern);
// Ende Komponenten
jbtnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jbtnCalculateActionPerformed(evt);
}
});
jbtnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jbtnResetActionPerformed(evt);
}
});
jbtnSpeichern.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jbtnSpeichernActionPerformed(evt);
}
});
setResizable(false);
setVisible(true);
}
// Anfang Ereignisprozeduren
public void jbtnCalculateActionPerformed(ActionEvent evt) {
// Daten lesen
String A = this.jtxtHeight.getText().replace(",",".");
try {
weight = Double.parseDouble(this.jtxtWeight.getText());
height = Double.parseDouble(A);
}catch(NumberFormatException e){
}
bmi = this.Calculate(weight, height);
bmi = Math.round(bmi*100.)/100.;
jlblBMIA.setText(Double.toString(bmi));
//Comment
if(bmi < 18.5){
jlblCommentA.setText("Untergewicht");
}
if(bmi > 18.5 && bmi <= 25){
jlblCommentA.setText("Normalgewicht");
}
if(bmi > 25 && bmi <= 30){
jlblCommentA.setText("Präadipositas");
}
if(bmi > 30 && bmi <= 35){
jlblCommentA.setText("Adipositas Grad I");
}
if(bmi > 35 && bmi <= 40){
jlblCommentA.setText("Adipositas Grad II");
}
if(bmi > 40){
jlblCommentA.setText("Adipositas Grad III");
}
}
private double Calculate(double w, double h) {
String Gewicht = this.jtxtWeight.getText();
String Groesse = this.jtxtHeight.getText();
String Bmi = this.jlblBMIA.getText();
double ret = 0;
try{
boolean texteOk = (Gewicht.length() >0)
&& (Groesse.length() >0)
&&(Datum.length()>0);
boolean allesOk = texteOk;
if(!allesOk) {
jlblAnweisung.setText("Bitte Felder korrekt ausfüllen !!!");
jlblAnweisung.setForeground(Color.red);
}
}catch(NumberFormatException e){}
// BMI = Gewicht / Größe²
ret = w / (h * h); System.out.println(w); System.out.println(h);
return ret;
}
public void jbtnResetActionPerformed(ActionEvent evt) {
this.Reset();
}
public void jbtnSpeichernActionPerformed(ActionEvent evt) {
if(jtxtDate.getText().length()<10){
jlblDate.setText("Falsches Datum!!!!!!!");
}
// Speichern:
File verz = new File("Projekt");
if(!verz.exists()){
verz.mkdir();
}
File datei = new File(verz.getPath()+ "/Letzte Berechnungen.doc");
try {
RandomAccessFile raf = new RandomAccessFile(datei, "rw");
raf.seek(raf.length());
raf.writeBytes("Datum: "+this.jtxtDate.getText().substring(0, 10)+";"+"\n"
+"Height: "+this.jtxtHeight.getText()+" m" +";"+"\n"
+"Weight: "+this.jtxtWeight.getText()+"kg"+";"+"\n"+"BMI: "
+this.jlblBMIA.getText()+"\n"+"Comment: "
+this.jlblCommentA.getText()+"\n\r\n");
raf.close();
}catch(NumberFormatException e){
}catch(IOException e){}
//Dialog nochmal?:
this.dlg.setVisible(true);
if(this.dlg.antwort==true){
this.Reset();
}else{
System.exit(0);
}
}
private void Reset() {
this.jtxtHeight.setText("");
this.jlblBMIA.setText("");
this.jlblCommentA.setText("");
this.setBackground(Color.lightGray);
this.jlblDate.setText("Current Date");
this.jlblDate.setForeground(Color.black);
this.jlblAnweisung.setText("");
}
// Ende Ereignisprozeduren
public static void main(String[] args) {
new NewCreation("NewCreation");
}
}
ich danke schon im vorraus
SUSI