Java语言程序设计基础篇
*15.3 (移动小球)
- 编写一个程序,在面板上移动小球。应该定义一个面板类来显示小球,并提供向左、 向右 、向上和向下移动小球的方法,如图15-24c所示。请进行边界检査以防止球完全移到视线之外
代码展示:编程练习题15_3MoveBall.java
package chapter_15; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.text.Text; import javafx.stage.Stage; public class 编程练习题15_3MoveBall extends Application{ @Override public void start(Stage primaryStage) throws Exception { BorderPane borderPane = new BorderPane(); Pane pane = new Pane(); HBox hBox = new HBox(); pane.setPadding(new Insets(10, 10, 10, 10)); hBox.setAlignment(Pos.CENTER); Circle circle = new Circle(150, 150, 20); circle.setFill(Color.WHITE); circle.setStroke(Color.BLACK); pane.getChildren().add(circle); Button btLeft = new Button("Left"); Button btRight = new Button("Right"); Button btUp = new Button("Up"); Button btDown = new Button("Down"); Text text = new Text("下一个动作将越界!"); btLeft.setOnMouseClicked(e->{ borderPane.setTop(null); if(circle.getCenterX()-circle.getRadius()*2 { borderPane.setTop(null); if(circle.getCenterX()+circle.getRadius()*2 > borderPane.getWidth()) { borderPane.setTop(text); return; } circle.setCenterX(circle.getCenterX()+circle.getRadius()); }); btUp.setOnMouseClicked(e->{ borderPane.setTop(null); if(circle.getCenterY()-circle.getRadius()*2 { borderPane.setTop(null); if(circle.getCenterY()+circle.getRadius()*2 > borderPane.getHeight()) { borderPane.setTop(text); return; } circle.setCenterY(circle.getCenterY()+circle.getRadius()); }); hBox.getChildren().addAll(btLeft, btRight, btUp, btDown); borderPane.getChildren().add(pane); borderPane.setBottom(hBox); Scene scene = new Scene(borderPane, 300, 300); primaryStage.setTitle("编程练习题14_3Poker"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { Application.launch(args); } }
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

