按钮事件的未处理异常

时间:2014-08-08 01:32:10

标签: javafx

我正在写一份工资单程序,我遇到了两个问题。第一个,一个次要的,是我无法得到" stage2"当"提交"关闭单击按钮。第二个问题是我的文件输出。我得到Unhandled异常类型IOException。我似乎无法弄清楚如何纠正这个问题。任何帮助将不胜感激。

    public class companyParoll extends Application 
{
    private TextField tfNumOfEmployees = new TextField();
    private TextField tfEmpFirstName = new TextField();
    private TextField tfEmpLastName = new TextField();
    private TextField tfEmpPayRate = new TextField();
    private TextField tfEmpHoursWorked = new TextField();
    private Button btEnterNum = new Button("Submit");
    private Button btNextEmp = new Button("Add Employee");
    private Button btRunReport = new Button("Run Report");
    private Button btQuit = new Button("Quit");

    //Declare Variables
    int totalEmployees;
    int index = 0;
    String[] firstName = new String[totalEmployees];
    String[] lastName = new String[totalEmployees];
    double[] payRate = new double[totalEmployees];
    double[] hoursWorked = new double[totalEmployees];

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage)
    {
        //Create the UI
        GridPane gridPane =new GridPane();
        GridPane gridPane2 = new GridPane();
        gridPane.setHgap(75);
        gridPane.setVgap(15);
        gridPane.add(new Label("Employee's First Name:"), 0, 0);
        gridPane.add(tfEmpFirstName, 1, 0);
        gridPane.add(new Label("Employee's Last Name:"), 0, 1);
        gridPane.add(tfEmpLastName, 1, 1);
        gridPane.add(new Label("Employee's Hourly Pay Rate:"), 0, 2);
        gridPane.add(tfEmpPayRate, 1, 2);
        gridPane.add(new Label("Hours Worked by Employee"), 0, 3);
        gridPane.add(tfEmpHoursWorked, 1, 3);
        gridPane.add(btNextEmp, 1, 4);
        gridPane.add(btQuit, 0, 6);
        gridPane.add(btRunReport, 1, 6);

        //Set properties
        gridPane.setAlignment(Pos.CENTER);
        tfEmpFirstName.setAlignment(Pos.BOTTOM_RIGHT);
        tfEmpLastName.setAlignment(Pos.BOTTOM_RIGHT);
        tfEmpPayRate.setAlignment(Pos.BOTTOM_RIGHT);
        tfEmpHoursWorked.setAlignment(Pos.BOTTOM_RIGHT);
        GridPane.setHalignment(btQuit, HPos.LEFT);
        GridPane.setHalignment(btRunReport, HPos.RIGHT);
        GridPane.setHalignment(btNextEmp, HPos.RIGHT);

        gridPane2.setHgap(75);
        gridPane2.setVgap(15);
        gridPane2.add(new Label("Enter the Number of Employees:"), 0, 0);
        gridPane2.add(tfNumOfEmployees,0 ,1);
        gridPane2.add(btEnterNum, 0, 2);

        gridPane2.setAlignment(Pos.CENTER);
        tfNumOfEmployees.setAlignment(Pos.BOTTOM_RIGHT);
        GridPane.setHalignment(btEnterNum, HPos.CENTER);    

        btEnterNum.setOnAction(e -> getArraySize());
        btRunReport.setOnAction(e -> outputReport());
        btNextEmp.setOnAction(e -> addEmployeeData());
        btQuit.setOnAction(e -> quitApplication());

        // Create a scene and place it in the stage
        Scene scene= new Scene(gridPane, 400, 250) ;
        primaryStage.setTitle("Payroll Calculator"); // Set title
        primaryStage.setScene(scene); // Place the scene in t he stage
        primaryStage.show(); // Display the stage   

        //Create new window to get number of employees
        Stage stage2 = new Stage();
        Scene scene2 = new Scene(gridPane2, 200, 150);
        stage2.setTitle("Number of Employees");
        stage2.setScene(scene2);
        stage2.show();
    }

    public void getArraySize()
    {
        totalEmployees = Integer.parseInt(tfNumOfEmployees.getText());
//      stage2.close();

    }

    public void addEmployeeData()
    {

        while (index < firstName.length)
        {
            firstName[index] = tfEmpFirstName.getText();
            lastName[index] = tfEmpLastName.getText();
            payRate[index] = Double.parseDouble(tfEmpPayRate.getText());
            hoursWorked[index] = Integer.parseInt(tfEmpHoursWorked.getText());

            index ++;
            break;
        }

        tfEmpFirstName.clear();
        tfEmpLastName.clear();
        tfEmpPayRate.clear();
        tfEmpHoursWorked.clear();
    }

    public void outputReport() throws IOException 
    {
        PrintWriter empPayroll = new PrintWriter("Payroll.txt");

        double regularHours = 0;
        double overtimeHours = 0;
        double regularPay = 0;
        double overtimePay = 0;
        double totalPay = 0;

        for (index = 0; index < firstName.length; index ++)
        {
            if(hoursWorked[index] >= 40)
                regularHours = 40;
            else
                regularHours = hoursWorked[index];

            if(hoursWorked[index] > 40)
                overtimeHours = hoursWorked[index] - 40;
            else
                overtimeHours = 0;

            regularPay = (payRate[index] * regularHours);

            overtimePay = ((payRate[index] * 1.5) * overtimeHours);

            totalPay = regularPay + overtimePay;            

            empPayroll.println("\nName: " + firstName[index] + " " + lastName[index]);
            empPayroll.println("Pay Rate: " + payRate[index]);
            empPayroll.println("Regular Hours Worked: " + regularHours);
            empPayroll.println("Overtime Hours Worked: " + overtimeHours);
            empPayroll.println("Regular Pay: " + regularPay);
            empPayroll.println("Overtime Pay: " + overtimePay);
            empPayroll.println("Total Gross Pay: " + totalPay);
        }
        empPayroll.close();

    }

    public void quitApplication()
    {
        Platform.exit(); //Close application
    }

    public static void main(String[] args)
    {
        Application.launch();
    }
}

