首页 > 解决方案 > 检查按钮图形是否等于图像

问题描述

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

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

        }
    });

感谢您的每一个回答。

标签: imagejavafx

解决方案


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

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

推荐阅读