简体   繁体   中英

JavaFX labels won't show up

I am trying to practice with BorderPanes, which so far has been a nightmare. I have tried to get my label to just show up in the window with no avail. Any ideas?

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);

BorderPane root = new BorderPane(arrayLabel);
root.setPrefSize(600, 600);
root.setCenter(searchbox);

A BorderPane can only have one node in any region. (That node, of course, could be a parent containing arbitrary numbers of other nodes.) The constructor taking a single parameter treats that parameter as the node to be displayed in the center.

So

BorderPane root = new BorderPane(arrayLabel);

is equivalent to

BorderPane root = new BorderPane();
root.setCenter(arrayLabel);

When you subsequently (immediately) then call

root.setCenter(searchbox);

the center node is replaced by searchbox , so arraylabel is no longer part of the BorderPane .

It's not really clear what you intend here: you are essentially trying to put two different UI components ( arrayLabel and searchbox ) into the same region of the same BorderPane . There's no information (provided either to us, or to the poor BorderPane who is trying to position these nodes) as to how you want these two components positioned relative to each other. What are you actually trying to achieve, in terms of how you want these laid out?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM