我正在尝试img_1
使用我的数据库的第一张图像设置 imageView。由于某种原因它不起作用,我不知道为什么。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;
}
这是第二种有效的方法
公共 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());
}
}