Ich arbeite an meinem BMI-Rechner-Projekt, in dem ich diese JButton namens "Clear", die den gesamten Text aus dem JTextField löschen wird. Ich habe versucht feetText.setText("");
, aber es hat nicht funktioniert. Was kann ich möglicherweise tun?Klartext innerhalb der JTextField in Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BMI extends JFrame implements ActionListener {
public static void main(String[] args) {
BMI runner = new BMI();
runner.setSize(450, 600);
runner.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
runner.setVisible(true);
}
JTextField feetText, inchesText, weightText, waistText, neckText, hipsText;
JLabel feet, height,weightLabel, pounds, waist, neck, gender, maleLabel, femaleLabel;
JLabel inches, inches2, inches3, inches4, info1, info2, info3, info4;
JLabel activityLevel, sedentaryLabel, moderateLabel, activeLabel;
JLabel bmi, ratio, pbf, hips, lbm;
JRadioButton male, female, sedentary, moderate, active;
JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8;
JPanel panel9, panel10, panel11, panel12, panel13, panel14, panel15;
JButton clear, calculate;
Font font, font2, font3, font4;
double getBMI;
double roundedWTHR;
JLabel one, two;
public BMI() {
JFrame f = new JFrame("BMI and Body Fat Calculator");
f.setSize(450, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Initialize the panels and set their layout
panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel5 = new JPanel();
panel5.setLayout(new FlowLayout());
panel6 = new JPanel();
panel6.setLayout(new FlowLayout());
panel7 = new JPanel();
panel7.setLayout(new FlowLayout());
panel8 = new JPanel();
panel8.setLayout(new FlowLayout());
panel9 = new JPanel();
panel9.setLayout(new FlowLayout());
panel10 = new JPanel();
panel10.setLayout(new FlowLayout());
panel11 = new JPanel();
panel11.setLayout(new FlowLayout());
panel12 = new JPanel();
panel12.setLayout(new FlowLayout());
panel13 = new JPanel();
panel13.setLayout(new FlowLayout());
panel14 = new JPanel();
panel14.setLayout(new FlowLayout());
panel15 = new JPanel();
panel15.setLayout(new FlowLayout());
//Height
height = new JLabel("Height: ");
feetText = new JTextField(10);
feet = new JLabel(" Feet");
inchesText = new JTextField(10);
inches = new JLabel("inches");
feetText.setActionCommand("height");
feetText.addActionListener(this);
//Weight
weightLabel = new JLabel("Weight: ");
weightText = new JTextField(10);
pounds = new JLabel(" pounds");
//Waist
waist = new JLabel("Waist: ");
waistText = new JTextField(10);
inches2 = new JLabel("inches");
//Neck
neck = new JLabel("Neck: ");
neckText = new JTextField(10);
inches3 = new JLabel("inches");
//Hips
JLabel hips = new JLabel("Hips (Female): ");
hipsText = new JTextField(10);
inches4 = new JLabel("inches");
//Gender
gender = new JLabel("Sex: ");
male = new JRadioButton();
maleLabel = new JLabel("Male");
female = new JRadioButton();
femaleLabel = new JLabel("Female");
//Level of activity
activityLevel = new JLabel("Level of Activity: ");
sedentary = new JRadioButton();
sedentaryLabel = new JLabel("Sedentary");
moderate = new JRadioButton();
moderateLabel = new JLabel("Moderate");
active = new JRadioButton();
activeLabel = new JLabel("Active");
//Clear and Calculate
clear = new JButton("Clear");
calculate = new JButton("Calculate");
calculate.addActionListener(this);
clear.setActionCommand("clear");
//Body Mass Index
bmi = new JLabel("Body Mass Index: ");
font = new Font("Arial Black", Font.BOLD,12);
bmi.setFont(font);
//Waist-To-Height Ratio
ratio = new JLabel("Waist-To-Height Ratio: ");
font2 = new Font("Arial Black", Font.BOLD,12);
ratio.setFont(font2);
//Percentage Body Fat
pbf = new JLabel("Percentage Body Fat: ");
font3 = new Font("Arial Black", Font.BOLD,12);
pbf.setFont(font3);
//Lean Body Mass
lbm = new JLabel("Lean Body Mass: ");
font4 = new Font("Arial Black", Font.BOLD,12);
lbm.setFont(font4);
//Information 1
info1 = new JLabel("");
//Information 2
info2 = new JLabel("");
//Information 3
info3 = new JLabel("");
//Information 4
info4 = new JLabel("");
//Adding components to panel1
panel1.add(height);
panel1.add(feetText);
panel1.add(feet);
panel1.add(inchesText);
panel1.add(inches);
//Adding components to panel2
panel2.add(weightLabel);
panel2.add(weightText);
panel2.add(pounds);
//Adding components to panel3
panel3.add(waist);
panel3.add(waistText);
panel3.add(inches2);
//Adding components to panel4
panel4.add(neck);
panel4.add(neckText);
panel4.add(inches3);
//Adding components to panel5
panel5.add(hips);
panel5.add(hipsText);
panel5.add(inches4);
//Adding components to panel6
panel6.add(gender);
panel6.add(male);
panel6.add(maleLabel);
panel6.add(female);
panel6.add(femaleLabel);
/*The following code prohibits selecting
both the JRadioButtons selected at the same time*/
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(male);
genderGroup.add(female);
//Adding components to panel7
panel7.add(activityLevel);
panel7.add(sedentary);
panel7.add(sedentaryLabel);
panel7.add(moderate);
panel7.add(moderateLabel);
panel7.add(active);
panel7.add(activeLabel);
/*The following code prohibits selecting
both the JRadioButtons selected at the same time*/
ButtonGroup activityGroup = new ButtonGroup();
activityGroup.add(sedentary);
activityGroup.add(moderate);
activityGroup.add(active);
//Adding components to panel8
panel8.add(clear);
panel8.add(calculate);
//Adding a component to panel9
panel9.add(bmi);
//Adding a component to panel10
panel10.add(ratio);
//Adding a component to panel11
panel11.add(pbf);
//Adding a component to panel12
panel12.add(lbm);
//Adding components to panel13
panel13.add(info1);
panel13.add(info2);
//Adding components to panel14
panel14.add(info3);
//Adding components to panel15
panel15.add(info4);
//Set the layout
BoxLayout boxLayout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
setLayout(boxLayout);
//Adding panels to the frame
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
add(panel6);
add(panel7);
add(panel8);
add(panel9);
add(panel10);
add(panel11);
add(panel12);
add(panel13);
add(panel14);
add(panel15);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
double feet = Double.parseDouble(feetText.getText());
double inches = Double.parseDouble(inchesText.getText());
double weight = Double.parseDouble(weightText.getText());
double waist = Double.parseDouble(waistText.getText());
double neck = Double.parseDouble(neckText.getText());
double hip = Double.parseDouble(hipsText.getText());
double heightInInches = (feet * 12) + inches;
double heightInCm = (feet * 30.48) + (inches * 2.54);
if(event.getActionCommand().equals("clear")) {
feetText.setText("");
}
//Calculating the Percentage Body Fat
if(male.isSelected()) {
double percentageBodyFat = 495/1.0324 - 0.19077 * (Math.log10(waist*2.54 - neck*2.54)) + 0.15456 * (Math.log10(feet*12*2.54 + inches*2.54)) - 450;
double pbfRounded = Math.round(percentageBodyFat * 10.0)/10.0;
pbf.setText("Percent Body Fat: " + pbfRounded);
}
if(female.isSelected()) {
double percentageBodyFat = (495/(1.29579-0.35004*Math.log10(waist*2.54+hip*2.54-neck*2.54) + 0.22100 * Math.log10(feet*12*2.54 + inches*2.54))-450);
double pbfRounded = Math.round(percentageBodyFat * 10.0)/10.0;
double bodyFatPercent = pbfRounded;
pbf.setText("Percent Body Fat: " + pbfRounded + "%");
}
//Calculating the Body Mass Index
double bodyMassIndex = Math.round(703 * (weight/(heightInInches*heightInInches)*10.0))/10.0;
double getBMI = bodyMassIndex;
bmi.setText("Body Mass Index: " + bodyMassIndex + " kg/m²");
//Calculating the Waist-To-Height Ratio
double wthr = waist/heightInInches;
double roundedWTHR = Math.round(wthr * 100.0)/100.0;
ratio.setText("Waist-To-Height Ratio: " + roundedWTHR);
//Calculating the Lean Body Mass
if(male.isSelected()) {
double leanBodyMass = (0.32810 * weight) + (0.33929 * heightInCm) - 29.5336;
double lbmRounded = Math.round(leanBodyMass * 10.0)/10.0;
lbm.setText("Lean Body Mass: " + lbmRounded + "lb");
}
if(female.isSelected()) {
double leanBodyMass = (0.29569 * weight) + (0.41813 * heightInCm) - 43.2933;
double lbmRounded = Math.round(leanBodyMass * 10.0)/10.0;
lbm.setText("Lean Body Mass: " + lbmRounded + "lb");
}
if(getBMI < 15) {
info1.setText("Your Bmi is severly low");
info3.setText("You are severly underweight probably due to usual mass distributiuon");
} else if(getBMI <= 16) {
info1.setText("Your Bmi is extremely low");
info3.setText("You are extremely underweight probably due to usual mass distribution");
} else if(getBMI <= 18.5) {
info1.setText("Your BMI is lower than usual");
info3.setText("You may be underweight probably due to usual mass distribution");
} else if(getBMI <= 25) {
info1.setText("Your BMI is normal");
info3.setText("Your weight and mass distribution is normal");
} else if(getBMI <= 30) {
info1.setText("Your BMI is greater than normal");
info3.setText("You may be overweight or have unusual mass distribution");
} else if(getBMI <=35) {
info1.setText("Your BMI is very much greater than normal");
info3.setText("You are overweight or have unusual mass distribution");
} else if(getBMI <= 39.9) {
info1.setText("Your BMI is severly greater than normal");
info3.setText("You are extremely overweight or have unusual mass distribution");
} else if(getBMI >= 40) {
info1.setText("Your BMI is extremely greater than normal");
info3.setText("You are severly overweight or have unusual mass distribution");
}
if(male.isSelected() && roundedWTHR < 0.35) {
info2.setText("and your weight-to-height ratio is severly low");
}
else if(male.isSelected() && roundedWTHR <= 0.43) {
info2.setText("and your weight-to-height ratio is extremely low");
}
else if(male.isSelected() && roundedWTHR <= 0.46) {
info2.setText("and your weight-to-height ratio is lower than normal");
}
else if(male.isSelected() && roundedWTHR <= 0.53) {
info2.setText("but your weight-to-height ratio is normal");
}
else if(male.isSelected() && roundedWTHR <= 0.58) {
info2.setText("and your weight-to-height ratio is a little higher than usual");
}
else if(male.isSelected() && roundedWTHR <= 0.63) {
info2.setText("and your weight-to-height ratio is extremely higher than normal");
}
else if(male.isSelected() && roundedWTHR > 0.63) {
info2.setText("and your weight-to-height ratio is severly higher than normal");
}
if(female.isSelected() && roundedWTHR < 0.35) {
info2.setText("and your weight-to-height ratio is severly low");
}
else if(female.isSelected() && roundedWTHR <= 0.42) {
info2.setText("and your weight-to-height ratio is extremely low");
}
else if(female.isSelected() && roundedWTHR <= 0.46) {
info2.setText("and your weight-to-height ratio is lower than normal");
}
else if(female.isSelected() && roundedWTHR <= 0.49) {
info2.setText("but your weight-to-height ratio is normal");
}
else if(female.isSelected() && roundedWTHR <= 0.54) {
info2.setText("and your weight-to-height ratio is a little higher than usual");
}
else if(female.isSelected() && roundedWTHR <= 0.58) {
info2.setText("and your weight-to-height ratio is extremely higher than normal");
}
else if(female.isSelected() && roundedWTHR > 0.58) {
info2.setText("and your weight-to-height ratio is severly higher than normal");
}
if(sedentary.isSelected()) {
info4.setText("Your diet should contain at least " + Math.round(0.36*weight) + " grams of protein per day.");
}
if(moderate.isSelected()) {
info4.setText("Your diet should contain at least " + Math.round(0.4*weight) + " grams of protein per day.");
}
if(active.isSelected()) {
info4.setText("Your diet should contain at least " + Math.round(0.5*weight) + " grams of protein per day.");
}
}
}
'feetText.setText (" ");' ** sollte ** funktionieren. Wenn dies nicht der Fall ist, müssen Sie etwas debuggen. Bitte werfen Sie einen Blick auf: [Wie kleine Programme zu debuggen] (http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Es wird Ihr direktes Problem nicht lösen, aber es wird Ihnen Schritte geben, denen Sie folgen können, die Ihnen helfen sollten, es selbst zu lösen, oder selbst wenn das nicht erfolgreich ist, dann helfen Sie wenigstens, Ihr Problem besser zu isolieren, damit Ihre Frage es kann fokussierter und einfacher zu beantworten. –