📄 box.java
字号:
package box;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Box extends JButton implements ActionListener {
// this stores the starting location of the box
private Point startingPoint;
Box (Rectangle bounds, Icon icon) {
// set button's icon
super(icon);
// set x, y, w, h
setBounds(bounds);
// get starting x, y
startingPoint = bounds.getLocation();
// add action listener
addActionListener (this);
}
// move the box down
public void moveDown () {
this.setLocation( (int)getLocation().getX(), // same 'x'
(int)getLocation().getY() + 1); // increment 'y' coordinate
}
public int getY () {
// return the 'y' coordinate
return (int) getLocation().getY();
}
// move the box to starting point
public void moveToStart() {
setLocation(startingPoint);
}
// implement ActionListener interface
public void actionPerformed (ActionEvent ae){
new BoxAnimationThread ();
}
/**
* Inner class to animate the Box object
*/
class BoxAnimationThread implements Runnable {
BoxAnimationThread (){
// create a thread with this Runnable object
Thread t = new Thread (this);
// starts thread, calls the following 'run()' method
t.start();
}
/**
* Moves the box down until it reaches the
* end of frame. Then resets to starting point
*/
public void run() {
// check if within the bounds of frame
while(getY()< AnimateBox.MAX_HEIGHT) {
// call method of box to move down
moveDown();
try{
Thread.sleep(5); // delay of 5 msec
}catch(InterruptedException ie){}
}
// reset location of button to starting location
moveToStart();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -