如何在 JavaFX FXML 的窗格中编辑文本/Image/Button

How to edit text / Image/Button in a pane in JavaFX FXML

关于如何将 panesfrom 添加到 JavaFX FXML 中的 VBox。我现在的问题是如何编辑我添加的窗格内的内容? FXML 文件的代码仍然相同。新编辑的Java代码如下:

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

public class HotelReservationController implements Initializable{
@FXML
    private ScrollPane scrollPaneContent;
    @FXML
    private VBox vboxData;
    @FXML
    private AnchorPane cardAnchor;
    @FXML
    private HBox cardHBox;
    @FXML
    private Text cardTitle;

    @Override
        public void initialize(URL arg0, ResourceBundle arg1) {
            URL cardURL = getClass().getResource("/application/Cards.fxml");
//      vboxData.getChildren().add(cardAnchor);
        for (int j = 0; j < 10; j++) {
            Parent cardAnchor=null;
            Parent cardHBox=null;
            Parent cardTitle=null;
            try {
                cardAnchor = FXMLLoader.load(cardURL);
                cardHBox= FXMLLoader.load(cardURL);
                cardTitle = FXMLLoader.load(cardURL);
                cardTitle.setText("test" + j);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            vboxData.getChildren().add(cardAnchor);
        }
        
        try {
            connectToHotel();
        } catch (ClassNotFoundException | SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
   
            
        }

但是,当我这样做时出现以下错误:

javafx.fxml.LoadException: 
/C:/Users///////bin/application/HotelReservation.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.Main.start(Main.java:18)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null7(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.text.Text
    at application.HotelReservationController.initialize(HotelReservationController.java:55)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more

如何添加此文本字段和其他文本字段(我想处理图像窗格和按钮与处理它们类似)?

所以在研究了这个之后,考虑到@James_D 的回答,我所要做的就是为 Cards.fxml 创建一个控制器,然后在那个控制器中我应该添加数据。

我的是

public class CardsController implements Initializable{
@FXML
    private Text cardTitle;
    @FXML
    private Text cardLocation;
    @FXML
    private Text cardRating;
    @FXML
    private Text cardDescription;
    @FXML
    private Button cardDetails;
    @FXML
    private ImageView cardPhoto;
    @FXML
    private HBox cardHBox;
    @FXML
    private AnchorPane cardAnchor;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        cardTitle.setText("Test Title");
        cardLocation.setText(" Test Location" );
        cardRating.setText("5");
        cardDescription.setText("This is only for testing, data should be imported later");
    }
}

现在除了控制器,在 HotelReservationController class 中,特别是在我让它显示所有卡片的 for 循环中,我应该做以下事情:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Cards.fxml"));
                loader.load();

我的问题是 loader.load()。除非我加载加载器,否则 initialize 方法不会被调用,这就是导致我出现很多问题的原因。

希望这对以后像我一样无知的人有所帮助。