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

JavaFx将窗口控件按钮添加到菜单栏(类似IntelliJ),javafx,window,controls,styles,menubar,Javafx,Window,Controls,Styles,Menubar,由于IntelliJ版本2019.2 Jetbrains从IDE中删除了标题栏,并将窗口的最小化、最大化和关闭按钮放入菜单栏。到目前为止,我还没有发现如何使用javafx实现这一点。有没有一种方法可以实例化“WindowControlButtons”类,这样我就可以轻松地将它们添加到菜单栏中,还是必须自己添加一个按钮组并为每个平台的按钮设置样式 示例:它在Windows上的外观: 根据@mcwolf先生的建议,您可以尝试以下解决方案 import javafx.application.Appli

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

示例:它在Windows上的外观:


根据
@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上的输出如下所示


JavaFX没有用于此目的的API。据我所知,秋千也不会。IntelliJ可能正在使用某种方式。一种可能的选择是使用不带装饰的窗口。因此,窗口布局完全掌握在程序员手中。mcwolf的方法可能无法很好地融入操作系统的“标准窗口外观”,即使它只是不同版本的windows…完全正确。窗口装饰本身由OS(窗口管理器)制作。因此,自定义解决方案可能与其他窗口的布局不同,但无论平台如何,它都是相同的。这可能是优点也可能是缺点,具体取决于具体要求。这是一个类似的解决方案,在linux下。@mrmcwolf感谢您的验证,我目前没有linux机器来检查这个问题。