一、登录

  1. import javafx.application.Application;
  2. import javafx.event.ActionEvent;
  3. import javafx.event.EventHandler;
  4. import javafx.geometry.Insets;
  5. import javafx.geometry.Pos;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.*;
  8. import javafx.scene.layout.Background;
  9. import javafx.scene.layout.GridPane;
  10. import javafx.scene.layout.HBox;
  11. import javafx.scene.paint.Color;
  12. import javafx.scene.text.Font;
  13. import javafx.scene.text.FontWeight;
  14. import javafx.scene.text.Text;
  15. import javafx.stage.Stage;
  16. public class JavaFx13GridPaneLogin extends Application {
  17. @Override
  18. public void start(Stage stage) throws Exception {
  19. stage.setTitle("登录");
  20. GridPane gridPane = new GridPane();
  21. // gridPane.setGridLinesVisible(true);
  22. gridPane.setAlignment(Pos.CENTER);
  23. //gridPane.setBackground(Background.fill(Color.RED));
  24. gridPane.setVgap(10);
  25. Text text = new Text("欢迎登录");
  26. text.setFont(Font.font("宋体", FontWeight.NORMAL,20));
  27. gridPane.add(text,0,0,2,1);
  28. Label userName = new Label("用户名:");
  29. gridPane.add(userName,0,1);
  30. TextField userNameField = new TextField();
  31. gridPane.add(userNameField,1,1);
  32. Label password = new Label("密码:");
  33. gridPane.add(password,0,2);
  34. PasswordField passwordField = new PasswordField();
  35. gridPane.add(passwordField,1,2);
  36. Button saveButton = new Button("保存");
  37. saveButton.setOnAction(new EventHandler<ActionEvent>() {
  38. @Override
  39. public void handle(ActionEvent event) {
  40. String un = userNameField.getText();
  41. String pwd = passwordField.getText();
  42. if("admin".equals(un) && "123".equals(pwd)){
  43. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  44. alert.setTitle("成功");
  45. alert.setHeaderText("登录成功!");
  46. alert.setContentText("登录失败!");
  47. alert.showAndWait();
  48. }else{
  49. Alert alert = new Alert(Alert.AlertType.WARNING);
  50. alert.setTitle("警告");
  51. alert.setHeaderText("用户账号密码错误!");
  52. alert.setContentText("请重新填写!");
  53. alert.showAndWait();
  54. }
  55. }
  56. });
  57. HBox hBox = new HBox();
  58. hBox.getChildren().add(saveButton);
  59. hBox.setAlignment(Pos.BOTTOM_CENTER);
  60. gridPane.add(hBox,1,3);
  61. Scene scene = new Scene(gridPane,600,500);
  62. stage.setScene(scene);
  63. stage.show();
  64. }
  65. public static void main(String[] args) {
  66. launch(args);
  67. }
  68. }