我是 JavaFX 的初学者。我正在尝试创建自己的 Button 子类,该子类将具有用于鼠标进入和鼠标退出的动画。我试图实现的动画是一个简单的“变暗”或“变暗”过渡,当用户将鼠标悬停在按钮上时,按钮背景的颜色会变暗,并在鼠标退出按钮时动画回到正常状态。
首先,我认为我可以使用 FillTransition 来实现此目的,但为此我需要按钮的特定深色,这取决于按钮颜色。 所以现在我试图基本上淡入和淡出按钮顶部的低不透明度黑色矩形,但该矩形似乎根本没有出现。
这是我的按钮的代码:
public class FlatButton extends Button {
private Rectangle dimRectangle;
private Duration dimDuration = Duration.millis(250);
private Color dimColor = new Color(0,0,0,0.11);
public FlatButton(String text) {
super(text);
getStyleClass().addAll("flat-button-style");
createEffect();
}
private void createEffect()
{
dimRectangle = new Rectangle(this.getWidth(), this.getHeight(), dimColor);
dimRectangle.setOpacity(1.0);
dimRectangle.setX(this.get);
FadeTransition enterTransition = new FadeTransition(dimDuration, this);
enterTransition.setInterpolator(Interpolator.EASE_OUT);
enterTransition.setFromValue(0.0);
enterTransition.setToValue(1.0);
FadeTransition exitTransition = new FadeTransition(dimDuration, this);
exitTransition.setInterpolator(Interpolator.EASE_OUT);
exitTransition.setFromValue(1.0);
exitTransition.setToValue(0.0);
this.setOnMouseEntered(new EventHandler<MouseEvent>(){
public void handle(MouseEvent mouseEvent){
enterTransition.play();
}
});
this.setOnMouseExited(new EventHandler<MouseEvent>(){
public void handle(MouseEvent mouseEvent){
exitTransition.play();
}
});
}
}
编辑:代码“new FadeTransition(dimDuration, this);”中的部分应该是“new FadeTransition(dimDuration, dimRectangle);”。这只是我正在测试的东西。
编辑2:我认为“dimRectangle = new Rectangle(this.getWidth(), this.getHeight(), dimColor);”并没有真正起作用,但我还没有找到如何使矩形填充按钮尺寸的方法。
最佳答案
您可以使用ColorAdjust效果并改变它的 brightness使用 Timeline 的属性.
public class ButtonFadeDemo extends Application {
@Override
public void start(Stage primaryStage) {
try {
Pane root = new Pane();
Button button = new Button("Click me!");
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(0.0);
button.setEffect(colorAdjust);
button.setOnMouseEntered(e -> {
Timeline fadeInTimeline = new Timeline(
new KeyFrame(Duration.seconds(0),
new KeyValue(colorAdjust.brightnessProperty(), colorAdjust.brightnessProperty().getValue(), Interpolator.LINEAR)),
new KeyFrame(Duration.seconds(1), new KeyValue(colorAdjust.brightnessProperty(), -1, Interpolator.LINEAR)
));
fadeInTimeline.setCycleCount(1);
fadeInTimeline.setAutoReverse(false);
fadeInTimeline.play();
});
button.setOnMouseExited(e -> {
Timeline fadeOutTimeline = new Timeline(
new KeyFrame(Duration.seconds(0),
new KeyValue(colorAdjust.brightnessProperty(), colorAdjust.brightnessProperty().getValue(), Interpolator.LINEAR)),
new KeyFrame(Duration.seconds(1), new KeyValue(colorAdjust.brightnessProperty(), 0, Interpolator.LINEAR)
));
fadeOutTimeline.setCycleCount(1);
fadeOutTimeline.setAutoReverse(false);
fadeOutTimeline.play();
});
root.getChildren().addAll(button);
Scene scene = new Scene(root, 800, 400);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
关于button - JavaFX Transition - 悬停时变暗按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29879023/