怎样创建一个在JavaFX中显示8个文本字段的for循环?

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

How can I make a for loop that will display 8 TextFields in javaFx

问题

这是我目前所拥有的代码...

int nums = 8;
for (int j = 0; j < nums; j++) {
   TextField test = new TextField();
   test.setAlignment(Pos.CENTER);
}
this.getChildren().add(test);

我尝试过类似 TextField 'test' + j = new TextField(); 的方式,以便创建 test1、test2、test3 等,但这会导致语法错误。我不太确定如何以其他方式进行操作。

英文:

This is currently the code that I have so far...

int nums = 8;
for (int j = 0; j &lt; nums; j++) {
   TextField test = new TextField();
   test.setAlignment(Pos.CENTER);
}
this.getChildren().add(test);

I have tried doing somehting like TextField 'test' + j = new TextField(); so that it would create test1, test2, test3 ect. but that gave syntax errors. Not really sure on how I would go about this in any other way.

答案1

得分: 3

你需要将this.getChildren().add(test);这行代码放在for循环内部:

int nums = 8;
for (int j = 0; j < nums; j++) {
   TextField test = new TextField();
   test.setAlignment(Pos.CENTER);
   this.getChildren().add(test);
}
英文:

You have to move this.getChildren().add(test); inside the for-loop:

int nums = 8;
for (int j = 0; j &lt; nums; j++) {
   TextField test = new TextField();
   test.setAlignment(Pos.CENTER);
   this.getChildren().add(test);
}

答案2

得分: 2

将它们添加到能够垂直堆叠它们的容器中。同时,确保将它们的添加调用放在仍然在作用域内的 test 所在的循环内。

VBox box = new VBox(5);
int nums = 8;
for (int j = 0; j < nums; j++) {
   TextField test = new TextField();
   test.setAlignment(Pos.CENTER);
   box.getChildren().add(test);
}
this.getChildren().add(box);
英文:

Add them to something that allows them to stack vertically. Also, make sure your call to add them is inside the loop where test is still in scope.

VBox box = new VBox(5);
int nums = 8;
for (int j = 0; j &lt; nums; j++) {
   TextField test = new TextField();
   test.setAlignment(Pos.CENTER);
   box.getChildren().add(test);
}
this.getChildren().add(box);

答案3

得分: 0

@Dustin R的答案很好我只是在补充更多内容例如如何在每个创建的`TextField`上添加事件

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TestingGroundsTwo extends Application
{
    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage stage)
    {
        VBox root = new VBox();

        for (int i = 0; i < 8; i++) {
            TextField tempTextField = new TextField();//创建文本字段。
            final int t = i;
            tempTextField.setPromptText("我是文本字段 " + t);//将提示文本设置为便于识别文本字段。
            //在文本字段上创建键盘事件处理事件。
            tempTextField.setOnKeyReleased((event) -> {
                System.out.println("您在文本字段 " + t + " 中键入了 " + event.getCode() + "。我的文本值是 " + tempTextField.getText());
            });
            root.getChildren().add(tempTextField);//将文本字段添加到父节点中。在这种情况下是 VBox。
        }

        Platform.runLater(() -> root.requestFocus());
        stage.setTitle("Hello World!");
        Scene scene = new Scene(root, 750, 600);
        scene.setFill(Color.GHOSTWHITE);
        stage.setScene(scene);
        stage.show();
    }
}
英文:

@Dustin R's answer is great. I am just adding more. Like how to any an event on each TextField created.

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TestingGroundsTwo extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage stage)
{
VBox root = new VBox();
for (int i = 0; i &lt; 8; i++) {
TextField tempTextField = new TextField();//Create the TextField.
final int t = i;
tempTextField.setPromptText(&quot;I am TextField &quot; + t);//Set prompt text to easily identify TextField
//Create key handlding event on the TextField
tempTextField.setOnKeyReleased((event) -&gt; {
System.out.println(&quot;You typed &quot; + event.getCode() + &quot; in TextField &quot; + t + &quot;. My text value is &quot; + tempTextField.getText());
});
root.getChildren().add(tempTextField);//Add the TextField to a parent node. In this case VBox.
}
Platform.runLater(() -&gt; root.requestFocus());
stage.setTitle(&quot;Hello World!&quot;);
Scene scene = new Scene(root, 750, 600);
scene.setFill(Color.GHOSTWHITE);
stage.setScene(scene);
stage.show();
}
}

huangapple
  • 本文由 发表于 2020年3月4日 04:45:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/60515242.html
匿名

发表评论

匿名网友

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

确定