注意修改参数
package javaseniorprograme; import java.util.ArrayList; import javafx.application.Application; import javafx.event.Event; import javafx.event.EventHandler; import javafx.scene.Scene;import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import static javafx.scene.input.KeyEvent.KEY_TYPED; import javafx.scene.layout.BorderPane;import javafx.scene.layout.Pane; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.Text; import javafx.stage.Stage; /** * 输入并显示字符串 * @author ASUS */ public class Exercise15_10 extends Application{ static ArrayList aList = new ArrayList(); @Override public void start(Stage primaryStage){ // 创建文本 Text text = new Text(); text.setFont(Font.font("Arial", FontPosture.ITALIC, 20)); text.setText("6"); BorderPane pane = new BorderPane(); pane.setCenter(text); Scene scene = new Scene(pane,600,300); primaryStage.setTitle("Exercise15_10"); primaryStage.setScene(scene); primaryStage.show(); // 文本获得响应 text.requestFocus(); // 键盘事件1 text.setOnKeyTyped(e->{ aList.add(e.getCharacter()); //text.setText(String.join("", aList)); }); // 键盘事件2 text.setOnKeyPressed(e->{ if(e.getCode()==KeyCode.ENTER){ text.setText(String.join("", aList)); } }); } public static void main(String[] args){ Application.launch(args); } }