JavaFX实现输入框输入初始化计算弹出框

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chcndgradecalculator;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author houchule
*/
public class ChcndGradeCalculator extends Application {

public String title = "Grade Calculator"; 

public int width = 500; 
public int height = 400; 
public String fontStyle = "Comic Sans MS"; 

String num1,num2;
private  double n1,n2, FinalValue;

@Override
public void start(Stage primaryStage) {

    primaryStage.setTitle(title); 

    GridPane root = new GridPane();
    root.setAlignment(Pos.CENTER);
    root.setHgap(15);
    root.setVgap(15);

// root.setPadding(new Insets(25, 25, 25, 25));

    Label label1 = new Label("Category 1 (30%):");
    //label1.setFont(Font.font(fontStyle, FontWeight.NORMAL,18));
    root.add(label1, 0, 0);

    TextField Score1 = new TextField(); 
    root.add(Score1, 0, 1);
    Score1.setPrefWidth(400);


    Label label2 = new Label("Category 2 (70%):");
    //label2.setFont(Font.font(fontStyle, FontWeight.NORMAL,18));
    root.add(label2, 0, 2);

    TextField Score2 = new TextField(); 
    root.add(Score2, 0, 3);
    Score2.setPrefWidth(400);


    Label label3 = new Label("My Final Score:");
    //label3.setFont(Font.font(fontStyle, FontWeight.NORMAL,18));
    root.add(label3, 0, 4);

    TextField Score3 = new TextField(); 
    root.add(Score3, 0, 5);
    Score3.setPrefWidth(400);


    Button Button1 = new Button("Maximize");       
    Button Button2 = new Button("Calculate");        
    Button Button3 = new Button("Clear");
    Button Button4 = new Button("Alert");

    //Maximize
    Button1.setOnAction((ActionEvent e) -> {
        Score1.setText("100");
        Score2.setText("100");
    });
    //Calculate
    Button2.setOnAction((ActionEvent e) -> {
          num1 = Score1.getText();
          num2 = Score2.getText();
          n1 = Double.parseDouble(num1);
          n2 = Double.parseDouble(num2);
          FinalValue = (n1*0.3)+(n2*0.7);
          Score3.setText("My final score should be "+num1+" * 0.3"+" + "+num2+" * 0.7 = "+String.format("%.2f", FinalValue));

    });
    //Clear
    Button3.setOnAction((ActionEvent e) -> {
        Score1.setText("");
        Score2.setText("");
        Score3.setText("");
    });
    //Alert
    Button4.setOnAction((ActionEvent e) -> {

// Stage window = new Stage();
// window.setTitle(“My Final Score”);
// window.setMinWidth(300);
// window.setMinHeight(200);
//
// Label label = new Label(Score3.getText());
// label.setFont(Font.font(fontStyle, FontWeight.NORMAL,35));
//
// VBox layout = new VBox(10);
// layout.getChildren().addAll(label);
// layout.setAlignment(Pos.CENTER);
//
// Scene scene = new Scene(layout);
// window.setScene(scene);
// window.showAndWait();

          Alert information = new Alert(Alert.AlertType.INFORMATION,"My final score should be "+num1+" * 0.3"+" + "+num2+" * 0.7 = "+String.format("%.2f", FinalValue));
          information.setTitle("Alert"); //设置标题,不设置默认标题为本地语言的information
          information.setHeaderText("Message"); //设置头标题,默认标题为本地语言的information
          information.showAndWait(); //显示弹窗,同时后续代码等挂起


    });


    VBox Button = new VBox();
    Button.setPadding(new Insets(0, 0, 15, 0));
    Button.setSpacing(15);
    root.add(Button, 0, 6);
    Button.getChildren().addAll(Button1, Button2, Button3, Button4);
    Button1.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
    Button2.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
    Button3.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
    Button4.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);


    Scene scene = new Scene(root, width,height);
    primaryStage.setScene(scene);
    primaryStage.show(); 
}


/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

你可能感兴趣的:(JAVA)