java JavaFX 中的 SetAlignment 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40447500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SetAlignment method in JavaFX
提问by Naor Levi
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.
1)当我运行它时,所有对象都放置在 TOP_CENTER 中,正如您在代码中看到的那样,我试图将按钮放置在 BOTTOM_RIGHT 中,但它不起作用。
2) Can a Scene
include two layouts? (Two VBox
-es for example).
2) 可以Scene
包含两个布局吗? (VBox
例如两个-es)。
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);
}
回答by Ishan Srivastava
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
首先回答您的第二个问题:每个布局都是一个节点,因此您可以制作一个 Hbox,然后向其中添加 2 个 Vbox,并以类似的方式拥有它的所有组合。由于每个按钮也是一个节点,因此您可以将 Vbox 和按钮添加到 Hbox 以将它们相对于彼此水平放置
Now back to your first question:
Here is a list and an example with Pos.CENTER and Pos.CENTER_LEFT :
现在回到你的第一个问题:这是一个列表和一个 Pos.CENTER 和 Pos.CENTER_LEFT 的例子:
Hope this helps. Feel free to ask any more questions about this
希望这可以帮助。请随时提出有关此的更多问题
回答by kgeorgiy
1) button.setAlignment
sets how the text and icons are places insidethe button (javadoc). To align the button
within a pane
you should apply desired alignment to the pane
itself. In you case:
1)button.setAlignment
设置文本和图标在按钮内的放置方式( javadoc)。要button
在 a 内对齐,pane
您应该将所需的对齐应用于pane
自身。在你的情况下:
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.
2)Scene
应该有一个root
. 但它root
本身可能是一个VBox
或HBox
,你可以在另一个盒子里放多个盒子。