好了,所以這是我的代碼:如何讓這個線程工作?
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();
}
}
現在我想這個代碼顯示在屏幕上的圖案等待三秒鐘,然後爲用戶輸入顯示空白屏幕。現在,當我啓動它時,它會顯示一個白色屏幕等待,然後顯示空白屏幕。我知道白屏背後的原因是顯示器在睡眠之前無法獲得主線程。我怎樣才能解決這個問題? 在此先感謝!
你可以使用的wait()和notify(),等(3000)在等待幾秒鐘,然後通知是開始等待線程 –