Image 检查按钮图形是否等于图像

Image 检查按钮图形是否等于图像,image,javafx,Image,Javafx,所以我制作了一个小应用程序,里面有一个按钮和一个图像 btnPlay.setGraphic(new ImageView(imagePlay)); 现在我有了一个ActionEvent来切换图像,如果按钮被点击(就像Spotify中的暂停按钮)。问题是,我没有任何线索,在互联网上也找不到我如何用if-else语句处理这个问题。我试过的都不管用 btnPlay.setOnAction(new EventHandler<ActionEvent>() {

所以我制作了一个小应用程序,里面有一个按钮和一个图像

btnPlay.setGraphic(new ImageView(imagePlay));
现在我有了一个ActionEvent来切换图像,如果按钮被点击(就像Spotify中的暂停按钮)。问题是,我没有任何线索,在互联网上也找不到我如何用if-else语句处理这个问题。我试过的都不管用

        btnPlay.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {

            Image imagePause = new Image(path);


            if (btnPlay.getGraphic().equals(imagePause)) {
                btnPlay.setGraphic(new ImageView(imagePlay));
            }

        }
    });
btnPlay.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
图像暂停=新图像(路径);
if(btnPlay.getGraphic().equals(imagePause)){
btnPlay.setGraphic(新图像视图(图像播放));
}
}
});

谢谢你的回答

所以我没有找到一种方法来检查图像是否相等,但是我找到了一种方法来解决这个问题。这是我的解决方案:

    ImageView imgPlay = new ImageView(new Image(path + "play.png"));
    grid.add(imgPlay, 1, 0);

    ImageView imgPause = new ImageView(new Image(path + "pause.png"));

    imgPause.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            grid.getChildren().remove(imgPause);
            grid.add(imgPlay, 1, 0);
        }
    });

    imgPlay.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            grid.getChildren().remove(imgPlay);
            grid.add(imgPause, 1, 0);
        }
    });
ImageView imgPlay=newimageview(新图像(路径+“play.png”);
grid.add(imgPlay,1,0);
ImageView imgPause=新图像视图(新图像(路径+“pause.png”);
imgPause.addEventHandler(MouseEvent.MOUSE_单击,新建EventHandler()){
@凌驾
公共无效句柄(MouseeEvent事件){
grid.getChildren().remove(imgPause);
grid.add(imgPlay,1,0);
}
});
imgPlay.addEventHandler(MouseEvent.MOUSE_单击,新建EventHandler()){
@凌驾
公共无效句柄(MouseeEvent事件){
grid.getChildren().remove(imgPlay);
grid.add(imgPause,1,0);
}
});

正如注释中所建议的,下面的代码演示了在应用程序中使用一个保持当前“正在播放媒体”状态的
布尔属性

通过向该属性添加侦听器,可以确保“播放”按钮上始终显示正确的图标:

isPlaying.addListener((observable, oldValue, newValue) -> {
    if (newValue) {
        playButton.setGraphic(pauseIcon);
    } else {
        playButton.setGraphic(playIcon);
    }
});
然后让您的
按钮
也切换该
布尔属性的状态

playButton.setOnAction(event -> {
    // Toggle the isPlaying property
    isPlaying.set(!isPlaying.get());
});

下面是一个完整的示例应用程序,您可以自己运行来测试它:

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class PlayButtonToggleSample extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        // Simple Interface
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));

        // Create our two ImageViews to hold the icons we need
        ImageView playIcon = new ImageView("play.png");
        ImageView pauseIcon = new ImageView("pause.png");

        // BooleanProperty to track whether or not the application is currently playing media
        BooleanProperty isPlaying = new SimpleBooleanProperty();

        // Create a simple button with an image
        Button playButton = new Button();
        playButton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        playButton.setGraphic(playIcon);

        // Add action for the button
        playButton.setOnAction(event -> {
            // Toggle the isPlaying property
            isPlaying.set(!isPlaying.get());
        });

        // Now, add a listener to the isPlaying property that will toggle the icon used on the playButton
        isPlaying.addListener((observable, oldValue, newValue) -> {
            if (newValue) {
                playButton.setGraphic(pauseIcon);
            } else {
                playButton.setGraphic(playIcon);
            }
        });

        root.getChildren().add(playButton);

        // Show the stage
        primaryStage.setScene(new Scene(root));
        primaryStage.setTitle("Sample");
        primaryStage.show();
    }
}

基本问题不是图像,而是缺少状态的建模:f.i.播放一个booleanProperty,可以切换开/关,用该属性注册一个更改侦听器,并在该侦听器中更新按钮的图形(或ImageView的图像)-旁白:没有理由在每次状态更改时重新创建图像/视图,加载一次并重新使用。使用
图像
还允许您使用
==
来比较
图像
,以检查参考相等性。如果你出于某种原因想避免这种情况,仍然有一种方法。我的建议是使用
ToggleButton
并基于
ToggleButton
选定的
属性绑定
ImageView
image
属性。请参阅我对问题的评论-我肯定您想修改您的解决方案:)绝对不是一个好的解决方案,泊松。看看克利奥帕特拉的评论。@Zephyr是的,我看到了解决方案,但我真的不明白(或者可能知道)。我正在努力:-)
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class PlayButtonToggleSample extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        // Simple Interface
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));

        // Create our two ImageViews to hold the icons we need
        ImageView playIcon = new ImageView("play.png");
        ImageView pauseIcon = new ImageView("pause.png");

        // BooleanProperty to track whether or not the application is currently playing media
        BooleanProperty isPlaying = new SimpleBooleanProperty();

        // Create a simple button with an image
        Button playButton = new Button();
        playButton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        playButton.setGraphic(playIcon);

        // Add action for the button
        playButton.setOnAction(event -> {
            // Toggle the isPlaying property
            isPlaying.set(!isPlaying.get());
        });

        // Now, add a listener to the isPlaying property that will toggle the icon used on the playButton
        isPlaying.addListener((observable, oldValue, newValue) -> {
            if (newValue) {
                playButton.setGraphic(pauseIcon);
            } else {
                playButton.setGraphic(playIcon);
            }
        });

        root.getChildren().add(playButton);

        // Show the stage
        primaryStage.setScene(new Scene(root));
        primaryStage.setTitle("Sample");
        primaryStage.show();
    }
}