我无法使用网址或文件追踪器显示图片。我已经实现了从url或pc中提取文件的方法,但不知道如何显示这些图像。
以下是我从网址中提取的方法:
@FXML
private void button0Action (ActionEvent event) throws IOException {
catdog c = new catdog();
String cat = c.cat();
System.out.println(cat);
输出对话框返回url: http://24.media.tumblr.com/tumblr_llmbvhAcsF1qg20muo1_500.gif
这是我的Display类:
package screensaver;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Display extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
Scene scene = new Scene(pane);
primaryStage.setTitle("imageViewer");
primaryStage.setScene(scene);
primaryStage.show();
}
}
创建了Display和ImageView的新实例:
@FXML
private void button0Action (ActionEvent event) throws IOException {
catdog c = new catdog();
String cat = c.cat();
System.out.println(cat);
Display catDisplay = new Display();
ImageView catImage = new ImageView(cat);
}
如何将图像添加到窗格并调用start方法以显示场景?我知道我可以在Display类中创建一个ImageView实例,然后将一个图像添加到窗格中,虽然我无法从button0Action方法中提取包含url的变量cat。目前困住,不知道从哪里开始。过去几天一直在努力尝试不同的方法,没有运气。有什么想法吗?
以下是我目前的更改:
展示班级:
package screensaver;
import java.io.IOException;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Display {
public void showImage0 (String url) throws IOException {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
Catdog c = new Catdog();
pane.getChildren().add(new ImageView(c.cat()));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
public void showImage1 (String url) throws IOException {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
Catdog d = new Catdog();
pane.getChildren().add(new ImageView(d.dog()));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
public void showImage2 (String url) throws IOException {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
LandscapeImage lI = new LandscapeImage();
pane.getChildren().add(new ImageView(lI.imageSource));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
public void showImage3 (String file) throws IOException {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
FileChooser fc3 = new FileChooser();
pane.getChildren().add(new ImageView(fc3.fileChoice()));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
}
此处出现错误:' void'这里不允许输入
pane.getChildren().add(new ImageView(fc3.fileChoice()));
这是FileChooser类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package screensaver;
import java.io.File;
import java.io.IOException;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
/**
*
* @author Jacob
*/
public class FileChooser {
void fileChoice () throws IOException{
javafx.stage.FileChooser fc3 = new javafx.stage.FileChooser();
File selectedFile = fc3.showOpenDialog(null);
fc3.getExtensionFilters().addAll(
new javafx.stage.FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
if (selectedFile != null) {
String location = (selectedFile.getAbsoluteFile().toURI().toString());
System.out.println(location);
Display ownDisplay = new Display();
ownDisplay.showImage3(location);
} else {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION,"No file was selected");
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent((ButtonType response) -> {
System.out.println("OK");
});
}
}
}
最新代码:
显示方法:
public void showImage3 (String file) throws IOException, Exception {
Stage stage = new Stage();
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(0);
pane.setVgap(0);
pane.getChildren().add(new ImageView(file));
Scene scene = new Scene(pane);
stage.setTitle("imageViewer");
stage.setScene(scene);
stage.show();
}
button3Action方法:
@FXML
private void button3Action(ActionEvent event) throws IOException, Exception {
FileChooser fc3 = new FileChooser();
File selectedFile = fc3.showOpenDialog(null);
fc3.getExtensionFilters().addAll(
new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
if (selectedFile != null) {
String location = (selectedFile.getAbsoluteFile().toURI().toString());
System.out.println(location);
Display fileChoice = new Display();
fileChoice.showImage3(location);
} else {
Alert alert = new Alert(AlertType.CONFIRMATION,"No file was selected");
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent((ButtonType response) -> {
System.out.println("OK");
});
}
}
答案 0 :(得分:1)
编辑2:
因为我不能再跟着你了,
试着理解这个例子(它完全正常工作)
这应该有助于解决您的问题:
01000001