利用javaFX实现移动一个小球的示例代码
题目:编写一个程序,在面板上移动小球。应该定义一个面板类来显示小球,并提供向上下左右移动小球的方法。请进行边界检查以防止小球移动到视线之外。
问题:我写的程序可以运行但是无法显示小球的移动,如果将移动改为改变小球颜色则可以显示,检查许久也检查不到问题在哪,所以贴上来问问大佬们,问题出在哪里?应该如何改?
代码如下:
publicclassMoveBall_3extendsApplication{ privateCirclePanecirclePane=newCirclePane(250,250); publicstaticvoidmain(String[]args){ Application.launch(args); } publicvoidstart(StageprimaryStage)throwsException{ Buttonbt1=newButton("Left"); Buttonbt2=newButton("Right"); Buttonbt3=newButton("Up"); Buttonbt4=newButton("Down"); bt1.setOnAction(newEventHandler(){ @Override publicvoidhandle(ActionEventevent){ //TODO自动生成的方法存根 circlePane.moveLeft(); } }); bt2.setOnAction(newEventHandler (){ @Override publicvoidhandle(ActionEventevent){ //TODO自动生成的方法存根 circlePane.moveRight(); } }); bt3.setOnAction(newEventHandler (){ @Override publicvoidhandle(ActionEventevent){ //TODO自动生成的方法存根 circlePane.moveUp(); } }); bt4.setOnAction(newEventHandler (){ @Override publicvoidhandle(ActionEventevent){ //TODO自动生成的方法存根 circlePane.moveDown(); } }); FlowPanepane2=newFlowPane(); pane2.getChildren().addAll(bt1,bt2,bt3,bt4); circlePane.getChildren().addAll(pane2); Scenescene=newScene(circlePane,500,500); primaryStage.setTitle("MoveBall"); primaryStage.setScene(scene); primaryStage.show(); } } classCirclePaneextendsStackPane{ privateCirclecircle=newCircle(250,250,50); publicCirclePane(){ getChildren().add(circle); circle.setStroke(Color.BLACK); circle.setFill(Color.WHITE); } publicCirclePane(doublea,doubleb){ getChildren().add(circle); circle.setCenterX(a); circle.setCenterY(b); circle.setStroke(Color.BLACK); circle.setFill(Color.WHITE); } publicvoidmoveLeft(){ if(circle.getCenterX()-50-15>=0){ circle.setCenterX(circle.getCenterX()-15); circle.setCenterY(circle.getCenterY()); } else{ circle.setCenterX(50); circle.setCenterY(circle.getCenterY()); } } publicvoidmoveRight(){ if(circle.getCenterX()+50+15<=500){ circle.setCenterX(circle.getCenterX()+15); circle.setCenterY(circle.getCenterY()); } else{ circle.setCenterX(450); circle.setCenterY(circle.getCenterY()); } } publicvoidmoveUp(){ if(circle.getCenterY()-50-15>=0){ circle.setCenterY(circle.getCenterY()-15); circle.setCenterX(circle.getCenterX()); } else{ circle.setCenterY(50); circle.setCenterX(circle.getCenterX()); } } publicvoidmoveDown(){ if(circle.getCenterY()+50+15<=500){ circle.setCenterY(circle.getCenterY()+15); circle.setCenterX(circle.getCenterX()); } else{ circle.setCenterY(450); circle.setCenterX(circle.getCenterX()); } } }
到此这篇关于利用javaFX实现移动一个小球的示例代码的文章就介绍到这了,更多相关javaFX移动小球内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!