3

我在单行上有一个 DB 登录表单,如下所示,

    // Hostname Input
    Label hostName = new Label("Host:");
    grid.add(hostName, 0, 2);
    
    final TextField host = new TextField();
    host.getText();
    GridPane.setConstraints(host, 1, 2);
    grid.getChildren().add(host);
    
    // Port Input
    Label portName = new Label("Port:");
    grid.add(portName, 2, 2);

    final TextField port = new TextField();
    port.getText();
    GridPane.setConstraints(port, 3, 2);
    grid.getChildren().add(port);
    
    // SID Input
    Label sidName = new Label("SID:");
    grid.add(sidName, 4, 2);
    
    final TextField sid = new TextField();
    sid.getText();
    GridPane.setConstraints(sid, 5, 2);
    grid.getChildren().add(sid);
    
    // Username Input
    Label userName = new Label("Username:");
    grid.add(userName, 6, 2);
    
    final TextField user = new TextField();
    user.getText();
    GridPane.setConstraints(user, 7, 2);
    grid.getChildren().add(user);
    
    // Password Input
    Label pw = new Label("Password:");
    grid.add(pw, 8, 2);
    
    final TextField password = new PasswordField();
    password.getText();
    GridPane.setConstraints(password, 9, 2);
    grid.getChildren().add(password);

在以下情况下我需要添加更多行:

  1. 当 txt 输入文件超过一行时。

    ex) 有一行时,不需要扩展,有两行时,需要再增加一行。

  2. 当用户单击按钮添加行时。

是否可以在 Java FX 中做到这一点?请帮忙!!

感谢您的关注!

4

1 回答 1

2

为什么不只gridPane.addRow()在阅读文本文件时使用?

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();
    int lineCounter = 0;

    while (line != null) {
        lineCounter++;
        if(lineCounter > 1){
            gridPane.addRow(lineCounter-1, nodeThatYouWant);
        }
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
}
于 2015-03-11T07:56:42.377 回答