2013-12-17 3 views
0

Ok, так что это мой код:Как заставить эту тему работать?

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(); 
} 
} 

Теперь я хочу этот код, чтобы отобразить рисунок на экране, подождите три секунды, а затем отобразить пустой экран для ввода пользователя. Теперь, когда я запускаю его, он отображает белый экран, а затем отображает пустой экран. Я знаю, что причина, по которой стоит белый экран, связана с тем, что дисплей не может получить основной поток до того, как он спит. Как я могу это исправить? Спасибо заранее!

+0

вы можете использовать ожидание() и уведомить(), ждать (3000) ожидание несколько вторых уведомив, чтобы начать ожидающий поток –

ответ

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 
    } 
} 
+0

Поскольку установка модифицируя GUI, таймер Swing был бы лучшим выбором. –

+0

Спасибо, что работает отлично. –

Смежные вопросы