嗨,我对java有点新,我正在创建这个模式游戏。我希望程序做的是在4x4网格上显示一个模式然后等待几秒钟让用户记住模式然后显示一个空白输入屏幕供用户输入模式。我遇到的问题是JFrame出现应该有模式但JFrame显示空白屏幕等待然后显示空白输入屏幕。提前谢谢。
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 :(得分:0)
所以基本上没有用的是
public static void getpattern()
{
if (order == 1)
{
...
if (handlebars[a][b] == 1)
<强>更新强>
在游戏中开始调用Thread.sleep(3000);并打印出任何例外。
如果你发表评论
setup();
blank();
网格是否正确显示?