英文:
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 < 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 < 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 < 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 < 8; i++) {
TextField tempTextField = new TextField();//Create the TextField.
final int t = i;
tempTextField.setPromptText("I am TextField " + t);//Set prompt text to easily identify TextField
//Create key handlding event on the TextField
tempTextField.setOnKeyReleased((event) -> {
System.out.println("You typed " + event.getCode() + " in TextField " + t + ". My text value is " + tempTextField.getText());
});
root.getChildren().add(tempTextField);//Add the TextField to a parent node. In this case 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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论