不确定为什么按钮在JavaFX中不能相邻对齐
我不熟悉javafx。我正在创建一个基本的GUI,其外观如下: 但是,当我尝试时,我的图像如下所示: 我不确定为什么两个按钮之间有很大的间隙。我使用的是网格窗格格式。 这是我的密码:不确定为什么按钮在JavaFX中不能相邻对齐,java,user-interface,javafx,gridpane,Java,User Interface,Javafx,Gridpane,我不熟悉javafx。我正在创建一个基本的GUI,其外观如下: 但是,当我尝试时,我的图像如下所示: 我不确定为什么两个按钮之间有很大的间隙。我使用的是网格窗格格式。 这是我的密码: public class Main extends Application { @Override public void start(Stage stage) { //Creating Text field TextField textField1 = new TextField();
public class Main extends Application {
@Override
public void start(Stage stage) {
//Creating Text field
TextField textField1 = new TextField();
//Creating Buttons
Button submit = new Button("Submit");
Button cancel = new Button("Cancel");
//Creating a Grid Pane
GridPane gridPane = new GridPane();
//Setting size for the pane
gridPane.setMinSize(200, 100);
//Setting the padding
gridPane.setPadding(new Insets(10, 10, 10, 10));
//Setting the vertical and horizontal gaps between the columns
gridPane.setVgap(5);
gridPane.setHgap(1);
//Setting the Grid alignment
gridPane.setAlignment(Pos.CENTER);
//Arranging all the nodes in the grid
gridPane.add(textField1, 0, 0);
gridPane.add(submit, 0, 1);
gridPane.add(cancel, 1,1);
//Creating a scene object
Scene scene = new Scene(gridPane);
//Setting title to the Stage
stage.setTitle("Simple Form");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
非常感谢您的帮助:-)这里发生的事情是您的文本字段设置在左上角(0,0),但只跨一列。您可以看到位于右下角(1,1)的cancel按钮从TextField停止的位置开始(如果只查看x位置) 网格窗格有一个调试选项,因此您可以可视化更多发生的事情。就打电话
gridPane.setGridLinesVisible(true):
解决方案是将TextField设置为获取/span 2列。您可以通过以下方式进行操作:
GridPane.setColumnSpan(textField1, 2);
或者使用不同的add
方法:
gridPane.add(textField1, 0, 0, 2, 1);
有关GridPane如何工作的更多信息,请参见 这里发生的情况是,您的文本字段设置在左上角(0,0),但只跨一列。您可以看到位于右下角(1,1)的cancel按钮从TextField停止的位置开始(如果只查看x位置) 网格窗格有一个调试选项,因此您可以可视化更多发生的事情。就打电话
gridPane.setGridLinesVisible(true):
解决方案是将TextField设置为获取/span 2列。您可以通过以下方式进行操作:
GridPane.setColumnSpan(textField1, 2);
或者使用不同的add
方法:
gridPane.add(textField1, 0, 0, 2, 1);
有关GridPane如何工作的更多信息,请参见