我正在尝试将特定节点设置为选项卡窗格上的多个选项卡.问题是,只有最后一个选项卡在启动应用程序时具有该节点,但其余选项卡显示为空.
我附上代码和一些屏幕截图来解释问题:
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(); } }
如果我正在做什么有什么问题或者它是一个已知的错误,请告诉我?
在JavaFX中,每个都Node
可以只有一个(1)Parent
.请参阅JavaFX API中的Node Class Page.如果你将一个添加Node
到另一个Parent
,Node
它将失去与旧的"连接" Parent
,这意味着它将不会显示或访问旧的Parent
.在源代码中导致此问题的方法是tab.setContent(myText);
.
要解决您的问题,您必须创建五个不同的(=单独的)对象,并将它们中的每一个设置TabPane
为与子//内容完全相同的一个对象.