Button JavaFX过渡-悬停时变暗按钮
我是JavaFX的初学者。我正在尝试创建我自己的按钮子类,它将在鼠标进入和鼠标退出的动画中显示。我试图实现的动画是一个简单的“变暗”或“变暗”过渡,当用户将鼠标悬停在按钮上时,按钮背景的颜色会变暗,当鼠标退出按钮时,动画会恢复到正常状态 首先,我认为我可以通过FillTransition实现这一点,但为此,我需要按钮的特定深色,这取决于按钮的颜色。 所以现在我试着在按钮顶部淡入淡出一个低透明度的黑色矩形,但是这个矩形看起来根本没有出现 这是我的按钮的代码:Button JavaFX过渡-悬停时变暗按钮,button,javafx,hover,transitions,Button,Javafx,Hover,Transitions,我是JavaFX的初学者。我正在尝试创建我自己的按钮子类,它将在鼠标进入和鼠标退出的动画中显示。我试图实现的动画是一个简单的“变暗”或“变暗”过渡,当用户将鼠标悬停在按钮上时,按钮背景的颜色会变暗,当鼠标退出按钮时,动画会恢复到正常状态 首先,我认为我可以通过FillTransition实现这一点,但为此,我需要按钮的特定深色,这取决于按钮的颜色。 所以现在我试着在按钮顶部淡入淡出一个低透明度的黑色矩形,但是这个矩形看起来根本没有出现 这是我的按钮的代码: public class FlatBu
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();
}
});
}
}
公共类FlatButton扩展按钮{
私有矩形;
专用持续时间dimDuration=持续时间。毫秒(250);
专用颜色dimColor=新颜色(0,0,0,0.11);
公共平面按钮(字符串文本){
超级(文本);
getStyleClass().addAll(“平面按钮样式”);
createEffect();
}
私有void createEffect()
{
dimRectangle=新矩形(this.getWidth()、this.getHeight()、dimColor);
dimRectangle.setOpacity(1.0);
dimRectangle.setX(this.get);
FadeTransition enterTransition=新的FadeTransition(dimDuration,this);
输入transition.setInterpolator(Interpolator.EASE_OUT);
entertransform.setFromValue(0.0);
entertransformation.setToValue(1.0);
FadeTransition exitTransition=新的FadeTransition(dimDuration,this);
exitTransition.setInterpolator(Interpolator.EASE_OUT);
exitTransition.setFromValue(1.0);
exitTransition.setToValue(0.0);
this.setOnMouseCentered(新的EventHandler(){
公共无效句柄(MouseEvent MouseEvent){
enter transition.play();
}
});
this.setOnMouseExited(新的EventHandler(){
公共无效句柄(MouseEvent MouseEvent){
exitTransition.play();
}
});
}
}
编辑:代码“new FadeTransition(dimDuration,this);”中的部分应为“new FadeTransition(dimDuration,dimRectangle);”。这只是我在测试的东西
EDIT2:我认为“dimRectangle=new Rectangle(this.getWidth(),this.getHeight(),dimColor);”并没有真正起作用,但我还没有找到一种方法来让矩形填充按钮尺寸。您可以使用效果,并使用
除非它在这个代码之外,否则您似乎不会在场景图的任何地方添加
dimRectangle
?不,它不在代码之外。我可以直接在这个button类中添加它吗?我有点遵循这种材料涟漪效应代码:它似乎也没有添加到场景图中。我也看不出在代码中是如何添加到场景图中的。我要走的路线是将这两个按钮和按钮添加到堆栈窗格
,然后您应该能够在矩形上设置MouseTransparent()
,并以相同的方式淡入淡出。谢谢!它工作正常,但有没有办法不将效果应用于按钮文本?
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);
}
}