如何在JavaFX中更改已在.fxml文件中定义的文本元素的值?

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

How do you change the value of a text element already defined in a .fxml file in JavaFX?

问题

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane prefHeight="387.0" prefWidth="243.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.gui.Controller">
   <children>
      <ImageView fitHeight="266.0" fitWidth="184.0" layoutX="36.0" layoutY="26.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../images/mL0215_1024x1024.png" />
         </image>
      </ImageView>
      <Label layoutX="36.0" layoutY="308.0" style="-fx-background-color: #000000;" text="My Hero Acadamia" textFill="WHITE">
         <font>
            <Font name="System Bold" size="17.0" />
         </font>
      </Label>
   </children>
</AnchorPane>
@Override
public void start(Stage primaryStage) throws Exception {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/videoThumbnail.fxml"));
        Pane root = (Pane) loader.load();
        Scene scene = new Scene(new Group(root));

        primaryStage.setTitle("test");
        primaryStage.setScene(scene);
        primaryStage.show();

        letterbox(scene, root);
        primaryStage.setFullScreen(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}
package test.gui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;

public class Controller {
}
英文:

Currently, I'm making a client-side anime streamer, and currently learning JavaFX. I've created a static thumbnail, and I need to change the value of the static content. So, how do you change the value of a text element already defined in a .fxml file in JavaFX?

For reference, here's the code.

videoThumbnail.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane prefHeight="387.0" prefWidth="243.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.gui.Controller">
   <children>
      <ImageView fitHeight="266.0" fitWidth="184.0" layoutX="36.0" layoutY="26.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../images/mL0215_1024x1024.png" />
         </image>
      </ImageView>
      <Label layoutX="36.0" layoutY="308.0" style="-fx-background-color: #000000;" text="My Hero Acadamia" textFill="WHITE">
         <font>
            <Font name="System Bold" size="17.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

main.java

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/videoThumbnail.fxml"));
            Pane root = (Pane) loader.load();
            Scene scene = new Scene(new Group(root));

            primaryStage.setTitle("test");
            primaryStage.setScene(scene);
            primaryStage.show();

            letterbox(scene, root);
            primaryStage.setFullScreen(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

Controller.java

package test.gui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;

public class Controller {
}

答案1

得分: 4

首先,您需要为相关标签分配一个 id 属性。例如,如果您想引用上述 FXML 文件中的 AnchorPane,您可以使用以下代码:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane fx:id="anchorPane" prefHeight="387.0" prefWidth="243.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.gui.Controller">
   ...
</AnchorPane>

请注意 fx:id="anchorPane" 的添加。那就是 id 属性。

现在,您可以在控制器中使用 id 作为变量名来引用它。只需使用 @FXML 注释将 AnchorPane 添加为实例变量。

package test.gui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

public class Controller {
    @FXML
    private AnchorPane anchorPane;
    /*Rest of class*/

}

您可以在 FXML 文件中的任何对象上执行此操作,包括 Label。只需记得添加 id 属性。

英文:

First you need to assign an id attribute to the relevant tag. For example, if you wanted to reference the AnchorPane in the above FXML file, you would use this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import javafx.scene.control.*?&gt;
&lt;?import javafx.scene.image.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;
&lt;?import javafx.scene.text.*?&gt;

&lt;AnchorPane fx:id=&quot;anchorPane&quot; prefHeight=&quot;387.0&quot; prefWidth=&quot;243.0&quot; style=&quot;-fx-background-color: #000000;&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;test.gui.Controller&quot;&gt;
   ...
&lt;/AnchorPane&gt;

Note the addition of fx:id=&quot;anchorPane&quot;. That is the id attribute.

Now, you can reference it from the Controller by using the id as the variable name. Just add the AnchorPane as an instance variable using the @FXML annotation.

package test.gui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

public class Controller {
    @FXML
    private AnchorPane anchorPane;
    /*Rest of class*/

}

You can do this with any object in the FXML file, including the Labels. Just remember to add the id attribute.

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

发表评论

匿名网友

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

确定