英文:
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");
//root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
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(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
<?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");
//root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
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
得分: 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论