在不使用循环的情况下,调用方法以在满足条件时重新启动



我正在做一个学校的工资管理程序。我只使用了两周的gui,所以我的技能还有很大的提升空间。除了这个方法,一切都很好。如果输入的数字超出范围,我试图让该方法清除JTextAreas,并跳过将数据发送到JTextArea的剩余代码。到目前为止,如果超出范围,它会打印出名称,深度和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");
    }

你就快成功了。

将清除JTextFields的代码段移到检查工作小时数是否在范围内的代码段之前。然后在显示错误消息之后返回。

//Clears JTextFields before checking the error message
fnameField.setText("");
...
//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, "error msg.n", JOptionPane.ERROR_MESSAGE);
    return

注意:如果你只想在出现错误后清除字段,那么把它们移到显示错误信息的else语句中。

,

//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, "error msg.n", JOptionPane.ERROR_MESSAGE);
    //Clears JTextFields before checking the error message
    fnameField.setText("");
    ...
    return
}

相关内容

  • 没有找到相关文章

最新更新