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

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

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

问题

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <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">
  7. <children>
  8. <ImageView fitHeight="266.0" fitWidth="184.0" layoutX="36.0" layoutY="26.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="@../images/mL0215_1024x1024.png" />
  11. </image>
  12. </ImageView>
  13. <Label layoutX="36.0" layoutY="308.0" style="-fx-background-color: #000000;" text="My Hero Acadamia" textFill="WHITE">
  14. <font>
  15. <Font name="System Bold" size="17.0" />
  16. </font>
  17. </Label>
  18. </children>
  19. </AnchorPane>
  1. @Override
  2. public void start(Stage primaryStage) throws Exception {
  3. try {
  4. FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/videoThumbnail.fxml"));
  5. Pane root = (Pane) loader.load();
  6. Scene scene = new Scene(new Group(root));
  7. primaryStage.setTitle("test");
  8. //root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  9. primaryStage.setScene(scene);
  10. primaryStage.show();
  11. letterbox(scene, root);
  12. primaryStage.setFullScreen(true);
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. public static void main(String[] args) {
  18. launch(args);
  19. }
  1. package test.gui;
  2. import javafx.event.ActionEvent;
  3. import javafx.fxml.FXML;
  4. import javafx.scene.control.Button;
  5. import javafx.scene.input.MouseEvent;
  6. public class Controller {
  7. }
英文:

Currently, I'm making a client-side anime streamer(pls no bully), 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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import javafx.scene.image.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.text.*?>
  6. <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">
  7. <children>
  8. <ImageView fitHeight="266.0" fitWidth="184.0" layoutX="36.0" layoutY="26.0" pickOnBounds="true" preserveRatio="true">
  9. <image>
  10. <Image url="@../images/mL0215_1024x1024.png" />
  11. </image>
  12. </ImageView>
  13. <Label layoutX="36.0" layoutY="308.0" style="-fx-background-color: #000000;" text="My Hero Acadamia" textFill="WHITE">
  14. <font>
  15. <Font name="System Bold" size="17.0" />
  16. </font>
  17. </Label>
  18. </children>
  19. </AnchorPane>

main.java

  1. @Override
  2. public void start(Stage primaryStage) throws Exception {
  3. try {
  4. FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/videoThumbnail.fxml"));
  5. Pane root = (Pane) loader.load();
  6. Scene scene = new Scene(new Group(root));
  7. primaryStage.setTitle("test");
  8. //root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  9. primaryStage.setScene(scene);
  10. primaryStage.show();
  11. letterbox(scene, root);
  12. primaryStage.setFullScreen(true);
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. public static void main(String[] args) {
  18. launch(args);
  19. }

Controller.java

  1. package test.gui;
  2. import javafx.event.ActionEvent;
  3. import javafx.fxml.FXML;
  4. import javafx.scene.control.Button;
  5. import javafx.scene.input.MouseEvent;
  6. public class Controller {
  7. }

答案1

得分: 1

正常的步骤是在FXML中定义UI的静态部分(例如,在Pane上放置一个ImageView),然后在代码中添加动态部分(例如,更改ImageView内的图像)。这就是控制器的用途。

英文:

The normal procedure would be to define the static part of your UI in FXML (e.g. place an ImageView onto a Pane) and then add the dynamic part (e.g. changing the image inside the ImageView) in your code. That's what the Controller is meant for.

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

发表评论

匿名网友

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

确定