如何在JavaFXML中编辑窗格中的文本/图像/按钮

mzillmmw  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(391)

我之前有一个关于如何在javafxfxml中将panesfrom添加到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$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$null$147(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

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

zu0ti5jz

zu0ti5jz1#

所以在研究这个问题之后,考虑到@james\u d answer,我所要做的就是为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 类,特别是在我让它显示所有卡片的for循环中,我应该执行以下操作:

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

我的问题是loader.load()。这个 initialize 方法不会被调用,除非我加载加载程序,而这正是引起我许多问题的原因。
希望这能在将来帮助像我这样无知的人。

相关问题