java 在 Javafx 布局中对齐按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13840244/
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
Aligning buttons in Javafx layouts
提问by sazap10
I would like to align my button so that the first button is on the left of the screen and the other two are on the right. I am currently using HBox to try to position them but i cannot seem to able to figure out how to lay them out properly. The code below is what i am using now.
我想对齐我的按钮,使第一个按钮位于屏幕左侧,另外两个位于右侧。我目前正在使用 HBox 来尝试定位它们,但我似乎无法弄清楚如何正确布置它们。下面的代码是我现在使用的。
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class ButtonTest extends Application {
private Button min, close, openfile;
public static void main(String[] args){
launch(args);
}
@Override
public void start(final Stage stage) throws Exception {
stage.setTitle("Button Test");
Group root = new Group();
BorderPane borderpane = new BorderPane();
setUpButtons();
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.getChildren().add(openfile);
HBox hbox1 = new HBox();
hbox1.setAlignment(Pos.CENTER_RIGHT);
hbox1.getChildren().addAll(min, close);
hbox.getChildren().add(hbox1);
HBox.setHgrow(hbox1, Priority.ALWAYS);
borderpane.setTop(hbox);
root.getChildren().add(borderpane);
Scene scene = new Scene(root,800,600);
stage.setFullScreen(true);
scene.getStylesheets().add("button.css");
stage.setScene(scene);
stage.show();
}
private void setUpButtons() {
close = new Button("x");
close.setId("closeBtn");
min = new Button("_");
min.setId("minBtn");
openfile = new Button("Open file");
openfile.setId("openFileBtn");
}
}
Any help would be appreciated Thanks
任何帮助将不胜感激谢谢
回答by ftkg
Set the aligment of your outer HBox (hbox) to LEFT, and the aligment of your inner HBox (hbox1) to RIGHT.
将外部 HBox (hbox) 的对齐设置为 LEFT,将内部 HBox (hbox1) 的对齐设置为 RIGHT。
Then you can have outer content on the left, and inner content on the right.
然后你可以在左边有外部内容,在右边有内部内容。
*EDIT: got your problem now. Stop using Group and add the borderpane directly to the Scene:
*编辑:现在有你的问题。停止使用 Group 并将边框直接添加到场景中:
Scene scene = new Scene(borderpane,800,600);