【问题标题】:JavaFX imageview setImage returns null and doesn't workJavaFX imageview setImage返回null并且不起作用
【发布时间】:2015-04-12 17:50:46
【问题描述】:

我正在尝试将 imageView img_1 设置为我数据库的第一个图像。由于某种原因它不起作用,我不知道为什么。 loadImage 方法在不同的类中调用。

public class MainMenuController implements Initializable 
{
    /**
     * Initializes the controller class.
    */
    @Override
       public void initialize(URL url, ResourceBundle rb) {
       // TODO
    }

    //DBConnect dbimg = new DBConnect();
    @FXML
    private void openSecondWindow(ActionEvent event) {
    try {
        GUIController ctrl = new GUIController();
        ctrl.loadImg();
        //ctrl.firstScreen();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

我也尝试在单独的方法中设置图像,但我需要在创建整个舞台场景等的同时调用它,即使在从第二种方法调用它之后它也不起作用。

这是持有 setImage 的类

@FXML
private ImageView img_1;

@FXML
private AnchorPane stck1;

ResultSet rs = null;
Statement stmnt = null;
Connection con = null;

String host = "jdbc:derby://localhost:1527/InteractiveGameDatabase;allowMultiQueries=true";
String unm = "Kylar";
String pswrd = "aswzxc";

BufferedImage imgt = null;
InputStream fis = null;
int xcoord;
int ycoord;
int newcoord;

String SQL = "SELECT*FROM location";


public ImageView loadImg() throws IOException {

    try {

        Stage stage = new Stage();
           AnchorPane stck1 = ((AnchorPane) FXMLLoader.load(InteractiveFictionGame2.class.getResource("GUI.fxml")));

        stck1.getChildren().addAll();
        Scene scene = new Scene(stck1);
        stage.setTitle("Interactive Fiction Game");
        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();

        String SQL = "SELECT*FROM location";
        con = DriverManager.getConnection(host, unm, pswrd);
        stmnt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = stmnt.executeQuery(SQL);
        rs.next();
        fis = rs.getBinaryStream(4);
        imgt = javax.imageio.ImageIO.read(fis);
        Image newImg = SwingFXUtils.toFXImage(imgt, null);

设置图像在调用时会给我一个值 null,imageview 的默认值为 null 但我实际上正在加载图像,甚至使用 newImg.isError() 进行检查 - 返回为 "image loaded = true" 。按钮单击的下一个方法调用是再次设置 imageView 并且有效。

        img_1.setImage(newImg)
        rs.close();
        stmnt.close();
        con.close();
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return img_1;
}

这是第二种有效的方法

public ImageView goNorth() 抛出 IOException { 试试 {

        String SQLNorth = "select vista from location where ycoordinate = ? and xcoordinate = ?";
        System.out.println("coords are" + xcoord + ycoord);
        newcoord = ycoord + 1;
        System.out.println("New coord x and y are" + xcoord + newcoord);

        con = DriverManager.getConnection(host, unm, pswrd);
        stmnt2 = con.prepareStatement(SQLNorth);
        stmnt2.setInt(1, newcoord);
        stmnt2.setInt(2, xcoord);
        rs = stmnt2.executeQuery();
        rs.next();
        fis2 = rs.getBinaryStream(1);
        imgt2 = javax.imageio.ImageIO.read(fis2);
        Image newImg = SwingFXUtils.toFXImage(imgt2, null);
        img_1.setImage(newImg);


    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return img_1;
}

我不明白我该如何获取控制器整个类都是控制器?!什么是正确的方法,我只是在看 getClass() 方法,我不明白我是在一个类上还是在整个包上调用 getClass 以及在指定我设置哪个类的路径中控制器?

@FXML
 private void openSecondWindow(ActionEvent event) {
        try {
            FXMLLoader loader;
            loader = new FXMLLoader(GUIController.class.getClass().getResource("GUI.fxml").toExternalForm());
            Parent parent =loader.load();
            GUIController ctrl = loader.getController();
            ctrl.loadImg();
            //ctrl.firstScreen();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

【问题讨论】:

  • 在最后一个示例中,您需要将 parent 设置为新窗口的场景。如果这就是openSecondWindow 方法的作用

标签: image javafx loadimage


【解决方案1】:

问题是GUIController ctrl = new GUIController();

第一种方法的问题是带有@FXML 注释的字段仅在您加载fxml 时才会被实例化。用new 初始化控制器是不行的。

不是初始化控制器,而是从 FXMLLoader 中获取它

FMLLoader loader = new FXMLLoader(getClass().getResource("SOME_PATH").toExternalForm());
Parent parent = loader.load();
GUIController ctrl = loader.getController();

编辑 - 进一步问题的答案

我不明白我是否在一个类上调用 getClass

您不必这样做。你要么使用

getClass().getResource("SOME_PATH")

或者你使用

GUIController.class.getResource("SOME_PATH")

两者都会给你相同的结果。

在指定我将哪个类设置为控制器的路径中

由于您没有在您的问题中添加任何 fxml,我会猜测并说 GUI.fxml 看起来像:

<AnchorPane fx:id="vbox" prefHeight="117.0" prefWidth="285.0" xmlns="http://javafx.com/javafx/8" 
        xmlns:fx="http://javafx.com/fxml/1" fx:controller="package.GUIController">
    <children>
        ...
    </children>
</AnchorPane>

您的控制器是fx:controller="package.GUIController" 中指定的类,loader.getController() 将返回GUIController 类的实例。

【讨论】:

  • 那么这个父母是干什么的?
  • Parent 是场景图中所有具有子节点的基类。您可以将其替换为 fxml 的根布局。
  • @KylarStern 我已经为您的其他问题添加了答案
  • 仍然不太明白它是如何工作的,但确实如此,谢谢老兄。顺便说一句,我是新来的 :)
  • @KylarStern 你读过我的解释了吗?如果您还有其他问题,请在 cmets 中提问。
【解决方案2】:

如果您的目标是用新场景打开新窗口,我认为这将是更合适的方法。首先,制作新的舞台,加载fxml并在MainMenuController中设置场景。这将使事情分开。

@FXML
 private void openSecondWindow(ActionEvent event) {
        try {
            Stage stage = new Stage();
            FXMLLoader loader = new FXMLLoader(getClass().getResource("GUI.fxml"));
            Parent parent = loader.load();
            stage.setTitle("Interactive Fiction Game");      
            stage.setScene(new Scene(parent));
            GUIController ctrl = loader.getController();
            ctrl.loadImg();
            //ctrl.firstScreen();

            stage.show();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

在这段代码中,我看到你在不需要时覆盖了stck1,stck1 已经是初始化变量。

public ImageView loadImg() throws IOException {

    try {


        String SQL = "SELECT*FROM location";
        con = DriverManager.getConnection(host, unm, pswrd);
        stmnt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = stmnt.executeQuery(SQL);
        rs.next();
        fis = rs.getBinaryStream(4);
        imgt = javax.imageio.ImageIO.read(fis);
        Image newImg = SwingFXUtils.toFXImage(imgt, null);

【讨论】:

    猜你喜欢
    • 2020-01-20
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2014-06-25
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多