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

时间:2015-06-08 04:01:36

标签: java user-interface oop jtextfield jtextarea

我正在为学校制定薪资计划。我只用了两周时间使用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");

    }

1 个答案:

答案 0 :(得分:2)

你快到了。

在您检查工作时间在范围内的代码部分之前,移动清除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
}