访问 JavaFX SceneBuilder 应用中的字段

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

Accessing fields in a JavaFX SceneBuilder app

问题

IntelliJ IDEA + Oracle JDK 14 + JavaFX 14

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

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

---------------------
| field00 | field01 |
| field10 | field11 |
---------------------

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

@FXML private TextField field00;
@FXML private TextField field01;
@FXML private TextField field10;
@FXML private TextField field11;

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

RowList myRowList = new RowList();

MyRowClass aRow =
    new MyRowClass(
        Double.parseDouble(field00.getText()),
        Double.parseDouble(field01.getText()));
myRowList.addRow(aRow);

aRow =
    new Assignment(
        Double.parseDouble(field10.getText()),
        Double.parseDouble(field11.getText()));
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:

---------------------
| field00 | field01 |
| field10 | field11 |
---------------------

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?

@FXML private TextField field00;
@FXML private TextField field01;
@FXML private TextField field10;
@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:

RowList myRowList = new RowList();

MyRowClass aRow =
    new MyRowClass(
        Double.parseDouble(field00.getText()),
        Double.parseDouble(field01.getText()));
myRowList.addRow(aRow);

aRow =
    new Assignment(
        Double.parseDouble(field10.getText()),
        Double.parseDouble(field11.getText()));
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()方法来获取它的所有子节点。然后,您可以像这样循环遍历它们:

HBox h = new HBox();
h.getChildren().add(new TextField());
h.getChildren().add(new TextField());
h.getChildren().add(new TextField());

// 遍历HBox中的每个节点
for (Node n : h.getChildren())
{
    // 如果节点是TextField类型,并且内部文本不为null
    if (n instanceof TextField && ((TextField) n).getText() != null)
    {
        // 做一些操作
    }
}
英文:

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:

HBox h = new HBox();
h.getChildren().add(new TextField());
h.getChildren().add(new TextField());
h.getChildren().add(new TextField());

// loop through each node in the HBox
for (Node n : h.getChildren())
{
    // if the Node is of type TextField, and the text inside is not null
    if (n instanceof TextField && ((TextField) n).getText() != null)
    {
        // do something
    }
}

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:

确定