2 个答案:

答案 0 :(得分:0)

问题1 - 您已将stage2作为start方法的范围,您需要将stage2移动到类的主体,以便getArraySize()方法可以看到它。

问题2 - 不需要抛出IOException,而是需要在outputReport中使用try / catch。

e.g。

try
{
    // contents here that deal with the file handling
}
catch (IOException ex)
{
    ex.printStackTrace();
}

它的形式应与此类似:

import java.io.PrintWriter;
import java.io.FileNotFoundException;

public class PWriter
{
    public static void main(String[] args)
    {
        PrintWriter empPayroll = null;

        try
        {
            empPayroll = new PrintWriter("d:\\tests\\test.txt");
            empPayroll.println("Test");
        }
        catch (FileNotFoundException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            empPayroll.close();
        }
    }
}

答案 1 :(得分:0)

问题正是nejinx在他的回答中提到的,只是为了让你更深入地了解你出错的地方,我正在添加一个包含整个程序运行的答案!

  1. getArraySize()方法必须能够访问stage2,因此您可以在类级别声明Stage引用(正如我在下面的示例中所做的那样)或将引用作为参数传递给方法

  2. throws IOException移除outputReport()并在其中添加try / catch块!

  3. 工作示例

    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.geometry.HPos;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.GridPane;
    import javafx.stage.Stage;
    
    public class CompanyParoll extends Application {
        private TextField tfNumOfEmployees = new TextField();
        private TextField tfEmpFirstName = new TextField();
        private TextField tfEmpLastName = new TextField();
        private TextField tfEmpPayRate = new TextField();
        private TextField tfEmpHoursWorked = new TextField();
        private Button btEnterNum = new Button("Submit");
        private Button btNextEmp = new Button("Add Employee");
        private Button btRunReport = new Button("Run Report");
        private Button btQuit = new Button("Quit");
    
        // Declare Variables
        int totalEmployees;
        int index = 0;
        String[] firstName = new String[totalEmployees];
        String[] lastName = new String[totalEmployees];
        double[] payRate = new double[totalEmployees];
        double[] hoursWorked = new double[totalEmployees];
        Stage stage2;
    
        @Override
        // Override the start method in the Application class
        public void start(Stage primaryStage) {
            // Create the UI
            GridPane gridPane = new GridPane();
            GridPane gridPane2 = new GridPane();
            gridPane.setHgap(75);
            gridPane.setVgap(15);
            gridPane.add(new Label("Employee's First Name:"), 0, 0);
            gridPane.add(tfEmpFirstName, 1, 0);
            gridPane.add(new Label("Employee's Last Name:"), 0, 1);
            gridPane.add(tfEmpLastName, 1, 1);
            gridPane.add(new Label("Employee's Hourly Pay Rate:"), 0, 2);
            gridPane.add(tfEmpPayRate, 1, 2);
            gridPane.add(new Label("Hours Worked by Employee"), 0, 3);
            gridPane.add(tfEmpHoursWorked, 1, 3);
            gridPane.add(btNextEmp, 1, 4);
            gridPane.add(btQuit, 0, 6);
            gridPane.add(btRunReport, 1, 6);
    
            // Set properties
            gridPane.setAlignment(Pos.CENTER);
            tfEmpFirstName.setAlignment(Pos.BOTTOM_RIGHT);
            tfEmpLastName.setAlignment(Pos.BOTTOM_RIGHT);
            tfEmpPayRate.setAlignment(Pos.BOTTOM_RIGHT);
            tfEmpHoursWorked.setAlignment(Pos.BOTTOM_RIGHT);
            GridPane.setHalignment(btQuit, HPos.LEFT);
            GridPane.setHalignment(btRunReport, HPos.RIGHT);
            GridPane.setHalignment(btNextEmp, HPos.RIGHT);
    
            gridPane2.setHgap(75);
            gridPane2.setVgap(15);
            gridPane2.add(new Label("Enter the Number of Employees:"), 0, 0);
            gridPane2.add(tfNumOfEmployees, 0, 1);
            gridPane2.add(btEnterNum, 0, 2);
    
            gridPane2.setAlignment(Pos.CENTER);
            tfNumOfEmployees.setAlignment(Pos.BOTTOM_RIGHT);
            GridPane.setHalignment(btEnterNum, HPos.CENTER);
    
            btEnterNum.setOnAction(e -> getArraySize());
            btRunReport.setOnAction(e -> outputReport());
            btNextEmp.setOnAction(e -> addEmployeeData());
            btQuit.setOnAction(e -> quitApplication());
    
            // Create a scene and place it in the stage
            Scene scene = new Scene(gridPane, 400, 250);
            primaryStage.setTitle("Payroll Calculator"); // Set title
            primaryStage.setScene(scene); // Place the scene in t he stage
            primaryStage.show(); // Display the stage
    
            // Create new window to get number of employees
            stage2 = new Stage();
            Scene scene2 = new Scene(gridPane2, 200, 150);
            stage2.setTitle("Number of Employees");
            stage2.setScene(scene2);
            stage2.show();
        }
    
        public void getArraySize() {
            totalEmployees = Integer.parseInt(tfNumOfEmployees.getText());
            stage2.close();
        }
    
        public void addEmployeeData() {
    
            while (index < firstName.length) {
                firstName[index] = tfEmpFirstName.getText();
                lastName[index] = tfEmpLastName.getText();
                payRate[index] = Double.parseDouble(tfEmpPayRate.getText());
                hoursWorked[index] = Integer.parseInt(tfEmpHoursWorked.getText());
    
                index++;
                break;
            }
    
            tfEmpFirstName.clear();
            tfEmpLastName.clear();
            tfEmpPayRate.clear();
            tfEmpHoursWorked.clear();
        }
    
        public void outputReport() {
            try {
                PrintWriter empPayroll = new PrintWriter("Payroll.txt");
    
                double regularHours = 0;
                double overtimeHours = 0;
                double regularPay = 0;
                double overtimePay = 0;
                double totalPay = 0;
    
                for (index = 0; index < firstName.length; index++) {
                    if (hoursWorked[index] >= 40)
                        regularHours = 40;
                    else
                        regularHours = hoursWorked[index];
    
                    if (hoursWorked[index] > 40)
                        overtimeHours = hoursWorked[index] - 40;
                    else
                        overtimeHours = 0;
    
                    regularPay = (payRate[index] * regularHours);
    
                    overtimePay = ((payRate[index] * 1.5) * overtimeHours);
    
                    totalPay = regularPay + overtimePay;
    
                    empPayroll.println("\nName: " + firstName[index] + " "
                            + lastName[index]);
                    empPayroll.println("Pay Rate: " + payRate[index]);
                    empPayroll.println("Regular Hours Worked: " + regularHours);
                    empPayroll.println("Overtime Hours Worked: " + overtimeHours);
                    empPayroll.println("Regular Pay: " + regularPay);
                    empPayroll.println("Overtime Pay: " + overtimePay);
                    empPayroll.println("Total Gross Pay: " + totalPay);
                }
                empPayroll.close();
            } catch (IOException exp) {
                exp.printStackTrace();
            }
    
        }
    
        public void quitApplication() {
            Platform.exit(); // Close application
        }
    
        public static void main(String[] args) {
            Application.launch();
        }
    }