我的应用程序中有一个按钮。点击它会导致与按某个键完全相同的操作。所以我想,如果用户按下按键,按钮看起来像被点击,即短时间变黑,然后再次恢复正常。所以,在我的键盘处理我写道:JavaFX:让按钮看起来像被点击一样
myButton.fire();
导致操作事件被解雇了,但不是在寻找按钮被点击喜欢它。我怎样才能完成后者?
我的应用程序中有一个按钮。点击它会导致与按某个键完全相同的操作。所以我想,如果用户按下按键,按钮看起来像被点击,即短时间变黑,然后再次恢复正常。所以,在我的键盘处理我写道:JavaFX:让按钮看起来像被点击一样
myButton.fire();
导致操作事件被解雇了,但不是在寻找按钮被点击喜欢它。我怎样才能完成后者?
您可以使用myButton.arm(...)
,并myButton.disarm()
以 “释放” 了。
因为你很可能希望其出现压制了一段时间,然后似乎被释放,你会想沿着以下的说法:
myButton.arm();
PauseTransition pause = new PauseTransition(Duration.seconds(0.5));
pause.setOnFinished(e -> myButton.disarm());
pause.play();
,如果要真正地火的事件时,它的发布,请拨打myButton.fire()
当你调用disarm()
:
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);
}
}
定义一个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);
}
};
你想用myButton.doClick();
,这是模拟被点击的按钮,反对刚刚被解雇的按钮动作。对于javafx按钮,使用arm()和disarm()以及fire()。本页解释更https://community.oracle.com/thread/2564393?start=0&tstart=0
我在说关于javafx.scene.control.Button,而不是javax.swing.JButton。 – spilot
然后你想要使用arm()和disarm()以及fire(),请参阅https://community.oracle.com/thread/2564393?start=0&tstart=0 – Tristan
你显然没有读到这个问题。 – specializt
试试这个按钮的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();
}
});
这只是我已有的另一个实现。我的问题不是:使键和按钮导致事件。我的问题是:实现,如果按下按钮,按钮看起来像被点击。 – spilot