JavaFX标签无法显示,BorderPane使用问题求助
Hey there! I spot the problem right away—let's fix that missing Label for you.
What's Going Wrong
When you initialize your BorderPane with new BorderPane(arrayLabel), you're setting that arrayLabel as the center content of the BorderPane. But then you immediately overwrite the center region with root.setCenter(searchbox). Since BorderPane only allows one node per region, your original Label gets completely replaced and never renders on screen.
The Fix
You already set the Label's alignment to TOP_CENTER, so let's assign it to the BorderPane's top region instead of the center. Here's the adjusted code:
Label arrayLabel = new Label("NUMBERS"); BorderPane.setAlignment(arrayLabel, Pos.TOP_CENTER); TextField search = new TextField(); Button btn = new Button("search"); HBox searchbox = new HBox(2); searchbox.getChildren().addAll(search, btn); searchbox.setAlignment(Pos.CENTER); // Initialize an empty BorderPane instead of passing the label BorderPane root = new BorderPane(); root.setPrefSize(600, 600); // Assign the label to the TOP region (matches your alignment setting) root.setTop(arrayLabel); root.setCenter(searchbox);
Quick BorderPane Reminder
BorderPane has five distinct regions: top, bottom, left, right, and center. Each region can hold exactly one node—if you set a new node to a region, it replaces whatever was there before. That's why your original label got lost in the process!
内容的提问来源于stack exchange,提问作者Dannon Gilbert

