访问 JavaFX SceneBuilder 应用中的字段

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

Accessing fields in a JavaFX SceneBuilder app

问题

IntelliJ IDEA + Oracle JDK 14 + JavaFX 14

我正在制作一个简单的JavaFX计算器风格的应用程序,并使用SceneBuilder。

除了引用单独的唯一的fx:id之外,是否有更简单的方法来引用SceneBuilder构建的应用程序中的字段?例如,如果我在SceneBuilder中构建了一个具有一些数据行和列的UI:

  1. ---------------------
  2. | field00 | field01 |
  3. | field10 | field11 |
  4. ---------------------

并且我想对数据进行一些数学运算。是否有一种更简单的方法来引用这些字段,而不是引用静态名称?

  1. @FXML private TextField field00;
  2. @FXML private TextField field01;
  3. @FXML private TextField field10;
  4. @FXML private TextField field11;

我正在做一些可能非常幼稚的事情,例如:如果field00不为空,如果field01不为空,...。我已经通过创建一个表示每行的类并将这些对象读入ArrayList来改善了一些,但是用于创建对象的代码仍然很丑陋,例如:

  1. RowList myRowList = new RowList();
  2. MyRowClass aRow =
  3. new MyRowClass(
  4. Double.parseDouble(field00.getText()),
  5. Double.parseDouble(field01.getText()));
  6. myRowList.addRow(aRow);
  7. aRow =
  8. new Assignment(
  9. Double.parseDouble(field10.getText()),
  10. Double.parseDouble(field11.getText()));
  11. myRowList.addRow(aRow);

我希望能够以某种方式迭代访问这些字段。

另外,我想在UI中添加一个“添加一行”按钮,并将该行包含在计算中,这将引入一些非静态命名的字段。

在这里的解决方案是跳过SceneBuilder,直接在我的代码中创建UI吗?

英文:

Intellij IDEA + Oracle JDK 14 + JavaFX 14

I'm making a simple JavaFX calculator-style app, and using SceneBuilder.

Is there an easier way to reference the fields in a SceneBuilder-built app, other than referring to individual unique fx:id's? For example, if I have a UI built in SceneBuilder that has some rows and columns of data:

  1. ---------------------
  2. | field00 | field01 |
  3. | field10 | field11 |
  4. ---------------------

and I want to do some math on the data. Is there an easier way to reference the fields other than referring to static names?

  1. @FXML private TextField field00;
  2. @FXML private TextField field01;
  3. @FXML private TextField field10;
  4. @FXML private TextField field11;

I'm doing what has to be super naive stuff like: if field00 is not null and if field01 is not null and .... I've made it a bit better by creating a class to represent each row and I'm reading those objects into an ArrayList, but my code to create the objects is still ugly stuff like:

  1. RowList myRowList = new RowList();
  2. MyRowClass aRow =
  3. new MyRowClass(
  4. Double.parseDouble(field00.getText()),
  5. Double.parseDouble(field01.getText()));
  6. myRowList.addRow(aRow);
  7. aRow =
  8. new Assignment(
  9. Double.parseDouble(field10.getText()),
  10. Double.parseDouble(field11.getText()));
  11. myRowList.addRow(aRow);

I'd like to be able to iterate through the fields in some way.

Also, I'd like to add an "add a row" button to the UI and include that row in the calculations which would introduce some non-statically named fields to the mix.

Is the solution here to skip SceneBuilder and create the UI directly in my code?

答案1

得分: 1

如果您将TextFields放在一个容器中,比如一个HBox,您可以在容器对象上使用.getChildren()方法来获取它的所有子节点。然后,您可以像这样循环遍历它们:

  1. HBox h = new HBox();
  2. h.getChildren().add(new TextField());
  3. h.getChildren().add(new TextField());
  4. h.getChildren().add(new TextField());
  5. // 遍历HBox中的每个节点
  6. for (Node n : h.getChildren())
  7. {
  8. // 如果节点是TextField类型,并且内部文本不为null
  9. if (n instanceof TextField && ((TextField) n).getText() != null)
  10. {
  11. // 做一些操作
  12. }
  13. }
英文:

If you put the TextFields inside a container such as a HBox, you can use the .getChildren() method on the container object to get all of its children nodes. You could then loop through all of them like this:

  1. HBox h = new HBox();
  2. h.getChildren().add(new TextField());
  3. h.getChildren().add(new TextField());
  4. h.getChildren().add(new TextField());
  5. // loop through each node in the HBox
  6. for (Node n : h.getChildren())
  7. {
  8. // if the Node is of type TextField, and the text inside is not null
  9. if (n instanceof TextField && ((TextField) n).getText() != null)
  10. {
  11. // do something
  12. }
  13. }

huangapple
  • 本文由 发表于 2020年8月9日 00:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63317889.html
匿名

发表评论

匿名网友

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

确定