简体   繁体   English

当按下键盘上的相应键时如何使按钮按下? JavaFX

[英]How make a button pressed when corresponding key on a keyboard is pressed? JavaFX

For example, I created a button labeled "1". 例如,我创建了一个标记为“ 1”的按钮。 Whenever this button is pressed, 1 is appended to a textField. 每当按下此按钮时,都会在textField后面附加1。 However, I can add 1 to a textField simply by typing 1 on my keyboard. 但是,只需在键盘上键入1,即可将1添加到textField中。 When doing so, I'd like by button to get view as if it was pressed instead if a key. 这样做时,我希望通过按钮来获取视图,就好像按下该按钮一样,而不是按下某个键。 I've been thinking thant may be it's possible to manage this issue in this way: 我一直在想可能可以通过这种方式管理此问题:

   rootNode.setOnKeyTyped(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent event) {
            textField.appendText(event.getCharacter());
            if(event.getCharacter().equals("1")){
                // here button should be pressed
            }
        }
    });

Is there some method, that can change appearance of the button? 有没有什么方法可以改变按钮的外观? Thanks is advance. 感谢是前进。

@James_D , You program works correctly, but I connt apply your solution to my program. @James_D,您的程序正常工作,但是我同意将您的解决方案应用于我的程序。 Maybe it's because I customized my buttons. 也许是因为我自定义了按钮。 Have a look at a part of my code: 看一下我的代码的一部分:

    HashMap<String, Button> buttons = new HashMap<>();
    int btnVal = 1;
    for(int j = 5 ; j >= 3; j --){
    for(int i = 1; i <= 3; i ++){
       Button btn = createNumberButton(Integer.toString(btnVal++), inputField, i, j);
       rootNode.getChildren().add(btn);
       buttons.put(Integer.toString(btnVal), btn);
       }
    }
    rootNode.setOnKeyPressed(event -> {
        Button btn = buttons.get(event.getText());
        if (btn != null) {
            System.out.println(event.getText());
            btn.arm();
            inputField.appendText(event.getText());
        }
    });

    rootNode.setOnKeyReleased(event -> {
        Button btn = buttons.get(event.getText());
        if (btn != null) {
            btn.disarm();
        }
    });

Use button.arm() (and button.disarm() to "release" it). 使用button.arm() (和button.disarm()来“释放”它)。

SSCCE: SSCCE:

import java.util.HashMap;
import java.util.Map;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class PressButtonOnTyping extends Application {

    @Override
    public void start(Stage primaryStage) {

        GridPane root = new GridPane();
        TextField textField = new TextField();
        textField.setDisable(true);

        root.add(textField, 0, 0, 3, 1);

        Map<String, Button> buttons = new HashMap<>();

        for (int i = 1 ; i <= 9; i++) {
            Button button = createButton(i, textField);
            buttons.put(Integer.toString(i), button);
            root.add(button, (i-1)%3, 1+ (i-1) / 3);
        }
        Button zeroButton = createButton(0, textField);
        root.add(zeroButton, 1, 4);
        buttons.put("0", zeroButton);

        root.setOnKeyPressed(e -> {
            Button button = buttons.get(e.getText());
            if (button!=null) {
                button.arm();
                textField.appendText(e.getText());
            }
        });

        root.setOnKeyReleased(e -> {
            Button button = buttons.get(e.getText());
            if (button!=null) {
                button.disarm();
            }           
        });

        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }

    private Button createButton(int value, TextField target) {
        String s = Integer.toString(value);
        Button button = new Button(s);
        button.setOnAction(e -> target.appendText(s));
        return button ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM