屏幕更换后Jpanel没有刷新

时间:2013-11-21 18:16:55

标签: java swing jpanel keylistener thread-sleep

当我点击播放时,我正在创建一个链接到我的游戏的菜单。

 package GameScreen;

 import java.awt.Graphics; import java.awt.Rectangle; import
 java.awt.event.MouseEvent; import java.awt.event.MouseListener; import
 javax.swing.ImageIcon; import javax.swing.JComponent;

 public class MainMenu  extends JComponent
         implements MouseListener 
{ 
 private GameScreen PlayGame; private ImageIcon bg = new
 ImageIcon(getClass().getResource("bg.jpg")); private ImageIcon
 ButtonPlay = new ImageIcon(getClass().getResource("button.jpg"));
 private ImageIcon ButtonOptions = new
 ImageIcon(getClass().getResource("button2.jpg")); private ImageIcon
 ButtonHS = new ImageIcon(getClass().getResource("button3.jpg"));
 private ImageIcon ButtonQuit = new
 ImageIcon(getClass().getResource("button4.jpg")); private Rectangle
 Boundsplay = new Rectangle (340, 250, 400, 100); private Rectangle
 BoundsOptions = new Rectangle (340, 350, 400, 100); private Rectangle
 BoundsHS = new Rectangle (340, 450, 400, 100); private Rectangle
 BoundsQuit = new Rectangle (340, 550, 400, 100);

     public MainMenu(GameScreen Play)
     {
         PlayGame = Play;
         setSize (1024,768);
     }

     public void paint(Graphics g)
     {
         g.drawImage(bg.getImage(), 0, 0, 1024, 768, null);        
         g.drawImage(ButtonPlay.getImage(), 340, 250, 400, 100, null);
         g.drawImage(ButtonOptions.getImage(),340, 350, 400, 100, null);
         g.drawImage(ButtonHS.getImage(),340, 450, 400, 100, null);
         g.drawImage(ButtonQuit.getImage(),340, 550, 400, 100, null);
         addMouseListener(this);
     }

     @Override
     public void mouseClicked(MouseEvent e) {

     }

     @Override
     public void mousePressed(MouseEvent e) {

     }

     @Override
     public void mouseReleased(MouseEvent e) {
     if(Boundsplay.contains(e.getX(), e.getY()))    {
                     PlayGame.PlayGame();
    }
     if(BoundsOptions.contains(e.getX(), e.getY()))     {
               System.out.println("hit options");   }
     if(BoundsHS.contains(e.getX(), e.getY()))  {
               System.out.println("hit HS");    }
     if(BoundsQuit.contains(e.getX(), e.getY()))    {
               System.out.println("hit Quit");  }

     }

     @Override
     public void mouseEntered(MouseEvent e) {

     }

     @Override
     public void mouseExited(MouseEvent e) {

     }

 }

正如你所看到的,打击游戏将调用此PlayGame.PlayGame();这将工作但它不会刷新,所以它将保留在菜单上,直到我最小化并打开或调整面板大小以强制它刷新。 任何想法我怎么能让它刷新?或者某些错误导致它不能刷新?

以下是从中调用游戏方法的类。

package GameScreen;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;





public class GameScreen 
extends JFrame
implements Runnable, KeyListener
{
private Thread animation = new Thread(this);
private AniScreen aniscreen = new AniScreen();;
private MainMenu mainmenu = new MainMenu(this);;
private HighScores highscores = new HighScores();


public GameScreen()
{
   super("Zombie defense!");
   setBounds(20,20,1024,768);
   setContentPane (mainmenu);
   //setContentPane (highscores);
   animation.start();
   addKeyListener(this);
   setVisible (true);
   this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
}

public void PlayGame()
{
    setContentPane (aniscreen);

}

public static void main(String[] args)
   {
       GameScreen gs = new GameScreen();
   }

   @Override
   public void run()
   {
      while (true)
      {
            aniscreen.repaint();
            mainmenu.repaint();
            highscores.repaint();

         try
         {
            Thread.sleep(30);
         }
         catch(InterruptedException ex)
            {

            }
      }

   }

   @Override
   public void keyTyped(KeyEvent e)
   {
         }

   @Override
   public void keyPressed(KeyEvent e)
   {
      if (e.getKeyCode()==KeyEvent.VK_DOWN)
      {
        aniscreen.moveplayer(0,20);
      }
       if (e.getKeyCode()==KeyEvent.VK_UP)
      {
        aniscreen.moveplayer(0,-20);

      }

   }

   @Override
   public void keyReleased(KeyEvent e)
   {
      aniscreen.moveplayer(0,0);
   }
}

一如既往地感谢您提出的任何帮助或想法, 谢谢!

1 个答案:

答案 0 :(得分:2)

你有没有尝试过

panel.repaint();
panel.revalidate();

在您调用PlayGame();方法后?