如何在JavaFX中设置标签位置

时间:2013-06-04 18:02:52

标签: javafx-2 javafx javafx-8

我有这个带有标签的JavaFX代码。你能告诉我如何设置标签面板的位置始终位于主舞台的左侧:

VBox stackedTitledPanes = createStackedTitledPanes();

        ScrollPane scroll = makeScrollable(stackedTitledPanes);

        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
        // 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.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
        // 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
        // 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(395, 580);
        mainPane.setLayoutX(850);
        mainPane.setLayoutY(32);

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

        root.getChildren().add(mainPane);

1 个答案:

答案 0 :(得分:3)

<强>更新

您希望将stackedTitledPaneVBox更改为BorderPane。 所以这段代码:

VBox stackedTitledPanes = new VBox();

变为:

BorderPane stackedTitledPanes = new BorderPane();

然后,当您想要添加节点时,指定要放置它们的部分(我在中心窗格中放置一个标签作为填充符):

stackedTitledPanes.setLeft(mainPane);
stackedTitledPanes.setCenter(new Label("Main Content"));

旧答案

这会将标签放在左侧(我假设这是您正在寻找的):

    tabPane.setSide(Side.LEFT);

TabPane Left side