如何在JavaFX中的inputPane
中将面板BorderPane
居中?
这是我的代码。
public class MainStage {
private Scene mainScene;
private InputPane inputPane;
private DisplayPane displayPane;
private EditPane editPane;
private BorderPane borderPane;
private VBox mainPane;
public MainStage(){
borderPane = new BorderPane();
inputPane=new InputPane();
displayPane=new DisplayPane();
editPane=new EditPane();
mainPane=new VBox();
mainPane.getChildren().addAll(inputPane.getPane(),displayPane.getTable(),editPane.getPane());
mainPane.setSpacing(10);
mainScene=new Scene(mainPane,800,480);
mainScene.getStylesheets().add("styleSheet.css");
}
public Scene getMainScene() {
return mainScene;
}
public InputPane getInputPane() {
return inputPane;
}
public DisplayPane getDisplayPane() {
return displayPane;
}
public EditPane getEditPane(){
return editPane;
}
}
我正在尝试修改
InputPane
类。public class InputPane {
private Text server;
private Text textNumber;
private TextField tfNumber;
private Button addButton;
private GridPane griglia;
public InputPane() {
textNumber=new Text("Insert Port Number: ");
server = new Text("SERVER");
server.setStyle("-fx-font-weight:bold;"
+"-fx-color: black;"
+"-fx-font-size: 18;");
tfNumber = new TextField();
tfNumber.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
griglia=new GridPane();
griglia.add(server,1,0);
griglia.add(textNumber,0,1);
griglia.add(tfNumber,1,1);
griglia.setVgap(5);
griglia.setHgap(10);
GridPane.setHalignment(server, HPos.CENTER);
GridPane.setHalignment(textNumber, HPos.CENTER);
}
public GridPane getPane() {
return griglia;
}
public Button getAddButton() {
return addButton;
}
}
我给您一些想法,您现在可以看到我的GUI:
这是结果
我想这样想
谢谢
最佳答案
您的问题并不十分清楚:
如何在JavaFX的BorderPane中将面板inputPane居中?
但是在您的示例中,您从不使用BorderPane
,而仅使用VBox
要回答您的问题,只需将HBox
用作GridPane
的父项,并且将alignment属性设置为Pos.CENTER
。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane bord = new BorderPane();
HBox hb = new HBox();
GridPane griglia = new GridPane();
Text server = new Text("SERVER");
griglia.add(server, 1, 0);
griglia.add(new Text("Numero Porta"), 0, 1);
griglia.add(new TextField(), 1, 1);
griglia.setVgap(5.0);
griglia.setHgap(5.0);
GridPane.setValignment(server, VPos.CENTER);
GridPane.setHalignment(server, HPos.CENTER);
hb.getChildren().add(griglia);
hb.setStyle("-fx-border-color:red");
hb.setAlignment(Pos.CENTER);
bord.setTop(hb);
primaryStage.setScene(new Scene(bord, 800, 500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}