双击调整JavaFX选项卡大小

3

我有一个关于JavaFX选项卡的简单示例:

    public class test extends Application
{

    private BorderPane root;

    // Navigation Utilization
    private ActionTabs actiontabs;

    @Override
    public void start(Stage primaryStage)
    {

        // Set Main Window Label
        primaryStage.setTitle("Desktop Client");
        Image iv = new Image(getClass().getResource("/images/internet-icon.png").toExternalForm());
        primaryStage.getIcons().add(iv);

        root = new BorderPane();
        root.setLeft(getLeftHBox(primaryStage, root));
        Scene scene = new Scene(root, 1000, 1000, Color.WHITESMOKE);  // Set main Stage color

        primaryStage.setScene(scene);

        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        // Set Stage boundaries to visible bounds of the main screen
        primaryStage.setX(primaryScreenBounds.getMinX());
        primaryStage.setY(primaryScreenBounds.getMinY());
        primaryStage.setWidth(primaryScreenBounds.getWidth());  // Maximum width of the display
        primaryStage.setHeight(primaryScreenBounds.getHeight());    // Maximum height of the display
        primaryStage.show();

    }

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

    private HBox getLeftHBox(Stage primaryStage, BorderPane root)
    {
        HBox hbox = new HBox();

        TabPane tabPane = new TabPane();
        BorderPane mainPane = new BorderPane();

        tabPane.setStyle("-fx-font-size: 12pt;"); // Set global size for the font
        // Create Tabs
        Tab tabA = new Tab();
        tabA.setText("Main Component");
        tabA.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
        tabA.setClosable(false); 
        // Add something in Tab
        StackPane tabA_stack = new StackPane();
        tabA_stack.setAlignment(Pos.CENTER);
        tabA_stack.getChildren().add(new Label("Label@Tab A"));
        tabA.setContent(tabA_stack);
        tabPane.getTabs().add(tabA);

        Tab tabB = new Tab();
        tabB.setText("Second Component");
        tabB.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
        tabB.setClosable(false); 
        // Add something in Tab
        StackPane tabB_stack = new StackPane();
        tabB_stack.setAlignment(Pos.CENTER);
        tabB_stack.getChildren().add(new Label("Label@Tab B"));
        tabB.setContent(tabB_stack);
        tabPane.getTabs().add(tabB);

        Tab tabC = new Tab();
        tabC.setText("Last Component");
        tabC.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
        tabC.setClosable(false); // da se mahne opciqta da se zatvarq tab
        // Add something in Tab
        StackPane tabC_vBox = new StackPane();
        tabC_vBox.setAlignment(Pos.CENTER);
        tabC_vBox.getChildren().add(new Label("Label@Tab C"));
        tabC.setContent(tabC_vBox);
        tabPane.getTabs().add(tabC);

        mainPane.setCenter(tabPane);

        mainPane.setPrefSize(300, 500);
        //mainPane.setLayoutX(5);     // Horizontal Position
        mainPane.setLayoutY(32);    // Vertical Position

        hbox.getChildren().addAll(mainPane);

        return hbox;
    }


}

我希望当我双击选项卡名称时,可以最大化选项卡的主体大小,并将其设置为与应用程序大小相同的宽度和高度。例如类似于Eclipse IDE 选项卡。JavaFX 可以实现吗?

编辑

这是我测试过的代码。

