用java编程画机器猫_用JAVA编程:编写GUI程序,模拟龟兔赛跑游戏

匿名用户

7级

2012-05-28 回答

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; //定义一个JFrame窗体作为显示载体 public class TestTortoiseRabbit extends JFrame { private static final long serialVersionUID = 987654321L; static final int MAX_LENGTH = 700; private JButton begin = new JButton("开始"); private JButton exit = new JButton("退出"); private Rabbit rabbit = new Rabbit("兔子"); private Tortoise tortoise = new Tortoise("乌龟"); private JLabel stateOfRabbit = new JLabel(); private JLabel winner; public TestTortoiseRabbit() { super("龟兔赛跑"); setLayout(null); setBounds(200, 150, 800, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); add(rabbit); rabbit.setBounds(20, 100, 50, 20); add(tortoise); tortoise.setBounds(20, 150, 50 ,20); add(stateOfRabbit); stateOfRabbit.setBounds(300, 50, 100 ,20); add(begin);begin.setBounds(20, 200, 80, 30); add(exit);exit.setBounds(100, 200, 80, 30); setVisible(true); begin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new Thread(rabbit).start(); new Thread(tortoise).start(); begin.setVisible(false); stateOfRabbit.setText("兔子跑起来了!"); } }); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String[] args) { new TestTortoiseRabbit(); } class Tortoise extends JLabel implements Runnable { private static final long serialVersionUID = 12345678L; public Tortoise(String name) { super(name); } public Tortoise(Icon icon) { super(icon); } boolean stop; int speed = 2; int x, y; public void run() { x = getLocation().x; y = getLocation().y; while (!stop) { x += speed; setLocation(x, y); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } if (x >= MAX_LENGTH) { if (TestTortoiseRabbit.this.winner == null) { TestTortoiseRabbit.this.winner = this; } else { JOptionPane.showMessageDialog(TestTortoiseRabbit.this, TestTortoiseRabbit.this.winner.getText()+ "胜利!"); } stop = true; } } } } class Rabbit extends JLabel implements Runnable { private static final long serialVersionUID = 123456789L; public Rabbit(String name) { super(name); } public Rabbit(Icon icon) { super(icon); } boolean stop; int speed = 5; int x, y; Random r = new Random(); public void run() { x = getLocation().x; y = getLocation().y; while (!stop) { x += speed; setLocation(x, y); try { Thread.sleep(50); if(r.nextInt(100) > 90){ if(speed == 0){ speed = 3; stateOfRabbit.setText("兔子跑起来了!"); }else{ speed = 0; stateOfRabbit.setText("兔子在睡觉!"); } } } catch (InterruptedException e) { e.printStackTrace(); } if (x >= MAX_LENGTH) { stateOfRabbit.setText("兔子到终点了!"); if (TestTortoiseRabbit.this.winner == null) { TestTortoiseRabbit.this.winner = this; } else { JOptionPane.showMessageDialog(TestTortoiseRabbit.this, TestTortoiseRabbit.this.winner.getText()+ "胜利!"); } stop = true; } } } } }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值