2
我正在爲學校的工資計劃工作。我只用了兩週的時間來使用GUI,所以我的技能有很大的發展空間。除了這種方法,一切正常。我試圖得到清除JTextAreas的方法,如果輸入的數字超出範圍並跳過剩餘的代碼發送數據到JTextArea。到目前爲止,如果超出範圍,它會打印出名稱dept和0.00,但我不希望它顯示任何內容,除非它在範圍內。調用方法重新啓動,如果條件滿足,而不使用循環
public void buildAgain() {
// declare variables to hold the JTextField input as string
String rateStr, hoursStr, firstName, lastName, dept;
firstName = (String) fnameField.getText();
lastName = (String) lnameField.getText();
rateStr = rateField.getText();
// convert the rate field into double in order to calculate it
rate = Double.parseDouble(rateStr);
hoursStr = hoursField.getText();
// convert the hours into double in order to calculate it
hoursWorked = Double.parseDouble(hoursStr);
// Check how many hours worked to make sure they are within range
if(hoursWorked >= 0 && hoursWorked <=60)
hours = hoursWorked;
// Display message if hours are not within range
else
JOptionPane.showMessageDialog(null, "You have entered an invalid number of hours. \n"
+ " Please enter a number between 0 and 60.", "Hour Entry Error",
JOptionPane.ERROR_MESSAGE);
// Clears JTextFields after the error message
fnameField.setText("");
lnameField.setText("");
hoursField.setText("");
rateField.setText("");
// check the hourly rate to make sure it is within range
if (rate >= 6 && rate <=150)
payRate = rate;
// display an error message if rate entered not within range
else
JOptionPane.showMessageDialog(null, "You have entered an invalid pay rate. \n "
+ "Please enter the rate between 6 and 150.", "Pay Rate Error",
JOptionPane.ERROR_MESSAGE);
// clear the JTextFields after error message
fnameField.setText("");
lnameField.setText("");
hoursField.setText("");
rateField.setText("");
// calculate the pay regular hours
if (hours >= 0 && hours <= 40){
weeklyPay = hours*payRate;
// Calculate overtime pay
} else if (hours > 40 && hours <= 60){
overTime = (hours-40) * (payRate*1.5);
weeklyPay = overTime + (40*payRate);
}
// Display the total pay in uneditable table
totalPayField.setText("$"+dollar.format(weeklyPay));
// Send name to JTextArea
list.append(firstName + " ");
list.append(lastName);
// Get the selected department
if(hr.isSelected())
dept = hr.getText();
else if(accounting.isSelected())
dept = accounting.getText();//"Accounting";
else if(marketing.isSelected())
dept = marketing.getText();
else if(sales.isSelected())
dept = sales.getText();
else
dept = shipping.getText();
// Send selected department and pay to JTextArea
list.append("\t" + dept);
list.append("\t$" + dollar.format(weeklyPay) + "\n");
}
謝謝你這麼多兄弟。很簡單,但離我很遠,因爲我不知道。我希望我能投票。再次感謝。 – sammyb123