package Basic;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Bounce { public static void main(String[] args) {
JFrame frame = new BounceThreadFrame();
frame.setVisible(true);
}
}class BounceThreadFrame extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel canvas;
JLabel a1, a2;
JTextField b1, b2;

public BounceThreadFrame(){
setSize(400, 400);
setTitle("lift");

a1 = new JLabel();
a1.setText("一楼");
a1.setBounds(50, 200, 30, 30);
add(a1); JLabel a2 = new JLabel();
a2.setText("二楼");
a2.setBounds(50, 100, 30, 30);
add(a2);

b1 = new JTextField(20);
b1.setBounds(100, 40, 150, 110);
b1.setEnabled(false);
add(b1); b2 = new JTextField(20);
b2.setBounds(100, 140, 150, 110);
b2.setEnabled(false);
add(b2);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}); Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Up", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Ball b = new Ball(canvas);
b.start();
}
}); addButton(p, "Down", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Ball b = new Ball(canvas);
b.start();
}
});
contentPane.add(p, "South");
} public void addButton(Container c, String title, ActionListener a) {
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
}class Ball extends Thread {
private JPanel box;
private static final int XSIZE = 10;
private static final int YSIZE = 10;
private int x = 175; private int y = 240; private int count = 1; public Ball(JPanel b) {
box = b;
}
public void draw(){
 Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
}
public void move() {
if (!box.isVisible())
return;
    Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
while (true) {
while (count == 1 && y > 140) {
y--;
g.fillOval(x, y, XSIZE, YSIZE);
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
count++;
while (count == 2 && y < 240) {
y++;
g.fillOval(x, y, XSIZE, YSIZE);
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
}
} public void run() {
try {
draw();
move();
sleep(10);
} catch (InterruptedException e) {
}
}

}