如何去掉JavaFX primaryStage窗口的边框?

29

我正在制作JavaFX桌面应用程序。我想要移除默认的窗口边框,并自定义最小化、最大化和关闭三个标准图标。

这种外观或定制的原始动机是新版卡巴斯基2012用户界面...我想设计出类似的东西... :)


7
当一个本来就是原始问题的问题被标记为重复问题时,这种做法是多么不合逻辑。 - Haggra
4
如果你要设计自己的窗口,请非常保守,并尽量坚持每个本地平台的设计。很容易把它搞砸,让它看起来像廉价的噱头。 - RecursiveExceptionException
1个回答

48

这个例子可能是一个很好的起点。所有窗口装饰都被删除了。可以使用扩展HBox的类来放置标准窗口操作的自定义按钮。

package javafxdemo;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class JavaDemo extends Application {

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

    class WindowButtons extends HBox {

        public WindowButtons() {
            Button closeBtn = new Button("X");

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

                @Override
                public void handle(ActionEvent actionEvent) {
                    Platform.exit();
                }
            });

            this.getChildren().add(closeBtn);
        }
    }

    @Override
    public void start(Stage primaryStage) {
        //remove window decoration
        primaryStage.initStyle(StageStyle.UNDECORATED);

        BorderPane borderPane = new BorderPane();
        borderPane.setStyle("-fx-background-color: green;");

        ToolBar toolBar = new ToolBar();

        int height = 25;
        toolBar.setPrefHeight(height);
        toolBar.setMinHeight(height);
        toolBar.setMaxHeight(height);
        toolBar.getItems().add(new WindowButtons());

        borderPane.setTop(toolBar);

        primaryStage.setScene(new Scene(borderPane, 300, 250));
        primaryStage.show();
    }
}
您还可以下载JavaFX示例,在其中您可以找到许多更有用的示例。

感谢您提供的示例。 - Dhruv
4
很棒的回答!+1!但是如果有人想要仍然能够将窗口在屏幕上移动怎么办? - 735Tesla
4
没关系,我在这里找到了答案:https://dev59.com/6Wgt5IYBdhLWcg3w2A37 - 735Tesla
1
注意:不支持JavaFX8和Eclipse 4.5。 - simpleuser
1
链接已失效,请更新。 - Vampire

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接