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

本文介绍了JavaFX中如何设置窗口风格,包括DECORATED, UNDECORATED, TRANSPARENT, UTILITY和UNIFIED五种类型,并详细讲解了如何去除系统标题栏并实现自定义标题栏的布局代码示例。" 117160012,10326450,易语言递归搜索文件夹教程,"['易语言', '文件操作', '程序设计', '枚举', '文件搜索']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<ActionEvent>() {

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

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

			@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);

 

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值