我的應用程序中有一個按鈕。點擊它會導致與按某個鍵完全相同的操作。所以我想,如果用戶按下按鍵,按鈕看起來像被點擊,即短時間變黑,然後再次恢復正常。所以,在我的鍵盤處理我寫道: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