题目:编写一个程序,在面板上移动小球。应该定义一个面板类来显示小球,并提供向上下左右移动小球的方法。请进行边界检查以防止小球移动到视线之外。
问题:我写的程序可以运行但是无法显示小球的移动,如果将移动改为改变小球颜色则可以显示,检查许久也检查不到问题在哪,所以贴上来问问大佬们,问题出在哪里?应该如何改?
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
public class MoveBall_3 extends Application{
private CirclePane circlePane = new CirclePane(250,250);
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage primaryStage) throws Exception {
Button bt1 = new Button("Left");
Button bt2 = new Button("Right");
Button bt3 = new Button("Up");
Button bt4 = new Button("Down");
bt1.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
// TODO 自动生成的方法存根
circlePane.moveLeft();
}
});
bt2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// TODO 自动生成的方法存根
circlePane.moveRight();
}
});
bt3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// TODO 自动生成的方法存根
circlePane.moveUp();
}
});
bt4.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// TODO 自动生成的方法存根
circlePane.moveDown();
}
});
FlowPane pane2 = new FlowPane();
pane2.getChildren().addAll(bt1,bt2,bt3,bt4);
circlePane.getChildren().addAll(pane2);
Scene scene = new Scene(circlePane,500,500);
primaryStage.setTitle("MoveBall");
primaryStage.setScene(scene);
primaryStage.show();
}
}
class CirclePane extends StackPane{
private Circle circle = new Circle(250,250,50);
public CirclePane() {
getChildren().add(circle);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
}
public CirclePane(double a,double b) {
getChildren().add(circle);
circle.setCenterX(a);
circle.setCenterY(b);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
}
public void moveLeft() {
if(circle.getCenterX()-50-15 >= 0) {
circle.setCenterX(circle.getCenterX()-15);
circle.setCenterY(circle.getCenterY());
}
else {
circle.setCenterX(50);
circle.setCenterY(circle.getCenterY());
}
}
public void moveRight() {
if(circle.getCenterX()+50+15 <= 500) {
circle.setCenterX(circle.getCenterX()+15);
circle.setCenterY(circle.getCenterY());
}
else {
circle.setCenterX(450);
circle.setCenterY(circle.getCenterY());
}
}
public void moveUp() {
if(circle.getCenterY()-50-15 >= 0) {
circle.setCenterY(circle.getCenterY()-15);
circle.setCenterX(circle.getCenterX());
}
else {
circle.setCenterY(50);
circle.setCenterX(circle.getCenterX());
}
}
public void moveDown() {
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 移动小球内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://blog.csdn.net/weixin_43461540/article/details/85631033
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 82
-
2025-05-29 91
-
2025-05-29 23
-
2025-05-29 25
-
2025-05-29 26
热门评论

