package cn.hncu.MyThread2; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.Timer; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class BallsJFrame2 extends JFrame implements ChangeListener,ActionListener{ //这些变量需要做事件监听,因此不能放在构造方法里面 private JSpinner spinner; private RollBalls ball; private JButton btn1,btn2; public BallsJFrame2(){ super("Rolling balls"); this.setBounds(400, 200, 700, 600); this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭窗口的功能 this.setResizable(false);//设置窗口不能拉大拉小,true为能够拉伸 Color colors[]={Color.black,Color.blue,Color.cyan,Color.DARK_GRAY,Color.green, Color.red,Color.magenta,Color.orange,Color.pink,Color.yellow};//为每一个球设置颜色 //建立球的对象,同时会将整个画布添加进去 ball=new RollBalls(100, colors); this.getContentPane().add(ball); JPanel panel=new JPanel(); this.getContentPane().add(panel,"South"); panel.add(new JLabel("delay")); spinner=new JSpinner(); spinner.setValue(100); panel.add(spinner); spinner.addChangeListener(this); btn1=new JButton("start"); btn1.setEnabled(false); btn1.addActionListener(this); panel.add(btn1); btn2=new JButton("stop"); btn2.addActionListener(this); panel.add(btn2); this.setVisible(true); } public static void main(String[] args) { new BallsJFrame2(); } @Override public void stateChanged(ChangeEvent e) { int value=Integer.parseInt(""+spinner.getValue()); ball.setDelay(value); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource()==btn1) { btn1.setEnabled(false); btn2.setEnabled(true); ball.setStart(); } if (e.getSource()==btn2) { btn1.setEnabled(true); btn2.setEnabled(false); ball.setStop(); } } } class RollBalls extends Canvas implements ActionListener{ private Balls balls[]; private int delay; private Timer timer; public RollBalls(int delay,Color colors[]){ this.delay=delay; balls=new Balls[colors.length]; //将每一个球对象的位置和大小确定 for (int i = 0,x=40; i < balls.length; i++,x+=20) { balls[i]=new Balls(x, x, colors[i]); } timer=new Timer(delay, this); timer.start();//启动线程 } public void setDelay(int delay){ timer.setDelay(delay); } public void setStop(){ timer.stop(); } public void setStart(){ timer.restart(); } @Override public void paint(Graphics g) { for (int i = 0; i < balls.length; i++) { g.setColor(balls[i].color); balls[i].x=balls[i].left? balls[i].x-10:balls[i].x+10; if (balls[i].x<=0||balls[i].x>=this.getWidth()-20) { balls[i].left=!balls[i].left; } balls[i].y=balls[i].up? balls[i].y-10:balls[i].y+10; if (balls[i].y<=0||balls[i].y>=this.getHeight()-20) { balls[i].up=!balls[i].up; } g.fillOval(balls[i].x, balls[i].y, 20, 20); } } class Balls{ private int x,y; private Color color; private boolean left,up; public Balls(int x,int y,Color color){ this.x=x; this.y=y; this.color=color; left=up=false; } } @Override public void actionPerformed(ActionEvent e) { repaint(); } }