移动小球:在面板上移动小球,定义一个面板显示小球,并提供左右上下移动小球的方法。检查边界检查以防止球完全移到视线之外

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.animation.Animation;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
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;

public class Fifteen152 extends Application{
	public void start(Stage pr) {
		Pane pane=new Pane();
		Circle circle=new Circle(50,50,20);
		
		circle.setStroke(Color.BLACK);
		circle.setFill(Color.WHITE);
		pane.getChildren().add(circle);
		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.getChildren().addAll(bt1,bt2,bt3,bt4);
		hbox.setAlignment(Pos.CENTER);
		hbox.setSpacing(10);
		BorderPane bdpane=new BorderPane();
		bdpane.setLeft(pane);
		bdpane.setBottom(hbox);
		
		Scene scene=new Scene(bdpane,400,200);
		pr.setTitle("152");
		pr.setScene(scene);
		pr.show();
		 
		bt1.setOnAction(e->{circle.setCenterX(circle.getCenterX() > 0 ? circle.getCenterX() - 10 : 0);});
		bt2.setOnAction(e->{circle.setCenterX(circle.getCenterX() < scene.getWidth() ? circle.getCenterX() + 10 : 0);});
		bt3.setOnAction(e->{circle.setCenterY(circle.getCenterY() > 0 ? circle.getCenterY() - 10 : 0);});
		bt4.setOnAction(e->{circle.setCenterY(circle.getCenterY() < scene.getHeight() ? circle.getCenterY() + 10 : 0);});
	}
	
	
	public static void main(String[] args) {
		Application.launch(args);
	}

}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值