JavaFX-简单的登录界面

2021-03-02 14:55:50 浏览数 (14)

效果
代码
代码语言:javascript复制
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.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import static javafx.geometry.HPos.RIGHT;

public class Login1 extends Application {

	@Override
	public void start(Stage primaryStage) {
		primaryStage.setTitle("JavaFX Welcome");
		//允许开发者创建一个灵活的网格,按行列来布局其内容节点。
		GridPane grid = new GridPane();
		// 显示网格线
//		grid.setGridLinesVisible(true);
		//居中
		grid.setAlignment(Pos.CENTER);
		//行列之间的间隔
		grid.setHgap(10);
		grid.setVgap(10);
		//面板边缘周围的间隔
		grid.setPadding(new Insets(25, 25, 25, 25));

		Text scenetitle = new Text("Welcome");
//		scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
		grid.add(scenetitle, 0, 0, 2, 1);

		Label userName = new Label("User Name:");
		grid.add(userName, 0, 1);

		TextField name = new TextField();
		name.setPromptText("Enter your name.");
		grid.add(name, 1, 1);

		Label pw = new Label("Password:");
		grid.add(pw, 0, 2);

		TextField passwordField = new TextField();
		name.setPromptText("Enter your password.");
		grid.add(passwordField, 1, 2);

		Button btn = new Button("Sign in");
		//
		HBox hbBtn = new HBox(10);
		hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
		hbBtn.getChildren().add(btn);
		grid.add(hbBtn, 1, 4);

		final Text actiontarget = new Text();
		grid.add(actiontarget, 0, 6);
		GridPane.setColumnSpan(actiontarget, 2);
		GridPane.setHalignment(actiontarget, RIGHT);

		btn.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent e) {
//				actiontarget.setFill(Color.FIREBRICK);
				actiontarget.setText("Sign in button pressed");
			}
		});

		/*
		scene被创建并使用gripane作为root节点,
		这在使用layout容器构建界面时是一种常见用法
		布局 ,可以自动改变大小
		 */
		Scene scene = new Scene(grid, 300, 275);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

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

}

0 人点赞