附上官方例子:http://docs.oracle.com/javase/8/javafx/sample-apps/
/* * 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 login; 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.Button; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; 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; import javax.swing.JOptionPane; /** * * @author Administrator */ public class Login extends Application { @Override public void start(Stage primaryStage) throws Exception { String title = "javafx welcome to login! h=" + String.valueOf((int) primaryStage.getHeight()) + ",w=" + String.valueOf((int) primaryStage.getWidth()); System.out.println(title); System.out.println(primaryStage.getWidth()); if (Double.isNaN(primaryStage.getHeight())) { System.out.println("is nan"); } else { System.out.println("not is nan"); } //primaryStage.setWidth(500); // primaryStage.setHeight(500); primaryStage.setTitle(title); GridPane gridPane = new GridPane(); gridPane.setAlignment(Pos.CENTER); gridPane.setHgap(10); gridPane.setVgap(10); gridPane.setPadding(new Insets(25, 25, 25, 25)); Scene scene = new Scene(gridPane, 300, 300); primaryStage.setScene(scene); Text sceneTitle = new Text("Welcome"); sceneTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20)); gridPane.add(sceneTitle, 0, 0, 2, 1); Label userName = new Label("User Name:"); gridPane.add(userName, 0, 1); final TextField userTextField = new TextField(); gridPane.add(userTextField, 1, 1); Label pw = new Label("Password:"); gridPane.add(pw, 0, 2); final PasswordField pwdBox = new PasswordField(); gridPane.add(pwdBox, 1, 2); Button btn = new Button("Sign in"); HBox hbBtn = new HBox(10); hbBtn.setAlignment(Pos.BOTTOM_RIGHT); hbBtn.getChildren().add(btn); gridPane.add(hbBtn, 1, 4); final Text actiontarget = new Text(); gridPane.add(actiontarget, 1, 6); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { actiontarget.setFill(Color.FIREBRICK); actiontarget.setText("Sign in button pressed"); String userString=userTextField.getText(); String passwordString=pwdBox.getText(); if(userString!=null && userString.equals("admin")&&passwordString!=null&&passwordString.equals("123456")) { JOptionPane.showMessageDialog(null, "登陆成功!"); }else{ JOptionPane.showMessageDialog(null, "登陆失败,账号密码不正确!"); } } }); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }