从理论到实践使用JButton,JPanel,JFrame,内部类,线程休眠,鼠标事件
package homework;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
class rabbitText extends JPanel {//用于记录兔子比赛的TextArea
private JTextArea text = new JTextArea();
private String text1;
public String getText1() {
return text1;
}
public void setText1(String a){
this.text1 = a;
}
public void setMatchSituation(String t) {
text.setText(t); //= text;
}
public rabbitText(){
add(text);
}
}
class tortoiseText extends JPanel { //用于记录乌龟比赛的TextArea
private JTextArea text = new JTextArea();
private String text1;
public String getText1() {
return text1;
}
public void setText1(String a){
this.text1 = a;
}
public void setMatchSituation(String t) {
text.setText(t); //= text;
}
public tortoiseText(){
add(text);
}
}
public class rabbitAndTortoise extends JFrame implements MouseListener{
//新建组件
public rabbitText t1 = new rabbitText();
public tortoiseText t2 = new tortoiseText();
public JButton t3 = new JButton("比赛开始");
public JButton t4 = new JButton("退出");
public JPanel t5 = new JPanel();
public rabbitAndTortoise() {
// TODO Auto-generated constructor stub
super();
Container container = getContentPane();
t1.setText1("火箭兔\n\n");
t2.setText1("牛乌龟\n\n");
t5.add(t3);
t5.add(t4);
t3.addMouseListener(this);
t4.addMouseListener(this);
container.setLayout(new BorderLayout()); //布局
container.add(t1,BorderLayout.EAST);
container.add(t2,BorderLayout.WEST);
container.add(t5,BorderLayout.SOUTH);
}
private class rabbitMatch implements Runnable{ //用于兔子比赛的线程休眠类
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 1; i <= 10; i ++){
String text1 = t1.getText1();
if(i<9){
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
text1 = text1+"兔子跑了 "+i+"0米\n";
t1.setMatchSituation(text1);
}
if(i == 9){
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
text1 = text1+"兔子跑了 90米\n兔子在睡觉\n";
t1.setMatchSituation(text1);
}
if(i == 10){
try {
Thread.sleep(12000);
} catch (Exception e) {
// TODO: handle exception
}
text1 = text1+"兔子跑了 100米\n兔子到达终点\n";
t1.setMatchSituation(text1);
}
t1.setText1(text1);
}
}
}
private class tortoiseMatch implements Runnable{ //用于乌龟比赛的线程休眠类
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 1; i <= 10; i ++){
String text1 = t2.getText1();
if( i < 10){
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
text1 = text1+"乌龟跑了 "+i+"0米\n";
t2.setMatchSituation(text1);
}
if(i == 10){
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
}
text1 = text1+"乌龟跑了 100米\n乌龟到达终点\n\n乌龟胜利\n";
t2.setMatchSituation(text1);
}
t2.setText1(text1);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == t3){
Thread a = new Thread(new rabbitMatch());
Thread b = new Thread(new tortoiseMatch());
a.start();
b.start();}
else{
System.exit(0);
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args){
rabbitAndTortoise a = new rabbitAndTortoise();
a.setTitle("牛龟与箭兔赛跑");
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(300, 400);
a.setVisible(true);
}
}