-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBounceFrame.java
More file actions
62 lines (52 loc) · 1.37 KB
/
BounceFrame.java
File metadata and controls
62 lines (52 loc) · 1.37 KB
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
package Chapter14Thread.bounce;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BounceFrame extends JFrame{
private BallComponent comp;
private static final int STEPS=100;
private static final int DELAY=3;
public BounceFrame(){
this.setTitle("弹球测试");
comp=new BallComponent();
add(comp,BorderLayout.CENTER);
JPanel buttonPanel=new JPanel();
//添加按钮及监听
addButton(buttonPanel,"start",new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBall();
}
});
addButton(buttonPanel, "stop",new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
private void addButton(Container c, String title, ActionListener actionListener) {
JButton button=new JButton(title);
c.add(button);
button.addActionListener(actionListener);
}
private void addBall(){
Ball ball=new Ball();
comp.add(ball);
for(int i=1;i<STEPS;i++){
try {
System.out.println(i);
ball.move(comp.getBounds());
comp.paint(comp.getGraphics());
//暂停当前线程的活动
Thread.sleep(DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}