SetAlignment method in JavaFX

本秂侑毒 提交于 2020-01-24 22:24:29

问题


1) When I run it, all the objects placed in the TOP_CENTER as you can see in the code I tried to place the button in the BOTTOM_RIGHT and it didn't work.

2) Can a Scene include two layouts? (Two VBox-es for example).

public void NewQuestion ()
{
    sum++;
    t=new Text("Question number: "+sum);

    textfield=new TextField();
    pane = new VBox();

    Button NextQuestion = new Button ("Next Question");
    NextQuestion.setOnAction(e-> NextQuestionButtonClicked(e));
    pane.getChildren().addAll(t, textfield, NextQuestion);
    pane.setAlignment(Pos.TOP_CENTER);
    NextQuestion.setAlignment(Pos.BOTTOM_RIGHT);//<---


    Scene mainscene = new Scene(pane,420, 530);


    Qstage.setScene(mainscene);


}

回答1:


Answering your second question first : each layout is a node so you can make a Hbox and then add 2 Vboxs to it and in a similar fashion have all combinations of it. since each button is also a node, you can add a Vbox and a button to an Hbox to position them horizontally with respect to each other

Now back to your first question: Here is a list and an example with Pos.CENTER and Pos.CENTER_LEFT :

Hope this helps. Feel free to ask any more questions about this




回答2:


1) button.setAlignment sets how the text and icons are places inside the button (javadoc). To align the button within a pane you should apply desired alignment to the pane itself. In you case:

pane.setAlignment(Pos.BOTTOM_RIGHT);

instead of

pane.setAlignment(Pos.TOP_CENTER);

2) The Scene should have a single root. But the root itself may be a VBox or HBox and you could put multiple boxes inside other box.



来源:https://stackoverflow.com/questions/40447500/setalignment-method-in-javafx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!