我在java fx中显示图像有问题。我想简单地显示我从字符串路径获取的图像。在这种情况下我的图像images.jpg在项目的src文件夹中。 到目前为止,我有这样的代码: FXML文件:
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com /javafx/8">
<children>
<ImageView fx:id="imageView" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
控制器类:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class MainPaneController implements Initializable {
@FXML
private ImageView imageView;
private Image image;
String path;
@Override
public void initialize(URL location, ResourceBundle resources) {
String path = "indeks.jpg";
imageView = new ImageView();
image = new Image(path);
imageView.setImage(image);
}
public ImageView getImageView() {
return imageView;
}
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
}
主要课程:
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent parent = (Parent) FXMLLoader.load(getClass().getResource(
"/pl/gallery/view/MainPane.fxml"));
Scene scene = new Scene(parent);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我不知道我在这里做错了什么。