0.題目描述
《Java語言程序設計與數據結構(基礎篇)》編程練習題15.3
15.3(移動小球)編寫一個程序,在面板上移動小球。需定義一個面板類來顯示小球,並提供向左、向右、向上和向下移動小球的方法。請進行邊界檢查以防止球完全移到視線之外。
1.UML類圖
球類Ball:

畫板類Board:

框架類Frame:

2.源代碼
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.shape.Circle; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; public class Test1 extends Application { Ball ball = new Ball(200, 100, 30); @Override public void start(Stage primaryStage) { Board board = new Board(ball); //Pane Frame frame = new Frame(); //HBox BorderPane borderPane = new BorderPane(); borderPane.setCenter(board); borderPane.setBottom(frame); Scene scene = new Scene(borderPane, 390, 240); board.setBackground(new Background(new BackgroundFill(Color.RED,null,null))); primaryStage.setResizable(false); primaryStage.setTitle("MoveBall"); primaryStage.setScene(scene); primaryStage.show(); frame.btLeft.setOnAction(new LeftHandler()); frame.btRight.setOnAction(new RightHandler()); frame.btUp.setOnAction(new UpHandler()); frame.btDown.setOnAction(new DownHandler()); } class LeftHandler implements EventHandler <ActionEvent> { @Override public void handle(ActionEvent e) { ball.Left(); ball.checkBall(); } } class RightHandler implements EventHandler <ActionEvent> { @Override public void handle(ActionEvent e) { ball.Right(); ball.checkBall(); } } class UpHandler implements EventHandler <ActionEvent> { @Override public void handle(ActionEvent e) { ball.Up(); ball.checkBall(); } } class DownHandler implements EventHandler <ActionEvent> { @Override public void handle(ActionEvent e) { ball.Down(); ball.checkBall(); } } public static void main(String[] args) { Application.launch(args); } } //球類 class Ball extends Circle { public Ball() { } public Ball(double x, double y, double r) { super(x,y,r); setStroke(Color.BLACK); //設置顏色 setFill(Color.WHITE); //設置填充 } public void Left() { setCenterX(getCenterX() - 10); } public void Right() { setCenterX(getCenterX() + 10); } public void Up() { setCenterY(getCenterY() - 10); } public void Down() { setCenterY(getCenterY() + 10); } public void checkBall() { System.out.println("X:"+getCenterX()+" Y:"+getCenterY()); if (getCenterX() < 0) { Right(); } if (getCenterX() > 400) { Left(); } if (getCenterY() < 0) { Down(); } if (getCenterY() > 220) { Up(); } System.out.println("X:"+getCenterX()+" Y:"+getCenterY()+"\n"); } } //畫板類 class Board extends Pane { public Board() {} public Board(Ball ball) { getChildren().add(ball); } } //框架類 class Frame extends HBox { public Button btLeft = new Button("Left"); public Button btRight = new Button("Right"); public Button btUp = new Button("Up"); public Button btDown = new Button("Down"); public Frame() { this.getChildren().add(btLeft); this.getChildren().add(btRight); this.getChildren().add(btUp); this.getChildren().add(btDown); setAlignment(Pos.CENTER); } }
3.運行結果

邊界情況:


4.技術總結
將四個Button放在一個HBox中,使用BorderPane將畫板和HBox分別設置在上(中)部和下部。
難點主要在於按鈕調動函數的格式寫法。
