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

时间:2019-02-20 08:02:27

标签: image javafx

所以我正在制作一个带有按钮和图像的小应用程序。

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));
            }

        }
    });

感谢您的每一个答案。

2 个答案:

答案 0 :(得分:1)

如注释中所建议,下面的代码演示了如何在应用程序中使用BooleanProperty来保持当前的“正在播放媒体”状态:

BooleanProperty isPlaying = new SimpleBooleanProperty();

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

isPlaying.addListener((observable, oldValue, newValue) -> {
    if (newValue) {
        playButton.setGraphic(pauseIcon);
    } else {
        playButton.setGraphic(playIcon);
    }
});

然后让您的Button也切换该BooleanProperty的状态:

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();
    }
}

答案 1 :(得分:0)

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

    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);
        }
    });