public BorderPane initNavigation(Stage primaryStage)
    {

        VBox stackedTitledPanes = createStackedTitledPanes();

        ScrollPane scroll = makeScrollable(stackedTitledPanes);

        final TabPane tabPane = new TabPane();
        final BorderPane mainPane = new BorderPane();

        final Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        tabPane.setStyle("-fx-font-size: 12pt;"); // Set global size for the font
        // Create Tabs
        Tab tabA = new Tab();

        tabPane.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            private double sizeX, sizeY;
            private boolean first = true;

            @Override
            public void handle(MouseEvent me)
            {
                if (first)
                {
                    sizeX = mainPane.getWidth();
                    sizeY = mainPane.getHeight();
                    first = false;
                }

                if (me.getButton().equals(MouseButton.PRIMARY) && me.getClickCount() % 2 == 0)
                {
                    if (sizeX != mainPane.getWidth() || sizeY != mainPane.getHeight())
                    {
                        mainPane.setPrefSize(sizeX, sizeY);

                    }
                    else
                    {
                        mainPane.setPrefSize(primaryScreenBounds.getWidth(), primaryScreenBounds.getHeight());
                        //mainPane.toFront();
                    }
                }
            }
        });

        tabA.setText("Main Component");
        tabA.setContextMenu(makeTabContextMenu(tabA, tabPane));  // Set right mouse click menu
        // Add something in Tab
        StackPane tabA_stack = new StackPane();
        tabA_stack.setAlignment(Pos.CENTER);
        tabA_stack.getChildren().add(scroll);
        tabA.setContent(tabA_stack);
        tabPane.getTabs().add(tabA);

        Tab tabB = new Tab();
        tabB.setText("Second Component");
        tabB.setContextMenu(makeTabContextMenu(tabB, tabPane));  // Set right mouse click menu
        // Add something in Tab
        StackPane tabB_stack = new StackPane();
        tabB_stack.setAlignment(Pos.CENTER);
        tabB_stack.getChildren().add(new Label("Label@Tab B"));
        tabB.setContent(tabB_stack);
        tabPane.getTabs().add(tabB);

        Tab tabC = new Tab();
        tabC.setText("Last Component");
        tabC.setContextMenu(makeTabContextMenu(tabC, tabPane));  // Set right mouse click menu
        // Add something in Tab
        StackPane tabC_vBox = new StackPane();
        tabC_vBox.setAlignment(Pos.CENTER);
        tabC_vBox.getChildren().add(new Label("Label@Tab C"));
        tabC.setContent(tabC_vBox);
        tabPane.getTabs().add(tabC);

        mainPane.setCenter(tabPane);
        mainPane.setPrefSize(300, 500);
        //mainPane.setLayoutX(5);     // Horizontal Position
        mainPane.setLayoutY(32);    // Vertical Position

        scroll.setPrefSize(395, 580);
        scroll.setLayoutX(5);
        scroll.setLayoutY(32);

        return mainPane;
    }

问题是,当我双击选项卡名称时,如何使用选项卡代码覆盖舞台?

在你的代码示例中,选项卡已经被最大化了。请给出一个在tabPane周围还有其他组件的示例。因为了解场景的布局非常重要,而最大化是相对于这个布局来说的。 - Uluk Biy
我更新了帖子。我有一个带有边框面板的主舞台。在左侧,我有一些简单的选项卡。我该如何修改代码呢?我想要当我点击选项卡名称时,最大化选项卡的内容并填充舞台与选项卡。类似于Eclipse中双击选项卡名称的效果。 - Peter Penzov
2个回答

2

您需要在您的代码中添加几行内容,这里是一个样例:

.....

Tab tabA = new Tab();

Label tabALabel = new Label("Main Component");
tabA.setGraphic(tabALabel);

tabALabel.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent mouseEvent) {
        if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
            if (mouseEvent.getClickCount() == 2) {

                mainPane.setPrefSize(500, 500); //Your required size

            }
       }
    }
});

....

请尝试此操作,并告知是否遇到任何困难。


还有一个问题:我希望所影响的面板优先于所有其他面板。例如,使面板覆盖所有其他面板或者隐藏所有其他面板。 - Peter Penzov
通过 hbox.toFront() 将包含主面板的 hBox 置于最前面。 - Shreyas Dave
如果(mouseEvent.getClickCount() == 2) { mainPane.setPrefSize(500, 500); //您所需的大小 hbox.toFront(); } - Shreyas Dave
在我的情况下,我有一个BorderPane,选项卡放置在上面。我尝试使用mainPane.toFront();,但没有效果。我得到了相同的结果。 - Peter Penzov
尝试像以前一样将mainPane添加到Hbox中,这样是有效的。 - Shreyas Dave

1
您可以创建另一个BorderPane,将您的root = new BorderPane();放置在中心,并在双击时用Tabpanel替换它。

最终效果为:

   rootRoot = new BorderPane();
   BorderPane  root = new BorderPane();
   root.setLeft(getLeftHBox(primaryStage, root));
   rootRoot.setCenter(root);
   Scene scene = new Scene(rootRoot, 1000, 1000, Color.WHITESMOKE);  // Set main Stage color

将“rootRoot”作为新的根目录(好名字,我知道^^),并

    Label tabALabel=new Label("Label@Tab A");
    tabALabel.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
                if (mouseEvent.getClickCount() == 2) {

                    //mainPane.setPrefSize(500, 500); //Your required size
                    rootRoot.setCenter(mainPane);
                }
           }
        }
    });

为了最大化。


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