Javafx 如何将文件路径从一个FileChooser按钮传递到另一个按钮?
我正在尝试使用JavaFx制作一个带有一些图像过滤器的程序,所以我至少需要两个按钮,一个是打开图像的文件选择器,另一个是允许选择过滤器的选择框 我的问题是选择框如何从文件选择器获取路径名或文件对象 以下是我未完成的程序:Javafx 如何将文件路径从一个FileChooser按钮传递到另一个按钮?,javafx,imageview,filechooser,Javafx,Imageview,Filechooser,我正在尝试使用JavaFx制作一个带有一些图像过滤器的程序,所以我至少需要两个按钮,一个是打开图像的文件选择器,另一个是允许选择过滤器的选择框 我的问题是选择框如何从文件选择器获取路径名或文件对象 以下是我未完成的程序: import javafx.application.Application; import javafx.collections.FXCollections; import javafx.scene.control.Button; import javafx.scene.con
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Filter extends Application{
public void start(final Stage stage) {
stage.setTitle("FilterShop");
final FileChooser fileChooser = new FileChooser();
final Button openButton = new Button("Select a photo");
ChoiceBox<String> choiceBox = new ChoiceBox<>();
choiceBox.getItems().add("Choose a Filter");
choiceBox.getItems().addAll("Remove watermark", "Brightness", "Grey", "Mosaic");
choiceBox.getSelectionModel().selectFirst();
final Pane stac = new Pane();
openButton.setOnAction(e -> {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
Image image = new Image(file.toURI().toString());
ImageView imageView = new ImageView(image);
imageView.setX(50);
imageView.setY(50);
imageView.setFitWidth(300);
imageView.setFitHeight(470);
imageView.setPreserveRatio(true);
stac.getChildren().add(imageView);
}
});
choiceBox.setOnAction(event1 -> {
if (choiceBox.getValue() == "Mosaic") {
try {
BufferedImage imagen = ImageIO.read(/* A file object is needed here. */ );
new Mosaic().mosaico(imagen, 80, 80);
} catch (IOException ie) {
System.err.println("I/O Error");
ie.printStackTrace(System.err);
}
}
});
openButton.setLayoutX(300);
openButton.setLayoutY(350);
choiceBox.setLayoutX(430);
choiceBox.setLayoutY(350);
stac.getChildren().addAll(openButton, choiceBox);
stage.setScene(new Scene(stac, 800, 400));
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
我不确定你面临的确切问题是什么。首先,FileChooser不是一个按钮。它是一个帮助类,用于与ToolKit交互以打开操作系统特定的文件选择器并返回结果。显然,它不会记录返回的结果 您有责任保留检索到的文件的引用。这可以通过N种方式实现。当您的问题集中在从“打开”按钮获取值时,我建议使用以下方法
openButton.setOnAction(e -> {
File file = fileChooser.showOpenDialog(stage);
openButton.getProperties().put("FILE_LOCATION", file.getAbsolutePath());
...
});
choiceBox.setOnAction(event1 -> {
if (choiceBox.getValue() == "Mosaic") {
File file = new File(openButton.getProperties().get("FILE_LOCATION").toString());
}
});
我不确定你面临的确切问题是什么。首先,FileChooser不是一个按钮。它是一个帮助类,用于与ToolKit交互以打开操作系统特定的文件选择器并返回结果。显然,它不会记录返回的结果 您有责任保留检索到的文件的引用。这可以通过N种方式实现。当您的问题集中在从“打开”按钮获取值时,我建议使用以下方法
openButton.setOnAction(e -> {
File file = fileChooser.showOpenDialog(stage);
openButton.getProperties().put("FILE_LOCATION", file.getAbsolutePath());
...
});
choiceBox.setOnAction(event1 -> {
if (choiceBox.getValue() == "Mosaic") {
File file = new File(openButton.getProperties().get("FILE_LOCATION").toString());
}
});
如果我遗漏了一些内容,我很抱歉,但是为什么不在openButton.setOnAction事件侦听器之外定义文件对象文件呢。这样,它就不会超出choiceBox.setOnAction事件侦听器的范围,您可以在那里使用它。Java只允许匿名函数引用标记为final的外部变量。如果我遗漏了一些内容,我很抱歉,但是为什么不在openButton.setOnAction事件侦听器之外定义文件对象文件呢。这样它就不会超出choiceBox.setOnAction事件侦听器的范围,您可以在那里使用它。Java只允许匿名函数引用标记为final的外部变量。我尝试了您的建议,效果很好!那太有帮助了,你太好了,非常感谢你很高兴它帮助了你。如果你的问题得到了回答/解决,你能相应地做标记吗。谢谢我试过你的建议,效果很好!那太有帮助了,你太好了,非常感谢你很高兴它帮助了你。如果你的问题得到了回答/解决,你能相应地做标记吗。谢谢