英文:
Javafx Gridpane layout being really weird
问题
这是我的Settings类。
public class Settings {
public static void display() {
GridPane layout = new GridPane();
layout.setPadding(new Insets(10));
layout.setVgap(8);
layout.setHgap(10);
Scene scene = new Scene(layout, 600, 400);
//标题标签
Label label = new Label("设置");
GridPane.setConstraints(label, 1, 0);
//分钟标签
Label minutesLabel = new Label("分钟:");
GridPane.setConstraints(minutesLabel, 0, 1);
//分钟输入
TextField minutesInput = new TextField();
minutesInput.setPromptText("分钟");
GridPane.setConstraints(minutesInput, 1, 1);
//秒钟标签
Label secondsLabel = new Label("秒钟:");
GridPane.setConstraints(secondsLabel, 0, 2);
//秒钟输入
TextField secondsInput = new TextField();
secondsInput.setPromptText("秒钟");
GridPane.setConstraints(secondsInput, 1, 2);
//确定按钮
Button okButton = new Button("确定");
okButton.setOnAction(e -> {
try {
int minutes = Integer.parseInt(minutesInput.getText());
int seconds = Integer.parseInt(secondsInput.getText());
if(minutes > 59) minutes = 59;
else if(minutes < 1) minutes = 1;
if(seconds > 59) seconds = 59;
else if(seconds < 1) seconds = 1;
Main.updateMinsAndSecs(minutes, seconds);
Main.setSceneMain();
}catch(NumberFormatException numberFormatException) {
label.setText("请输入有效的数字");
}catch(FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
});
okButton.setStyle("-fx-background-radius: 0;");
GridPane.setConstraints(okButton, 0, 3);
//取消按钮
Button cancelButton = new Button("取消");
cancelButton.setOnAction(e -> {
Main.setSceneMain();
});
cancelButton.setStyle("-fx-background-radius: 0;");
GridPane.setConstraints(cancelButton, 1, 3);
//应用按钮
Button applyButton = new Button("应用");
applyButton.setOnAction(e -> {
try {
int minutes = Integer.parseInt(minutesInput.getText());
int seconds = Integer.parseInt(secondsInput.getText());
if(minutes > 59) minutes = 59;
else if(minutes < 1) minutes = 1;
if(seconds > 59) seconds = 59;
else if(seconds < 1) seconds = 1;
Main.updateMinsAndSecs(minutes, seconds);
}catch(NumberFormatException numberFormatException) {
label.setText("请输入有效的数字");
}catch(FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
});
applyButton.setStyle("-fx-background-radius: 0;");
GridPane.setConstraints(applyButton, 2, 3);
layout.getChildren().addAll(label, minutesLabel, minutesInput, secondsLabel, secondsInput, okButton, cancelButton, applyButton);
Main.window.setScene(scene);
Main.window.setTitle("设置");
}
}
这是它的最终效果。
我希望解决方案不使用FXML。Stackoverflow说我的帖子主要是代码,需要更多细节,所以我还要说一下,我使用GridPane创建了一个简单的登录界面,那个界面效果很好。代码基本上是相同的,除了定位和内容。
英文:
I tried to just make a simple Gridpane that would ask the user to input two numbers. For some reason the elements just went all over the place. I'm pretty sure I got the constraints correct.
This is my Settings class.
public class Settings {
public static void display() {
GridPane layout = new GridPane();
layout.setPadding(new Insets(10));
layout.setVgap(8);
layout.setHgap(10);
Scene scene = new Scene(layout, 600, 400);
//Title label
Label label = new Label("Settings");
GridPane.setConstraints(label, 1, 0);
//Minutes label
Label minutesLabel = new Label("Minutes:");
GridPane.setConstraints(minutesLabel, 0, 1);
//Minutes input
TextField minutesInput = new TextField();
minutesInput.setPromptText("minutes");
GridPane.setConstraints(minutesInput, 1, 1);
//Seconds label
Label secondsLabel = new Label("Seconds:");
GridPane.setConstraints(secondsLabel, 0, 2);
//Seconds input
TextField secondsInput = new TextField();
secondsInput.setPromptText("seconds");
GridPane.setConstraints(secondsInput, 1, 2);
//Ok button
Button okButton = new Button("Ok");
okButton.setOnAction(e -> {
try {
int minutes = Integer.parseInt(minutesInput.getText());
int seconds = Integer.parseInt(secondsInput.getText());
if(minutes > 59) minutes = 59;
else if(minutes < 1) minutes = 1;
if(seconds > 59) seconds = 59;
else if(seconds < 1) seconds = 1;
Main.updateMinsAndSecs(minutes, seconds);
Main.setSceneMain();
}catch(NumberFormatException numberFormatException) {
label.setText("Please enter valid number(s)");
}catch(FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
});
okButton.setStyle("-fx-background-radius: 0;");
GridPane.setConstraints(secondsInput, 0, 3);
//Cancel button
Button cancelButton = new Button("Cancel");
cancelButton.setOnAction(e -> {
Main.setSceneMain();
});
cancelButton.setStyle("-fx-background-radius: 0;");
GridPane.setConstraints(secondsInput, 1, 3);
//Apply button
Button applyButton = new Button("Apply");
applyButton.setOnAction(e -> {
try {
int minutes = Integer.parseInt(minutesInput.getText());
int seconds = Integer.parseInt(secondsInput.getText());
if(minutes > 59) minutes = 59;
else if(minutes < 1) minutes = 1;
if(seconds > 59) seconds = 59;
else if(seconds < 1) seconds = 1;
Main.updateMinsAndSecs(minutes, seconds);
}catch(NumberFormatException numberFormatException) {
label.setText("Please enter valid number(s)");
}catch(FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
});
applyButton.setStyle("-fx-background-radius: 0;");
GridPane.setConstraints(secondsInput, 2, 3);
layout.getChildren().addAll(label, minutesLabel, minutesInput, secondsLabel, secondsInput, okButton, cancelButton, applyButton);
Main.window.setScene(scene);
Main.window.setTitle("Settings");
}
}
This is how it turned out.
I would like the solution to not use fxml.
Stackoverflow says my post is mostly code and I need more details so I'm also going to say that I have used a Gridpane to create a simple login screen and that one was pretty good. The code was basically the same except the positioning and the content.
答案1
得分: -1
你正在将不同的约束条件应用于 secondsInput
,而应该应用于 okButton
、cancelButton
和 applyButton
:
GridPane.setConstraints(secondsInput, 1, 2);
GridPane.setConstraints(secondsInput, 0, 3);
GridPane.setConstraints(secondsInput, 1, 3);
GridPane.setConstraints(secondsInput, 2, 3);
而应该改为:
GridPane.setConstraints(secondsInput, 1, 2);
GridPane.setConstraints(okButton, 0, 3);
GridPane.setConstraints(cancelButton, 1, 3);
GridPane.setConstraints(applyButton, 2, 3);
英文:
You are setting different constraints to secondsInput
instead of okButton
, cancelButton
and applyButton
:
GridPane.setConstraints(secondsInput, 1, 2);
GridPane.setConstraints(secondsInput, 0, 3);
GridPane.setConstraints(secondsInput, 1, 3);
GridPane.setConstraints(secondsInput, 2, 3);
Instead of:
GridPane.setConstraints(secondsInput, 1, 2);
GridPane.setConstraints(okButton, 0, 3);
GridPane.setConstraints(cancelButton, 1, 3);
GridPane.setConstraints(applyButton, 2, 3);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论