退出按钮无法正常工作

时间:2018-10-13 01:13:36

标签: java javafx

我已经完成了程序的计算部分,但是我不知道如何获得清除字段中的清除按钮和退出按钮以关闭程序的方法。

我为两者尝试了不同的方法,但是我无法使它们正常工作。

我能够复制代码以为其他两个按钮进行计算,只是没有使它们按预期工作。

public class JavaFXPaycheckJava extends Application {
// define the constants
    public static final double OVERTIME_RATE = 1.5;   // time and a half
    public static final double TAX_RATE = 0.17;       // 0.17 is 17%

    // list of controls on the scene
    private static Label lblTitle;
    private static Label lblHours;
    private static Label lblPayRate;
    private static Button btnCompute;
    private static TextField txtHours;
    private static TextField txtPayRate;
    private static TextArea txtPaycheck; 
    private static Button btnClear;
    private static Button btnExit;

    @Override
    public void start(Stage stage) {
        Pane root = new Pane();
        createControls (root);

        Scene scene = new Scene(root, 450, 320);
        stage.setTitle("JavaFX Paycheck Project");
        stage.setResizable(false);
        stage.setScene(scene);
        stage.show();
    }

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


     // process the Compute button click event

    private static void btnComputeClick() {
        double hours;
        double payRate;
        double regHours, overtimeHours;
        double regPay, overtimePay;
        double grossPay, taxes, netPay;

        // input the hours and payRate from the TextFields
        try {
            hours = Double.valueOf(txtHours.getText());
            payRate = Double.valueOf(txtPayRate.getText());
            if (hours < 0 || payRate < 0)
                throw new IllegalArgumentException ("Inputs must be positive");
        }
        catch (NumberFormatException e) {
            txtPaycheck.setText("Values for hours and pay rate must be numeric"); 
            return; // no more processing
        }  
        catch (IllegalArgumentException e) {
            txtPaycheck.setText(e.getMessage()); 
            return; // no more processing
        }

        // compute the regHours and overtimeHours
        if (hours <= 40) {              // Less or = to 40 hours, then
            regHours = hours;           //   all hours are regular
            overtimeHours = 0.0;        //   with no overtime
        }
        else {                          // Over 40 hours, then
            regHours = 40.0;            //    first 40 at regular pay
            overtimeHours = hours-40.0; //    anything over 40
        }

        // compute the paycheck
        regPay = regHours * payRate;
        overtimePay = overtimeHours * payRate * OVERTIME_RATE;
        grossPay = regPay + overtimePay;
        taxes = grossPay * TAX_RATE;
        netPay = grossPay - taxes;

        // display the paycheck, formatted with 2 digits past the decimal
        txtPaycheck.setText( 
                String.format("Your gross pay is $%.2f\n", grossPay) +
                String.format("Your taxes are    $%.2f\n", taxes) +
                String.format("Your net pay is   $%.2f", netPay) );

    } // end of btnCompute_Click
    private static void btnClearClick(){





    }// end of ClearClick
    private static void btnExitClick(){


    }//end of ExitClick

    private static void createControls(Pane root) {
        Font font24B = Font.font("Ariel", FontWeight.BOLD, 24); // title
        Font font18  = Font.font("Ariel", FontWeight.NORMAL, 18); // 

        lblTitle = new Label("Paycheck Calculator");
            lblTitle.setFont(font24B);          // large-bold font
            lblTitle.setLayoutX(12);
            lblTitle.setLayoutY(4);
            root.getChildren().add(lblTitle);

        lblHours = new Label("Hours");
            lblHours.setFont(font18);           // normal font, size=18
            lblHours.setLayoutX(24);
            lblHours.setLayoutY(57);
            root.getChildren().add(lblHours);

        txtHours = new TextField();
            txtHours.setFont(font18);
            txtHours.setLayoutX(123);
            txtHours.setLayoutY(57);
            txtHours.setMaxSize(119,30);        
            txtHours.setMinSize(119,30);
            root.getChildren().add(txtHours);

        lblPayRate = new Label("Pay Rate");
            lblPayRate.setFont(font18);
            lblPayRate.setLayoutX(24);
            lblPayRate.setLayoutY(101);
            root.getChildren().add(lblPayRate);

        txtPayRate = new TextField();
            txtPayRate.setFont(font18);
            txtPayRate.setLayoutX(123);
            txtPayRate.setLayoutY(95);
            txtPayRate.setMaxSize(119,30);
            txtPayRate.setMinSize(119,30);
            root.getChildren().add(txtPayRate);

        btnCompute = new Button("Compute");
            btnCompute.setFont(font18);
            btnCompute.setLayoutX(40);
            btnCompute.setLayoutY(148);
            btnCompute.setMaxSize(110,40);      
            btnCompute.setMinSize(110,40);
            btnCompute.setOnAction( e -> btnComputeClick() );
            btnCompute.setOnKeyPressed (e -> btnComputeClick() );
            root.getChildren().add(btnCompute);

        btnClear = new Button("Clear");
            btnClear.setFont(font18);
            btnClear.setLayoutX(180);
            btnClear.setLayoutY(148);
            btnClear.setMaxSize(110,40);     
            btnClear.setMinSize(110,40);
            btnClear.setOnAction( e -> btnClearClick() );
            btnClear.setOnKeyPressed (e -> btnClearClick() );
            root.getChildren().add(btnClear);

        btnExit = new Button("Exit");
            btnExit.setFont(font18);
            btnExit.setLayoutX(320);
            btnExit.setLayoutY(148);
            btnExit.setMaxSize(110,40);      
            btnExit.setMinSize(110,40);
            btnExit.setOnAction( e -> btnExitClick() );
            btnExit.setOnKeyPressed (e -> btnExitClick() );
            root.getChildren().add(btnExit);    

        txtPaycheck = new TextArea();          // TextArea to display the paycheck
            txtPaycheck.setFont(font18);
            txtPaycheck.setLayoutX(40);
            txtPaycheck.setLayoutY(205);
            txtPaycheck.setMinSize(400,100);
            txtPaycheck.setMaxSize(400,100);
            txtPaycheck.setEditable(false); // don't let user change paycheck
            enter code hereroot.getChildren().add(txtPaycheck);       
    } // end of createControls()

} // end of class JavaFXPaycheck

我如何完成此任务?

2 个答案:

答案 0 :(得分:0)

btnExitClick()方法不执行任何操作。更改为:

 private static void btnExitClick(){
    Platform.exit();
    System.exit(0);
}

答案 1 :(得分:-1)

清除。您需要对按钮进行操作

btnClear.setOnAction(e->{
lblTitle.setText("");
lblHours.setText("");
...
});

结束

btnExit.setOnAction(e->{
stage.close();
});