Javafx Gridpane 布局表现非常奇怪

huangapple go评论113阅读模式
英文:

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("设置");
    }
}

这是它的最终效果。

screenshot

我希望解决方案不使用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(&quot;Settings&quot;);
GridPane.setConstraints(label, 1, 0);
//Minutes label
Label minutesLabel = new Label(&quot;Minutes:&quot;);
GridPane.setConstraints(minutesLabel, 0, 1);
//Minutes input
TextField minutesInput = new TextField();
minutesInput.setPromptText(&quot;minutes&quot;);
GridPane.setConstraints(minutesInput, 1, 1);
//Seconds label
Label secondsLabel = new Label(&quot;Seconds:&quot;);
GridPane.setConstraints(secondsLabel, 0, 2);
//Seconds input
TextField secondsInput = new TextField();
secondsInput.setPromptText(&quot;seconds&quot;);
GridPane.setConstraints(secondsInput, 1, 2);
//Ok button
Button okButton = new Button(&quot;Ok&quot;);
okButton.setOnAction(e -&gt; {
try {
int minutes = Integer.parseInt(minutesInput.getText());
int seconds = Integer.parseInt(secondsInput.getText());
if(minutes &gt; 59) minutes = 59;
else if(minutes &lt; 1) minutes = 1;
if(seconds &gt; 59) seconds = 59;
else if(seconds &lt; 1) seconds = 1;
Main.updateMinsAndSecs(minutes, seconds);
Main.setSceneMain();
}catch(NumberFormatException numberFormatException) {
label.setText(&quot;Please enter valid number(s)&quot;);
}catch(FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
});
okButton.setStyle(&quot;-fx-background-radius: 0;&quot;);
GridPane.setConstraints(secondsInput, 0, 3);
//Cancel button
Button cancelButton = new Button(&quot;Cancel&quot;);
cancelButton.setOnAction(e -&gt; {
Main.setSceneMain();
});
cancelButton.setStyle(&quot;-fx-background-radius: 0;&quot;);
GridPane.setConstraints(secondsInput, 1, 3);
//Apply button
Button applyButton = new Button(&quot;Apply&quot;);
applyButton.setOnAction(e -&gt; {
try {
int minutes = Integer.parseInt(minutesInput.getText());
int seconds = Integer.parseInt(secondsInput.getText());
if(minutes &gt; 59) minutes = 59;
else if(minutes &lt; 1) minutes = 1;
if(seconds &gt; 59) seconds = 59;
else if(seconds &lt; 1) seconds = 1;
Main.updateMinsAndSecs(minutes, seconds);
}catch(NumberFormatException numberFormatException) {
label.setText(&quot;Please enter valid number(s)&quot;);
}catch(FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
});
applyButton.setStyle(&quot;-fx-background-radius: 0;&quot;);
GridPane.setConstraints(secondsInput, 2, 3);
layout.getChildren().addAll(label, minutesLabel, minutesInput, secondsLabel, secondsInput, okButton, cancelButton, applyButton);
Main.window.setScene(scene);
Main.window.setTitle(&quot;Settings&quot;);
}
}

This is how it turned out.

screenshot

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,而应该应用于 okButtoncancelButtonapplyButton

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);

huangapple
  • 本文由 发表于 2020年8月2日 21:16:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63216447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定