【训练4】龟兔赛跑
使用线程的加入模拟龟兔赛跑:兔子跑到70米时,开始睡觉;乌龟爬至终点时,兔子醒了跑至终点。
/*【训练4】龟兔赛跑
* 使用线程的加入模拟龟兔赛跑:兔子跑到70米时,开始睡觉;乌龟爬至终点时,兔子醒了跑至终点。
* */
package dxc;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Xl4 extends JFrame{
private JPanel contentPane;
private JTextArea rabbitTextArea;
private JTextArea tortoiseTextArea;
Runnable runRabbit = new Rabbit();
Runnable runTortoise = new Tortoise();
Thread rabbit = new Thread(runRabbit);
Thread tortoise = new Thread(runTortoise);
public static void main(String[] args) {
// TODO Auto-generated method stub
Xl4 frame = new Xl4();
frame.setVisible(true);
}
public Xl4() {
setTitle("龟兔赛跑");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
// 按钮
JPanel buttonPane = new JPanel();
contentPane.add(buttonPane, BorderLayout.SOUTH);
JButton button = new JButton("比赛开始");
button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
do_button_actionPerformed(arg0);
}
});
buttonPane.add(button);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(1, 2, 5, 5));
// 兔子的比赛记录
JPanel rabbitPane = new JPanel();
panel.add(rabbitPane);
rabbitPane.setLayout(new BorderLayout(0, 0));
JLabel rabbitLabel = new JLabel("兔子的比赛记录");
rabbitLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
rabbitLabel.setHorizontalAlignment(SwingConstants.CENTER);
rabbitPane.add(rabbitLabel, BorderLayout.NORTH);
rabbitTextArea = new JTextArea();
rabbitTextArea.setFont(new Font("微软雅黑",Font.PLAIN,16));
javax.swing.JScrollPane rabbitScrollPane = new javax.swing.JScrollPane();
rabbitScrollPane.setViewportView(rabbitTextArea);
rabbitPane.add(rabbitScrollPane);
// 乌龟的比赛记录
JPanel tortoisePane = new JPanel();
panel.add(tortoisePane);
tortoisePane.setLayout(new BorderLayout(0, 0));
JLabel tortoiseLabel = new JLabel("乌龟的比赛记录");
tortoiseLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
tortoiseLabel.setHorizontalAlignment(SwingConstants.CENTER);
tortoisePane.add(tortoiseLabel, BorderLayout.NORTH);
tortoiseTextArea = new JTextArea();
tortoiseTextArea.setFont(new Font("微软雅黑",Font.PLAIN,16));
javax.swing.JScrollPane tortoiseScrollPane = new javax.swing.JScrollPane();
tortoiseScrollPane.setViewportView(tortoiseTextArea);
tortoisePane.add(tortoiseScrollPane);
}
protected void do_button_actionPerformed(ActionEvent arg0) {
rabbit.start();
tortoise.start();
}
private class Rabbit implements Runnable{
@Override
public void run() {
for(int i = 1;i < 11;i++) {
String text = rabbitTextArea.getText();
try {
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
rabbitTextArea.setText(text + "兔子跑了" + i + "0米\n");
if(i == 7) {
rabbitTextArea.setText(text + "兔子在睡觉\n");
try {
tortoise.join();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
if(i == 10) {
rabbitTextArea.setText(text + "兔子到达终点\n");
}
}
}
}
private class Tortoise implements Runnable{
@Override
public void run() {
for(int i = 1;i < 11;i++) {
String text = tortoiseTextArea.getText();
try {
Thread.sleep(500);
}catch(InterruptedException e) {
e.printStackTrace();
}
tortoiseTextArea.setText(text + "乌龟跑了" + i + "0米\n");
if(i == 10) {
tortoiseTextArea.setText(text + "乌龟到达终点\n");
}
}
}
}
}