【问题标题】:JavaFX: Make a button look like it's being clickedJavaFX:使按钮看起来像被点击
【发布时间】:2016-04-30 22:53:28
【问题描述】:

我的应用程序中有一个按钮。单击它会导致与按下某个键完全相同的操作。因此,我希望如果用户按下该键,该按钮看起来就像被点击了一样,即短暂变暗,然后再次恢复正常。因此,在我的密钥处理程序中,我写道:

myButton.fire();

这会导致触发一个动作事件,但不会在看起来像被点击的按钮中。我怎样才能完成后者?

【问题讨论】:

    标签: java button javafx


    【解决方案1】:

    您可以使用myButton.arm(...)myButton.disarm() 来“释放”它。

    由于您可能希望它看起来被按下一段时间,然后看起来被释放,因此您需要以下几行:

    myButton.arm();
    PauseTransition pause = new PauseTransition(Duration.seconds(0.5));
    pause.setOnFinished(e -> myButton.disarm());
    pause.play();
    

    如果您想在事件发布时实际触发事件,请在调用 disarm() 时调用 myButton.fire()

    pause.setOnFinished(e -> {
        myButton.disarm();
        myButton.fire();
    });
    

    这是一个 SSCCE。如果按“向上”键(即向上光标键),则模拟按下按钮:

    import javafx.animation.Animation;
    import javafx.animation.KeyFrame;
    import javafx.animation.PauseTransition;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.input.KeyCode;
    import javafx.scene.input.KeyEvent;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class PrgorammaticallyPressButton extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            IntegerProperty count = new SimpleIntegerProperty();
            Label label = new Label();
            label.textProperty().bind(count.asString("Count: %d"));
    
            Button button = new Button("Increment");
            button.setOnAction(e -> count.set(count.get()+1));
    
    
    
            VBox root = new VBox(5, label, button);
            root.setAlignment(Pos.CENTER);
            Scene scene = new Scene(root, 350, 150);
    
            scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
                if (e.getCode() == KeyCode.UP) {
                    button.arm();
                    PauseTransition pause = new PauseTransition(Duration.seconds(0.5));
                    pause.setOnFinished(evt -> {
                        button.disarm();
                        button.fire();
                    });
                    pause.play();
                }
            });
    
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

      【解决方案2】:

      定义一个EventHandler

      EventHandler evtHandler = event -> {
        System.out.println("Execute event");
      }
      

      然后将相同的事件处理程序实例分配为按钮的处理程序,并从 KeyEventListener 委托给该事件,因为您可能想区分按下了什么键:

      EventHandler<KeyEvent> keyEventHandler = event -> {
              if (event.getCode().equals(KeyCode.F) && event.isControlDown()) {
                  evtHandler.handle(event);
              }
      };
      

      【讨论】:

      • 这只是我已经拥有的另一个实现。我的问题不是:使键和按钮都导致事件。我的问题是:如果按键被按下,按钮看起来就像被点击一样。
      【解决方案3】:

      在按钮 setAction 上试试这个,

      logIn.setOnAction(new EventHandler<ActionEvent>() {
              @Override
              public void handle(ActionEvent event) {
                  final Color startColor = Color.web("#000000");
                  final Color endColor = Color.web("#FFFFFF");
                  final ObjectProperty<Color> color = new SimpleObjectProperty<Color>(startColor);
                    final Timeline timeline = new Timeline(
                            new KeyFrame(Duration.ZERO, new KeyValue(color, startColor)),
                            new KeyFrame(Duration.seconds(1), new KeyValue(color, endColor)));
      
                    final StringBinding cssColorSpec = Bindings.createStringBinding(new Callable<String>() {
                        @Override
                        public String call() throws Exception {
                            return String.format("-fx-body-color: rgb(%d, %d, %d);", 
                                    (int) (256*color.get().getRed()), 
                                    (int) (256*color.get().getGreen()), 
                                    (int) (256*color.get().getBlue()));
                        }
                    }, color);
                  logIn.styleProperty().bind(cssColorSpec);
                   timeline.play();
              }
          });
      

      【讨论】:

        【解决方案4】:

        您想使用myButton.doClick();,这模拟了被单击的按钮,而不是被触发的按钮动作。对于 javafx 按钮,使用 arm() 和 disarm() 以及 fire()。本页解释更多https://community.oracle.com/thread/2564393?start=0&tstart=0

        【讨论】:

        猜你喜欢
        • 2012-07-06
        • 2011-06-20
        • 2010-09-05
        • 1970-01-01
        • 2019-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        相关资源
        最近更新 更多