2013-12-13 18 views
1

嗨我是一個新的Java和我創建這個模式遊戲。我想讓程序做的是在4x4網格上顯示一個圖案,然後等待幾秒鐘讓用戶記住圖案,然後顯示一個空白輸入屏幕供用戶輸入圖案。我遇到的問題是JFrame出現應該有模式,但JFrame顯示一個空白屏幕等待,然後顯示空白輸入屏幕。提前致謝。無法讓我的程序等待給我的白色屏幕! Java

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 menu.menu; 

public class Game 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 void setup() { 
     menu.close(); 
     gf.dispose(); 
     gf = new JFrame(); 
     gf.setLocation(300,100); 
     gf.setSize(size); 
     gf.setResizable(false); 
     gf.setLayout(Ggrid); 
     gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     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); 
      } 
     } 
    } 

    public static int flag =0; 
    public static void gameStart() 
    { 

     getpattern(); 
     try 
     { 
      t.sleep(3000); 
     } 
     catch (Exception e) 
     { 

     } 

     setup(); 
     blank(); 
     } 
    public static 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"); 
        gf.add(button); 
        } 
        else 
        { 
         JButton button = new JButton(""); 
         gf.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"); 
        gf.add(button); 
        } 
        else 
        { 
         JButton button = new JButton(""); 
         gf.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"); 
       gf.add(button); 
       } 
       else 
       { 
        JButton button = new JButton(""); 
        gf.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"); 
       gf.add(button); 
       } 
       else 
       { 
        JButton button = new JButton(""); 
        gf.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"); 
       gf.add(button); 
       } 
       else 
       { 
        JButton button = new JButton(""); 
        gf.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"); 
       gf.add(button); 
       } 
       else 
       { 
        JButton button = new JButton(""); 
        gf.add(button); 
       } 
      } 
     } 
     } 
     gf.setVisible(true); 
    } 

    public static Thread t = new Thread(); 

    @Override 
    public void run() { 

     t=new Thread(this); 
     t.start(); 
    } 
} 

回答

0

所以基本上不工作是

public static void getpattern() 
    { 
     if (order == 1) 
      { 

...

   if (handlebars[a][b] == 1) 

更新

在遊戲開始調用了Thread.sleep(3000);並打印出任何例外。

如果您註釋掉

setup(); 
blank(); 

不會電網正確顯示?

+0

把手,ys,飛船,羊羣,百分比,方形都在這個類實現的另一個類「屬性」中初始化 –

+0

你的'getpattern()'方法只會被調用'order == 1' –

+0

計劃後人得到它會做的模式'順序++;'然後顯示下一個模式 –

相關問題