javafx窗口风格、包括自定义标题栏

1、窗口风格设置:(primaryStage.initStyle(StageStyle);)

 

StageStyle有几种类型

1) DECORATED——白色背景,带有最小化/最大化/关闭等有操作系统平台装饰( 默认设置)

2) UNDECORATED——白色背景,没有操作系统平台装饰

3) TRANSPARENT——透明背景,没有操作系统平台装饰

4) UTILITY——白色背景,只有关闭操作系统平台装饰

5) UNIFIED——有操作系统平台装饰,消除装饰和内容之间的边框,内容背景和边框背景一致

2、自定义标题栏:

 

首先去掉系统自带的标题栏。直接设置窗口风格,设置为没有操作系统平台装饰的,然后加上自定义布局,直接上代码

		GridPane gridPane = new GridPane();
		gridPane.setStyle("-fx-background-color: rgb(78.0,163.0,248.0);");
		gridPane.setPrefHeight(32);
		gridPane.setAlignment(Pos.CENTER_LEFT);
		Label label = new Label("title");
		label.setFont(Font.font(14));
		label.setTextFill(Paint.valueOf("white"));
		ImageView imageView = new ImageView("icon.png");
		imageView.setFitHeight(24);
		imageView.setFitWidth(24);
		label.setGraphic(imageView);
		Button minButton = new Button("—");
		Button amxButton = new Button("口");
		Button closeButton = new Button("X");
		minButton.setStyle("-fx-base: rgb(243,243,243); -fx-border-color: rgb(243,243,243); -fx-border-width: 0.1; "
				+ "-fx-max-height: infinity;-fx-text-fill: white ; -fx-border-image-insets: 0;");
		amxButton.setStyle("-fx-base: rgb(243,243,243); -fx-border-color: rgb(243,243,243); -fx-border-width: 0.1; "
				+ "-fx-max-height: infinity;-fx-text-fill: white ; -fx-border-image-insets: 0;");
		closeButton.setStyle("-fx-base: rgb(255,128,128); -fx-border-color: rgb(243,243,243); -fx-border-width: 0.1; "
				+ "-fx-max-height: infinity;-fx-text-fill: white ; -fx-border-image-insets: 0;");
		minButton.setOnAction(new EventHandler() {

			@Override
			public void handle(ActionEvent event) {
				primaryStage.setIconified(true);
			}
		});
		amxButton.setOnAction(new EventHandler() {

			@Override
			public void handle(ActionEvent event) {
				primaryStage.setMaximized(!primaryStage.isMaximized());
			}
		});
		closeButton.setOnAction(new EventHandler() {

			@Override
			public void handle(ActionEvent event) {
				primaryStage.close();
			}
		});
		gridPane.addColumn(0, label);
		GridPane.setHgrow(label, Priority.ALWAYS);
		gridPane.addColumn(1, minButton);
		gridPane.addColumn(2, amxButton);
		gridPane.addColumn(3, closeButton);
                VBox box = new VBox();
		box.getChildren().add(gridPane);
		Scene scene = new Scene(box);
		primaryStage.setScene(scene);

 

 

 

 

你可能感兴趣的:(javafx,java)