注意修改參數
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); } }
