问题描述
packagecom.test;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.*;publicclassMyTankGameextendsJFrameimplementsActionListener{JMenuBarjmb;//一个菜单条,两个菜单,两个菜单项JMenujm2,jm1;JMenuItemjmi1,jmi2;MyStarPanelmsp=null;GamePanelgp;Threadt0;publicMyTankGame(){jmb=newJMenuBar();jm2=newJMenu("帮助");jm1=newJMenu("游戏");jmi1=newJMenuItem("开始");jmi2=newJMenuItem("结束");super.setLayout(null);jm1.add(jmi1);jmi1.addActionListener(this);jmi1.setActionCommand("new");jmi2.addActionListener(this);jmi2.setActionCommand("exit");jm1.add(jmi2);jmb.add(jm1);jmb.add(jm2);this.setJMenuBar(jmb);//创建打开时的界面msp=newMyStarPanel();Threadt0=newThread(msp);t0.start();this.add(msp);this.setLocation(0,0);this.setSize(600,450);this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);this.setVisible(true);}publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(newRunnable(){publicvoidrun(){newMyTankGame();}});}publicvoidactionPerformed(ActionEventarg0){if(arg0.getActionCommand().equals("new")){//点解开始菜单的new,开始的删去开始时界面,新建一个界面。MyStarPanel.isfront=false;msp.repaint();this.remove(msp);msp=null;t0=null;jmi2.setEnabled(true);jmi1.setEnabled(false);this.validate();this.repaint();GamePanelgp=newGamePanel();Threadt0=newThread(gp);SwingUtilities.invokeLater(t0);System.out.println("111"+Thread.currentThread().getName());this.add(gp);this.validate();this.repaint();}elseif(arg0.getActionCommand().equals("exit")){System.exit(0);}}}classMyStarPanelextendsJPanelimplementsRunnable{inttimes=0;publicstaticbooleanisfront=true;staticintbz=0;publicMyStarPanel(){super();this.setSize(450,350);this.setBackground(Color.blue);System.out.println("color=blue");}publicvoidpaintComponent(Graphicsg){super.paintComponent(g);g.setColor(Color.red);g.fillRect(50,50,300,200);if(times%2==0){g.setColor(Color.yellow);Fontmyfont=newFont("华文行楷",Font.BOLD,30);g.setFont(myfont);if(bz==0){g.drawString("stage:"+1,150,150);}else{g.drawString("英雄败北",150,150);}}else{}}publicvoidrun(){//TODOAuto-generatedmethodstubwhile(isfront){try{Thread.sleep(100);}catch(Exceptione){e.printStackTrace();//TODO:handleexception}times++;this.repaint();}}}classGamePanelextendsJPanelimplementsRunnable{//Herohero=newHero(200,200,0);publicGamePanel(){super();//this.setSize(400,300);this.setBounds(0,0,400,300);this.setBackground(Color.green);System.out.println("color=green");}publicvoidpaint(Graphicsg){drawTank(200,200,g,0,1);}publicvoiddrawTank(intx,inty,Graphicsg,intdirect,inttype){//判断什么类型的坦克switch(type){case0:g.setColor(Color.cyan);break;case1:g.setColor(Color.yellow);break;}//判断方向switch(direct){case0://1.画出上边的矩形g.fill3DRect(x-15,y-10,30,5,false);//2.画出下边的矩形g.fill3DRect(x-15,y+5,30,5,false);//3.画出中间矩形g.fill3DRect(x-10,y-5,20,10,false);//4.画出圆形g.fillOval(x-5,y-5,10,10);//5.画出线g.drawLine(x,y,x-15,y);break;}}@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubwhile(true){try{Thread.sleep(100);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();System.out.println("gm.runiserro.");}this.repaint();}}}我在主窗口上,添加一个菜单栏和jpanel1。在这个界面上,点击菜单:游戏--开始。理论上是删除jpanel1,然后再添加一个jpanel2并在上面画一个图形。结果是要画的图形显示出来了,但主窗口上的菜单和关闭都不能用了,并且我设置的jpanel2为绿色也没有显示出来,显示的是白色。希望大神给指点一下,应该是我的理解有问题!!!
解决方案
解决方案二:
仅供参考:1)将SwingUtilities.invokeLater(t0);改为t0.start();解决按键不能响应问题2)在GamePanel类重写的publicvoidpaint(Graphicsg)方法中首行添加super.paint(g);解决背景颜色问题