日期:2014-05-20  浏览次数:20796 次

一个关于界面交互中死循环的问题~·谢谢各位~·在线等着的~·
最近在做一个数据结构的课程设计遇上些麻烦,其中有个问题想了好几天还是没头绪的,于是只能上网求助各位了。。。由于明儿要第一次演示程序的,而我又刚好在最后的一个坎给卡主了,所以希望大家能帮助我,我在线等的。。。下面是我为了说明问题写的一个简易的小程序例子:
 

import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 
import javax.swing.*;
 public class test03 extends JFrame implements ActionListener{
 
/**
  * @param args
  */
 JPanel p1;
 JLabel jlb;
 JButton jb1;

 public static void main(String[] args) {
 // TODO Auto-generated method stub
 test03 test03 = new test03 ();


 }
 
public test03(){

 p1=new JPanel();
 p1.setBackground(Color.black);
 //p1.setLayout(new BorderLayout());


 jb1=new JButton("新建一个窗口");
jb1.addActionListener(this);
 p1.add(jb1);

 this.add(p1);
 
this.setSize(615, 473);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.setVisible(true);
 }
 
@Override
 public void actionPerformed(ActionEvent arg0) {
 // TODO Auto-generated method stub
 test04 test04 = new test04 ();
 }
 }
 


 class test04 extends JFrame implements ActionListener{
  JPanel p11;
 JLabel jlb1;
 JButton jb11;

 Boolean []bool=new Boolean[11];
 int cc=0;
int count=0;
 public test04(){
 for(int i=0;i<11;i++){
 bool[i]=true;
 }
 
p11=new JPanel();
 p11.setBackground(Color.black);



 jb11=new JButton("画图");
jb11.addActionListener(this);
 p11.add(jb11);

 this.add(p11);

 this.setSize(615, 473);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.setVisible(true);
 }


  public void paint(Graphics g) {
        super.paint(g);
      
     g.setColor(Color.green);
      
     while( bool[cc] ){
    try {
 Thread.sleep(2000);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
      }
cc++;
      g.fillOval(10, 10, 10, 10);
                        while( bool[cc] ){
    try {
 Thread.sleep(2000);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
      }
 cc++;
     g.fillOval(91, 105, 10, 10);
                         while( bool[cc] ){