繁体   English   中英

JavaFx 将窗口控制按钮添加到菜单栏(类似 IntelliJ)

[英]JavaFx add Window-Control-Buttons to Menubar (IntelliJ like)

从 IntelliJ 2019.2 版本开始,Jetbrains 从 IDE 中移除了标题栏,并将窗口的最小化、最大化和关闭按钮放入菜单栏中。 到目前为止,我还没有发现如何使用 javafx 这样做。 有没有办法实例化一个“WindowControlButtons”类,这样我就可以轻松地将它们添加到菜单栏,或者我是否必须添加一个按钮组并自己为每个平台设置按钮样式?

例如它在 Windows 上的样子:

示例图像

正如@mr mcwolf所建议的,您可以尝试以下解决方案。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class JavaFXApplication1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button closeButton = new Button();
        closeButton.setText("X");
        closeButton.setOnAction((ActionEvent event) -> {
            javafx.application.Platform.exit();
        });
        Button hideButton = new Button();
        hideButton.setText("-");
        hideButton.setOnAction((ActionEvent event) -> {
            primaryStage.setIconified(true);
        });
        Menu menu = new Menu("Menu");
        MenuItem menuItem1 = new MenuItem("item 1");
        MenuItem menuItem2 = new MenuItem("item 2");
        menu.getItems().add(menuItem1);
        menu.getItems().add(menuItem2);
        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().add(menu);
        HBox hBox = new HBox(menuBar, hideButton, closeButton);
        HBox.setHgrow(menuBar, Priority.ALWAYS);
        HBox.setHgrow(hideButton, Priority.NEVER);
        HBox.setHgrow(closeButton, Priority.NEVER);
        BorderPane root = new BorderPane();
        root.setTop(hBox);
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.show();
    }

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

}

Windows 上的输出如下所示。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM