package javaseniorprograme;
import javafx.application.Application;
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.stage.Stage;
/**
* 移动小球
* @author 李安国
*/
public class Exercise15_03 extends Application{
@Override
public void start(Stage primaryStage){
// 创建一个面板
Pane pane = new Pane();
// 创建一个HBox
HBox hbox = new HBox();
// 创建四个按钮
Button bt1 = new Button("Left");
Button bt2 = new Button("Right");
Button bt3 = new Button("Up");
Button bt4 = new Button("Down");
hbox.setAlignment(Pos.CENTER);
hbox.setSpacing(10);
hbox.getChildren().addAll(bt1,bt2,bt3,bt4);
// 创建一个圆
Circle circle = new Circle(50,50,25);
// 设置轮廓颜色
circle.setStroke(Color.BLACK);
// 设置填充色
circle.setFill(Color.WHITE);
pane.getChildren().add(circle);
// 创建一个BorderPane面板
BorderPane bdpane = new BorderPane();
bdpane.setCenter(pane);
bdpane.setBottom(hbox);
Scene scene = new Scene(bdpane,300,300);
bt1.setOnAction(e->{circle.setCenterX(circle.getCenterX() > 0 ? circle.getCenterX() - 10 : scene.getWidth());});
bt2.setOnAction(e->{circle.setCenterX(circle.getCenterX() < scene.getWidth()? circle.getCenterX() + 10 : 0);});
bt3.setOnAction(e->{circle.setCenterY(circle.getCenterY() > 0 ? circle.getCenterY() - 10 : scene.getHeight());});
bt4.setOnAction(e->{circle.setCenterY(circle.getCenterY() < scene.getHeight()? circle.getCenterY() + 10 : 0);});
primaryStage.setTitle("Exercise15_03");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}