現在、クライアント側のアニメストリーマーを作成しており、現在JavaFXを学習しています。静的サムネイルを作成しましたが、静的コンテンツの値を変更する必要があります。それでは、JavaFXの.fxmlファイルですでに定義されているテキスト要素の値をどのように変更しますか?
参考までに、コードは次のとおりです。
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 {
}
最初id
に、関連するタグに属性を割り当てる必要があります。たとえばAnchorPane
、上記の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 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
、変数名としてを使用して、コントローラーから参照できます。アノテーションAnchorPane
を使用して、インスタンス変数としてを追加するだけ@FXML
です。
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;
import javafx.fxml.FXML;
public class Controller {
@FXML
private AnchorPane anchorPane;
/*Rest of class*/
}
これはNode
、Label
s を含むFXMLファイル内の任意のもので実行できます。id
属性を追加することを忘れないでください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加