JavaFX:将同一节点设置为多个选项卡的问题

 回__复卷轴 发布于 2023-02-13 12:53

我正在尝试将特定节点设置为选项卡窗格上的多个选项卡.问题是,只有最后一个选项卡在启动应用程序时具有该节点,但其余选项卡显示为空.

我附上代码和一些屏幕截图来解释问题:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TabExample extends Application
{

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

@Override
public void start(Stage primaryStage)
{
    primaryStage.setTitle("Tabs");
    Group root = new Group();
    Scene scene = new Scene(root, 400, 250, Color.WHITE);

    TabPane tabPane = new TabPane();

    BorderPane borderPane = new BorderPane();

    Text myText = new Text("Hello");

    for (int i = 0; i < 5; i++)
    {
        Tab tab = new Tab();
        tab.setText("Tab"
            + i);
        HBox hbox = new HBox();
        hbox.getChildren().add(new Label("Tab"
            + i));
        hbox.setAlignment(Pos.CENTER);
        tab.setContent(myText);
        tab.setClosable(false);
        tabPane.getTabs().add(tab);
    }
    tabPane.setSide(Side.BOTTOM);

    // bind to take available space
    borderPane.prefHeightProperty().bind(scene.heightProperty());
    borderPane.prefWidthProperty().bind(scene.widthProperty());

    borderPane.setCenter(tabPane);
    root.getChildren().add(borderPane);
    primaryStage.setScene(scene);
    primaryStage.show();
}
}

在此输入图像描述

如果我正在做什么有什么问题或者它是一个已知的错误,请告诉我?

1 个回答
  • 在JavaFX中,每个都Node可以只有一个(1)Parent.请参阅JavaFX API中的Node Class Page.如果你将一个添加Node到另一个Parent,Node它将失去与旧的"连接" Parent,这意味着它将不会显示或访问旧的Parent.在源代码中导致此问题的方法是tab.setContent(myText);.

    要解决您的问题,您必须创建五个不同的(=单独的)对象,并将它们中的每一个设置TabPane为与子//内容完全相同的一个对象.

    2023-02-13 12:59 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有