1
我似乎無法添加文本字段並將其結果顯示在顯示卡路里文本字段中。無法在其他文本字段中添加並顯示文本字段值
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
public class caloriesBurnerApplet extends Applet implements ActionListener {
/****
//
//Running 6 mph : 10 METS
//Basketball : 8 METS
//Sleeping: 1 METS
//formula for the number calories burned per minute
//calories/minutes = 0.0175 * MET * Weight in kilogram
//
// 1 kg = 2.2 lbs ==> 1 lb = (1/2.2) kg
****/
Label lbWeight, lbRun, lbBsktBall, lbSleep, lbRunningCalories, lbBsktBallCalorioes,
lbSleepingCalories ;
Button caloriesCalcBtn ;
TextField txtWeight, txtRun, txtBsktBall, txtSleep, txtDspyRunCalr,
txtDspyBsktBallCalr, txtDspySleepCalr ;
int weight, minRun, minBsktBall, hourSleep;
float caloriesRun, caloriesBsktBall, caloriesSleep, caloriesTotal;
String weightString, minRunString, minBsktBallString, hourSleepString;
private static final long serialVersionUID = 1L;
public caloriesBurnerApplet()
{
lbWeight = new Label("Please enter your weight", JLabel.LEFT);
txtWeight = new TextField(15);
lbRun = new Label("How long do you run for 6 mph (in minutes)", JLabel.LEFT);
txtRun = new TextField(15);
lbBsktBall = new Label("How long do you play BasketBall (in minutes)");
txtBsktBall = new TextField(15);
lbSleep = new Label("How many hours do you sleep");
txtSleep = new TextField(15);
caloriesCalcBtn = new Button("Calculate calories");
lbRunningCalories = new Label("By Running you burnt these calories: ");
txtDspyRunCalr = new TextField(15);
lbBsktBallCalorioes = new Label("By Playing BasketBall you burnt there calories: ");
txtDspyBsktBallCalr = new TextField(15);
lbSleepingCalories = new Label("By Sleeping you burnt these calories: ");
txtDspySleepCalr = new TextField(15);
}
public void init()
{
add(lbWeight);
add(txtWeight);
add(lbRun);
add(txtRun);
add(lbBsktBall);
add(txtBsktBall);
add(lbSleep);
add(txtSleep);
add(caloriesCalcBtn);
add(lbRunningCalories);
add(txtDspyRunCalr);
add(lbBsktBallCalorioes);
add(txtDspyBsktBallCalr);
add(lbSleepingCalories);
add(txtDspySleepCalr);
}
@Override
public void actionPerformed(ActionEvent e) {
//calories/minutes = 0.0175 * MET * Weight in kilogram
// , minRunString, minBsktBallString, hourSleepString;
//weight = Integer.parseInt(txtWeight.getText());
//minRun = Double.parseDouble(txtRun.getText());
//minBsktBall = Double.parseDouble(txtBsktBall.getText());
//hourSleep = Double.parseDouble(txtSleep.getText());
weight = Integer.parseInt(txtWeight.getText());
minRun = Integer.parseInt(txtRun.getText());
minBsktBall = Integer.parseInt(txtBsktBall.getText());
hourSleep = Integer.parseInt(txtSleep.getText());
caloriesRun = (float) (minRun * 0.0175 * 10 * ((weight) * (1/2)));
if (e.getSource() == caloriesCalcBtn)
{
// can't seems to display result in the caloriesRun textfield
txtDspyRunCalr.setText(String.valueOf(caloriesRun));
}
}
}
我想我忘了添加此部分: \t caloriesCalcBtn.addActionListener(本); – Abdul
爲什麼要編寫一個小程序?如果由於規格而到期。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 –
嗨安德魯湯普森,我只是閱讀電子書,並嘗試編寫項目在每章的末尾(在應用程序和小程序)只爲練習。它不是一個家庭作業,只是自我實踐,但有一件事我錯了是當我txtDspyRunCalr按鈕來獲得卡路里運行,我得到0.0的答案,無論輸入值是minRun和體重。你知道我做錯了什麼嗎?我完全失去了 – Abdul