一、登录
import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.geometry.Insets;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.Background;import javafx.scene.layout.GridPane;import javafx.scene.layout.HBox;import javafx.scene.paint.Color;import javafx.scene.text.Font;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class JavaFx13GridPaneLogin extends Application { @Override public void start(Stage stage) throws Exception { stage.setTitle("登录"); GridPane gridPane = new GridPane();// gridPane.setGridLinesVisible(true); gridPane.setAlignment(Pos.CENTER); //gridPane.setBackground(Background.fill(Color.RED)); gridPane.setVgap(10); Text text = new Text("欢迎登录"); text.setFont(Font.font("宋体", FontWeight.NORMAL,20)); gridPane.add(text,0,0,2,1); Label userName = new Label("用户名:"); gridPane.add(userName,0,1); TextField userNameField = new TextField(); gridPane.add(userNameField,1,1); Label password = new Label("密码:"); gridPane.add(password,0,2); PasswordField passwordField = new PasswordField(); gridPane.add(passwordField,1,2); Button saveButton = new Button("保存"); saveButton.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { String un = userNameField.getText(); String pwd = passwordField.getText(); if("admin".equals(un) && "123".equals(pwd)){ Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("成功"); alert.setHeaderText("登录成功!"); alert.setContentText("登录失败!"); alert.showAndWait(); }else{ Alert alert = new Alert(Alert.AlertType.WARNING); alert.setTitle("警告"); alert.setHeaderText("用户账号密码错误!"); alert.setContentText("请重新填写!"); alert.showAndWait(); } } }); HBox hBox = new HBox(); hBox.getChildren().add(saveButton); hBox.setAlignment(Pos.BOTTOM_CENTER); gridPane.add(hBox,1,3); Scene scene = new Scene(gridPane,600,500); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); }}