我在寻找一种在“悬停”时设置按钮文本的方法。
Logic:
if (button.isHover()){
button.setText("blabla");
}不管是css解决方案还是代码解决方案都可以!
发布于 2015-11-15 12:04:58
可以使用绑定,也可以将侦听器添加到按钮的hoverProperty()中。
绑定
button.textProperty().bind(
Bindings.when(button.hoverProperty())
.then("Hovered")
.otherwise("Not Hovered"));侦听器
button.hoverProperty().addListener((ov, oldValue, newValue) -> {
if (newValue) {
button.setText("Hovered");
} else {
button.setText("Not Hovered");
}
});MCVE
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public void start(Stage stage) {
VBox pane = new VBox();
pane.setAlignment(Pos.CENTER);
Button button = new Button();
button.textProperty().bind(
Bindings.when(button.hoverProperty())
.then("Hovered")
.otherwise("Not Hovered"));
pane.getChildren().addAll(button);
Scene scene = new Scene(pane, 200, 200);
stage.setScene(scene);
stage.show();
}
}https://stackoverflow.com/questions/33719352
复制相似问题