问题描述
实际上,我想在Javafx中使用标签创建导航列表。我可以为每个标签分配fx:id并在控制器类中创建标签。
Actually, I want to create a navigation list using Labels in Javafx. I can assign fx:id to each label and create labels in controller class.
但我想要做的是,而不是控制器类中的十个Label对象,我想要控制器类中的标签数组,我在场景构建器中创建。
But what I want to do is, instead of ten Label objects in controller class, I want an array of Labels in controller class, which I created in scene builder.
有人可以帮我弄清楚...
Can somebody help me figure out a way...
推荐答案
有些事情在Java中比FXML更好。在这种情况下,我根本不会在FXML中创建标签;只需创建将要保留它们的窗格,然后在控制器中创建标签并将它们添加到窗格中。
Some things are just better done in Java than FXML. I would not create the labels in FXML at all in this scenario; just create the pane that is going to hold them, then create the labels in the controller and add them to the pane.
这就是说,它可以按照你的方式完成正在询问,使用。
That said, it can be done the way you are asking, using <fx:reference>
.
执行以下操作:
<!-- create all the labels as usual -->
<Label fx:id="label1" text="Message 1"/>
<Label fx:id="label2" text="Message 2"/>
<!-- ... -->
<!-- use a define block to define an array list containing the labels: -->
<fx:define>
<ArrayList fx:id="labelList">
<fx:reference source="label1" />
<fx:reference source="label2" />
<!-- ... -->
</ArrayList>
</fx:define>
然后在控制器中注入列表:
Then in the controller just inject the list:
@FXML
private List<Label> labelList ;
完整示例:
LabelListTest.fxml :
LabelListTest.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import java.util.ArrayList?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="LabelListController">
<Label fx:id="label1"/>
<Label fx:id="label2"/>
<Label fx:id="label3"/>
<Label fx:id="label4"/>
<Label fx:id="label5"/>
<Label fx:id="label6"/>
<Label fx:id="label7"/>
<Label fx:id="label8"/>
<Label fx:id="label9"/>
<Label fx:id="label10"/>
<fx:define>
<ArrayList fx:id="labelList" >
<fx:reference source="label1"/>
<fx:reference source="label2"/>
<fx:reference source="label3"/>
<fx:reference source="label4"/>
<fx:reference source="label5"/>
<fx:reference source="label6"/>
<fx:reference source="label7"/>
<fx:reference source="label8"/>
<fx:reference source="label9"/>
<fx:reference source="label10"/>
</ArrayList>
</fx:define>
</VBox>
LabelListController.java:
LabelListController.java:
import java.util.List;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class LabelListController {
@FXML
private List<Label> labelList ;
public void initialize() {
int count = 1 ;
for (Label label : labelList) {
label.setText("Message " + (count++) );
}
}
}
LabelListTest.java:
LabelListTest.java:
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class LabelListTest extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("LabelListTest.fxml"));
Scene scene = new Scene(root, 250, 450);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这篇关于在JavaFX中使用FXML创建Label数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!