image - 检查按钮图形是否等于图像
问题描述
所以我正在制作一个带有按钮和图像的小应用程序。
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));
}
}
});
感谢您的每一个回答。
解决方案
正如评论中所建议的那样,下面的代码演示了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();
}
}
推荐阅读
- javascript - 从父节点更新孩子的道具
- python - Keras/Tensorflow:在同一个 GPU 上循环或使用 Process 训练多个模型
- powershell - 在 Windows 任务计划程序上运行 PowerShell 的问题
- php - 从 CakePHP Shell 通过 sendgrid 发送电子邮件返回响应代码 0
- python - Python需要一个类似字节的对象,而不是'str'错误
- php - Atom Beauty 找不到 PHP
- time - 日期时间颤动
- mongodb - 使用 MongoDB 命令时出错
- json - 尝试使用 jq 从 aws-cli 响应中提取值但失败了,我做错了什么?
- google-cloud-firestore - Firestore 安全规则 - 允许公共创建访问