Java FX字母数字键盘

时间:2018-02-16 19:37:55

标签: java javafx custom-keyboard

我是编程的初学者。我正在创建一个看起来像traditional T9 Keyboard的键盘。我能够在显示屏上打印这些字母,但我现在几乎没有错误。我无法使用键的次数打印特定字符     压

这是我的代码,任何形式的帮助表示赞赏。感谢

    public int count = 0;

    @FXML
    private void handleButtonActionPQ(ActionEvent event) {
        count = count + 1;

        //key.btn1f();
        System.out.println(count);
        if(count <= 5 ){

            switch(count){
                case 1: display.appendText("p");
                        break;
                case 2: display.appendText("q");
                        break;
                case 3: display.appendText("x");
                        break;
                case 4: display.appendText("y");
                        break;
                case 5: display.appendText("z");
                        break;
                default: display.appendText("p");
            }
        }
        else if(count > 5){
            count = 0;
        }
    }

1 个答案:

答案 0 :(得分:0)

在下面的mcve中演示了识别按下哪个按钮的一种方法:

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

public class FxMain extends Application {

    private Button aButton, bButton;

    @Override
    public void start(Stage stage) {

        final GridPane cp = new GridPane();
        aButton = new Button("A");
        bButton = new Button("B");

        aButton.setOnAction(e ->  buttonPressed(e));
        bButton.setOnAction(e ->  buttonPressed(e));        
        cp.add(aButton, 0, 0); cp.add(bButton, 1, 0);

        stage.setScene(new Scene(cp));
        stage.show();
    }

    //you could also have buttonPressed(Button b) 
    //and use it like: aButton.setOnAction(e ->  buttonPressed(aButton))
    private void buttonPressed(ActionEvent e) {

        if(! (e.getSource() instanceof Button) ) { return;}
        Button button = (Button) e.getSource();

        if(button.equals(aButton)) { System.out.println("A pressed"); }
        else if(button.equals(bButton)) { System.out.println("B pressed"); }
    }

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