JavaFX Button发布了活动 [英] JavaFX Button released event

查看:127
本文介绍了JavaFX Button发布了活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,我想在释放时为按下的动作分配不同的动作。我在使用 javax.swing.ButtonModel 之前在Swing中做了这个,类似于:

I have a Button that I want to assign a different action on release to the pressed action. I did this in Swing before using javax.swing.ButtonModel similar to this:

ChangeListener changeListnerUp = new ChangeListener() {

    @Override
    public void stateChanged(ChangeEvent event) {

       AbstractButton button = (AbstractButton) event.getSource();
       ButtonModel model = button.getModel();
       boolean pressed = model.isPressed();

       if (pressed) {
           System.out.println( "Up Button pressed, String: " + upString );
       } else
            System.out.println( "Up Button released, String: " + stopString );
       }
    }

};

任何人都可以在JavaFX中推荐合适的替代方案吗?

Can anyone recommend a suitable alternative in JavaFX?

推荐答案

您可以捕获多个事件,但在这种情况下,已经存在根据这些事件更改的属性: 属性或者 武装属性,如果用鼠标离开按钮应导致事件。只需听听此属性的更改

You could catch multiple events, but in this case there is already a property that is changed according to these events: The pressed property or alternatively the armed property, if leaving the Button with the mouse should cause an event. Simply listen to changes of this property

示例:

以下代码为设置动画圆圈只要按下按钮

@Override
public void start(Stage primaryStage) {
    Circle circle = new Circle(300, 20, 5);
    Timeline animation = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(circle.centerYProperty(), circle.getCenterY())),
            new KeyFrame(Duration.seconds(1), new KeyValue(circle.centerYProperty(), 300))
    );
    animation.setCycleCount(Animation.INDEFINITE);
    animation.setAutoReverse(true);

    Button btn = new Button("Play");
    btn.pressedProperty().addListener((observable, wasPressed, pressed) -> {
        System.out.println("changed");
        if (pressed) {
            animation.play();
        } else {
            animation.pause();
        }
    });

    Pane root = new Pane(btn, circle);

    Scene scene = new Scene(root, 400, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}

这篇关于JavaFX Button发布了活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