如何使这个线程工作?

时间:2013-12-17 04:01:32

标签: java multithreading thread-sleep

好的,这是我的代码:

package game;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Timer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class PatternGame implements Properties
{
public static JFrame df = new JFrame();
public static int height = 800;
public static int width = 600;
public static int gsize = 4;
public static int order =1;
public static Dimension size = new Dimension(height,width);
public static menu Menu = new menu();
public static GridLayout Ggrid = new GridLayout(gsize,gsize);

public void DisplayPat()
{
    df.dispose();
    df = new JFrame();
    df.setLocation(300,100);
    df.setSize(size);
    df.setResizable(false);
    df.setLayout(Ggrid);
    df.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    df.setVisible(true);


}
public static InputGame game = new InputGame();
public static int flag =0;
public static void clear()
{
    df.dispose();
}
public void gameStart()
{

    DisplayPat();
    getpattern();
    try {
        Thread.sleep(3000);
    } 
    catch (InterruptedException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    InputGame.setup();

//notifyAll();
}
public void blank()
{
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {

            JButton button = new JButton(" ");
            df.add(button);


        }
    }
}
public void getpattern()
{
    if (order == 1)
        {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (handlebars[a][b] == 1)
                {
                JButton button = new JButton("X");
                df.add(button);
                }
                else
                {
                    JButton button = new JButton("");
                    df.add(button);
                }
                flag =1;

            }
        }
        }
    if (order == 2)
        {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (ys[a][b] == 1)
                {
                JButton button = new JButton("X");
                df.add(button);
                }
                else
                {
                    JButton button = new JButton("");
                    df.add(button);
                }
            }
        }
        }
    if (order == 3)
    {
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {
            if (spaceShip[a][b] == 1)
            {
            JButton button = new JButton("X");
            df.add(button);
            }
            else
            {
                JButton button = new JButton("");
                df.add(button);
            }
        }
    }
    }   if (order == 4)
    {
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {
            if (flock[a][b] == 1)
            {
            JButton button = new JButton("X");
            df.add(button);
            }
            else
            {
                JButton button = new JButton("");
                df.add(button);
            }
        }
    }
    }   if (order == 5)
    {
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {
            if (percent[a][b] == 1)
            {
            JButton button = new JButton("X");
            df.add(button);
            }
            else
            {
                JButton button = new JButton("");
                df.add(button);
            }
        }
    }
    }
    if (order == 6)
    {
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {
            if (square[a][b] == 1)
            {
            JButton button = new JButton("X");
            df.add(button);
            }
            else
            {
                JButton button = new JButton("");
                df.add(button);
            }
        }
    }
    }
    df.setVisible(true);
}



}

package game;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class InputGame implements Properties, Runnable{
public static JFrame gf = new JFrame();
public static int height = 800;
public static int width = 600;
public static int gsize = 4;
public static int order =1;
public static Dimension size = new Dimension(height,width);
public static menu Menu = new menu();
public static GridLayout Ggrid = new GridLayout(gsize,gsize);
//public static Thread d;


public static void setup() {

    gf.dispose();
    gf = new JFrame();
    gf.setLocation(300,100);
    gf.setSize(size);
    gf.setResizable(false);
    gf.setLayout(Ggrid);
    gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    PatternGame.clear();
    blank();
    gf.setVisible(true);

}
public static void blank()
{
    for (int a =0;a<4;a++)
    {
        for (int b =0;b<4;b++)
        {
            JButton button = new JButton("");
            gf.add(button);
        }
    }
}
static Thread t;
public void run() {
    // TODO Auto-generated method stub
    t = new Thread(this);
    t.start();
}
}

现在我希望此代码在屏幕上显示一个模式,等待三秒钟,然后显示一个空白屏幕供用户输入。现在,当我启动它时,它会显示一个白色屏幕,然后显示空白屏幕。我知道白屏背后的原因与显示器在睡眠前无法获得主线程有关。我怎样才能解决这个问题? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用Timer,如下所示:

public void gameStart() {
    DisplayPat();
    getpattern();
    schedule(3); //start the timer task
}

Timer timer = new Timer(); //timer object

public void schedule(int seconds) {
    timer.schedule(new ScheduleTask(), seconds*1000);
}

class ScheduleTask extends TimerTask {
    public void run() {
        System.out.format("Time completed, invoke setup");
        InputGame.setup();
        timer.cancel(); //Terminate Timer thread
    }
}