-1
我是一名初学java学生。我正在尝试编写一个简单的程序,单击按钮时将显示本地保存的图像。图片不会加载。代码如下所示。任何帮助表示赞赏。请记住我的技能水平。嵌入ImageView事件处理程序>>>不工作
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.scene.control.*;
public class PhotoViewer extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Text txtHeading = new Text("Click to see the picture");
txtHeading.setFont(new Font(20));
HBox paneTop = new HBox(txtHeading);
paneTop.setPadding(new Insets(20, 20, 20, 20));
Button btn1 = new Button("Picture");
btn1.setPrefWidth(80);
btn1.setOnAction(e -> btn1_Click());
HBox paneBottom = new HBox(btn1);
VBox pane = new VBox(paneTop, paneBottom);
pane.setPadding(new Insets(20, 20, 20, 20));
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setTitle("Picture Choice");
primaryStage.show();
}
public void btn1_Click() {
Image img = new Image("file:c:\\users\\sandy\\pictures\\IMG_0127.jpg");
ImageView iview1 = new ImageView(img);
iview1.setFitWidth(300);
iview1.setFitHeight(300);
iview1.setPreserveRatio(true);
BorderPane bpane1 = new BorderPane();
bpane1.setCenter(iview1);
}
}
感谢您的帮助!这似乎是有道理的。但是,我现在得到以下错误:在PhotoViewer.blackda $ 0(PhotoViewer.java:30)PhotoViewer.btn1_Click(PhotoViewer.java:54)线程中的“JavaFX Application Thread”java.lang.NullPointerException异常: – Ron