Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何在javafx中从图像库中选择图像?_Image_Select_Javafx_Gallery - Fatal编程技术网

Image 如何在javafx中从图像库中选择图像?

Image 如何在javafx中从图像库中选择图像?,image,select,javafx,gallery,Image,Select,Javafx,Gallery,我正在制作一个图像库。我正在使用JavaFX8以及用于GUI的场景生成器。我希望用户可以选择从图库中选择图像:我希望图库中有可单击和可选择的图像。我还想记下这些选定图像的顺序,即先选定的图像和最后选定的图像。我已经创建了图库,但我想知道我会怎么做才能使图像可点击并选中图像,并记录它们的顺序?我不想对图像使用复选框。您可以在每个ImageView上添加setOnMouseClicked处理程序 下面是我的意思的代码示例: public class ClickableImage extends Ap

我正在制作一个图像库。我正在使用JavaFX8以及用于GUI的场景生成器。我希望用户可以选择从图库中选择图像:我希望图库中有可单击和可选择的图像。我还想记下这些选定图像的顺序,即先选定的图像和最后选定的图像。我已经创建了图库,但我想知道我会怎么做才能使图像可点击并选中图像,并记录它们的顺序?我不想对图像使用复选框。

您可以在每个ImageView上添加setOnMouseClicked处理程序

下面是我的意思的代码示例:

public class ClickableImage extends Application {

    private ArrayList<String> clickedImages = new ArrayList<>();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {     
        String imgUrl = getClass().getResource("image.png").toExternalForm();
        assert imgUrl != null;
        Image img = new Image(imgUrl);
        assert img != null;

        BorderPane root = new BorderPane();
        ImageView imgView = new ImageView(img);
        imgView.setUserData(imgUrl);
        root.setCenter(imgView);

        Scene scene = new Scene(root, 600, 400);

        primaryStage.setScene(scene);
        primaryStage.setTitle("ClickableImage");
        primaryStage.show();

        //-------------

        imgView.setOnMouseClicked(e -> {
            String clickedImgUrl = (String)((ImageView)e.getSource()).getUserData();
            System.out.println("Image was clicked: " + clickedImgUrl);
            clickedImages.add(clickedImgUrl);
        });                
    }
}
公共类ClickableImage扩展应用程序{
private ArrayList ClickeImage=新建ArrayList();
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段初始阶段){
字符串imgUrl=getClass().getResource(“image.png”).toExternalForm();
断言imgUrl!=null;
图像img=新图像(imgUrl);
断言img!=null;
BorderPane根=新的BorderPane();
ImageView imgView=新的ImageView(img);
imgView.setUserData(imgUrl);
root.setCenter(imgView);
场景=新场景(root,600400);
初级阶段。场景(场景);
primaryStage.setTitle(“可点击图像”);
primaryStage.show();
//-------------
imgView.setOnMouseClicked(e->{
字符串clickedImgUrl=(字符串)((图像视图)e.getSource()).getUserData();
System.out.println(“单击图像:”+单击edimgurl);
单击edimages.add(单击edimgurl);
});                
}
}

您可以在每个ImageView上添加setOnMouseClicked处理程序

下面是我的意思的代码示例:

public class ClickableImage extends Application {

    private ArrayList<String> clickedImages = new ArrayList<>();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {     
        String imgUrl = getClass().getResource("image.png").toExternalForm();
        assert imgUrl != null;
        Image img = new Image(imgUrl);
        assert img != null;

        BorderPane root = new BorderPane();
        ImageView imgView = new ImageView(img);
        imgView.setUserData(imgUrl);
        root.setCenter(imgView);

        Scene scene = new Scene(root, 600, 400);

        primaryStage.setScene(scene);
        primaryStage.setTitle("ClickableImage");
        primaryStage.show();

        //-------------

        imgView.setOnMouseClicked(e -> {
            String clickedImgUrl = (String)((ImageView)e.getSource()).getUserData();
            System.out.println("Image was clicked: " + clickedImgUrl);
            clickedImages.add(clickedImgUrl);
        });                
    }
}
公共类ClickableImage扩展应用程序{
private ArrayList ClickeImage=新建ArrayList();
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段初始阶段){
字符串imgUrl=getClass().getResource(“image.png”).toExternalForm();
断言imgUrl!=null;
图像img=新图像(imgUrl);
断言img!=null;
BorderPane根=新的BorderPane();
ImageView imgView=新的ImageView(img);
imgView.setUserData(imgUrl);
root.setCenter(imgView);
场景=新场景(root,600400);
初级阶段。场景(场景);
primaryStage.setTitle(“可点击图像”);
primaryStage.show();
//-------------
imgView.setOnMouseClicked(e->{
字符串clickedImgUrl=(字符串)((图像视图)e.getSource()).getUserData();
System.out.println(“单击图像:”+单击edimgurl);
单击edimages.add(单击edimgurl);
});                
}
}

正如我所说,我还想记下单击图像的顺序,因此如何在使用setOnMouseClicked时获得单击的imageView,因为imageView与ActionEvent不兼容。我必须承认,我不明白您的问题是什么。将处理程序附加到所有图像视图后,每次单击图像时都会调用此处理程序。现在,您可以做的最简单的事情是从事件对象获取源(ImageView),从中可以获取包含的图像,然后可以将其存储在ArrayList中。然后,此列表的内容描述了单击的顺序。这是你想要的还是我误解了你?是的,你完全正确,但setOnMouseClicked不返回源代码。在给定的代码中,extendedImage是我的类,它扩展了image类并添加了一个额外的image id函数。给定的代码给出了错误,因为我无法将ActionEven image作为参数传递,并且无法获取源,,,,,,public void setValue(ActionEvent image){单击++;ImageView单击EdImageView=(ImageView)image.getSource();extendedImage clickedImage=(extendedImage)clickedImage view.getImage();int-imageId=clickedImage.getId();clickedImageId.add(imageId);}我还想告诉你,我正在使用sceneBuilder并在图像视图的代码选项中注册事件集OnMouseClicked here。我已经知道了。。你的回答是可以的。但我在scenebuilder中使用了35个ImageView,正如我在原始问题中所说的,这是一个图像库。我必须在控制器中创建35个FXML图像视图,并将每个视图与scenebuilder中的每个视图相关联。我要写这个setOnMouseClicked函数35次吗?正如我说的,我还想记下点击图像的顺序,所以当使用setOnMouseClicked时,我怎么能得到点击图像视图,因为图像视图与操作不兼容。即使我不得不承认我不明白你的问题是什么。将处理程序附加到所有图像视图后,每次单击图像时都会调用此处理程序。现在,您可以做的最简单的事情是从事件对象获取源(ImageView),从中可以获取包含的图像,然后可以将其存储在ArrayList中。然后,此列表的内容描述了单击的顺序。这是你想要的还是我误解了你?是的,你完全正确,但setOnMouseClicked不返回源代码。在给定的代码中,extendedImage是我的类,它扩展了image类并添加了一个额外的image id函数。给定的代码给出了错误,因为我无法将ActionEven image作为参数传递,并且无法获取源,,,,,,public void setValue(ActionEvent image){单击++;ImageView单击EdImageView=(ImageView)image.getSource();extendedImage clickedImage=(extendedImage)clickedImage View.getImage();int-imageId=clickedImage.getId();clickedImageId.add(imageId);}我还想告诉您我正在使用sceneBuilder并在代码op中注册事件集MouseClicked