package Practice;
public class GameMain {
public static void main(String [] args){
new GameFrame ();
}
}
package Practice;
public class Constant {
/*******************游戏窗口*********************/
public static final String Game_tanke="坦克大战";
public static final int Frame_width=900;
public static final int Frame_heigh=700;
public static final int Frame_x=1920-900>>1;
public static final int Frame_y=1080-900>>1;
/*******************游戏菜单*********************/
public static final int STATE_MENU=0;
public static final int STATE_HELP=1;
public static final int STATE_ABOUT=2;
public static final int STATE_RUN =3;
public static final int STATE_OVER=4;
public static final String[] MENUS={
"开始游戏",
"游戏帮助",
"游戏关于",
"退出游戏",
};
}
package Practice;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import static Practice.Constant.*;
public class GameFrame extends Frame {
//游戏状态
public static int gameState;
//对窗口进行初始化
public GameFrame() {
initFrame ();
initEventListen ();
}
//对游戏进行初始化
private void initGame(){
gameState=STATE_MENU;
}
//属性进行初始化
public void initFrame(){
setTitle (Game_tanke);//标题
setVisible (true);//显示窗口
setSize (Frame_width,Frame_width);//设置宽高
setLocation (Frame_x,Frame_y);//窗口位置
setResizable (false);//窗口大小不可改变
repaint ();
}
//窗口事件监听
private void initEventListen(){
//注册监听事件
addWindowListener (new WindowAdapter () {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.exit (0);
}
});
}
//是FRAME类的方法,所有需要在屏幕中现实的内容都要通过该方法调用,不能主动调用,需要通过repaint()去调用该方法
public void update(Graphics g) {
switch (gameState){
case STATE_MENU:
drawMenu (g);
break;
case STATE_HELP:
drawHelp (g);
break;
case STATE_ABOUT:
drawAbout (g);
break;
case STATE_RUN:
drawRun (g);
break;
case STATE_OVER:
drawOver (g);
break;
}
}
private void drawRun(Graphics g) {
}
private void drawOver(Graphics g) {
}
private void drawAbout(Graphics g) {
}
private void drawHelp(Graphics g) {
}
//绘制菜单状态下内容
private void drawMenu(Graphics g){
g.setColor (Color.BLACK);
g.fillRect (0,0,Frame_width,Frame_heigh);
final int STR_width=50;
int x=Frame_width-STR_width>>1;
int y=Frame_heigh/3;
final int DIS=30;
g.setColor (Color.white);
for (int i=0;i<MENUS.length;i++){
g.drawString (MENUS[i],x,y+DIS*i );
}
}
}
这个代码为什么我的运行出来的窗口闪一下就白屏了,求解