packagesample;importjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.input.KeyCode;importjavafx.scene.layout.Pane;importjavafx.scene.shape.Circle;importjavafx.scene.shape.Line;importjavafx.stage.Stage;publicclassMainextendsApplication{privatePane pane=newPane();privatedouble x =300;privatedouble y =300;@Overridepublicvoidstart(Stage primaryStage){
pane.setFocusTraversable(true);
pane.setOnKeyPressed(e ->{if(e.getCode()==KeyCode.UP&& y>=30){
pane.getChildren().add(newLine(x, y, x, y -30));
y -=30;}elseif(e.getCode()==KeyCode.DOWN&& y<=570){
pane.getChildren().add(newLine(x, y, x, y +30));
y +=30;}elseif(e.getCode()==KeyCode.LEFT&& x>=30){
pane.getChildren().add(newLine(x, y, x -30, y));
x -=30;}elseif(e.getCode()==KeyCode.RIGHT&& x<=570){
pane.getChildren().add(newLine(x, y, x +30, y));
x +=30;}});Scene scene =newScene(pane,600,600);
primaryStage.setTitle("使用箭头键画线");
primaryStage.setScene(scene);
primaryStage.show();
pane.requestFocus();}publicstaticvoidmain(String[] args){launch(args);}}