面板不会打开?

时间:2017-05-23 18:07:35

标签: java graphics jpanel listener jbutton

我正在做一个pacman游戏。计划是有一个显示欢迎页面的GUI和一个显示“播放”的JButton。在用户按下“播放”按钮后,它将打开一个新面板,该面板将包含由指令组成的图像。在说明面板上,将有一个JButton,显示“继续”。这将使用户使用四个JButton读取“Easy”,“Medium”,“Hard”和“QUIT”的GUI。这些按钮的actionPerformed方法已经编码并且工作正常(一个新的JFrame将打开,相应的Panel到每个级别,QUIT将退出程序)。

我创建它的方式是我创建了一个名为GUIPanel的类,并且在实例化GUIPanel对象时将打开所有面板。

在我用级别完成GUI之后,当我决定添加Welcome面板和Instructions面板时。到目前为止,我已经为欢迎面板编写了代码。我将它添加到级别按钮的代码上方。

我的问题是每当我运行它时,都会创建一个框架,但没有面板。请帮我!!这个项目明天就是这样。

//*****************************
//   GUI : Panel
//
//   Updated : May 4th, 2017
//
//*****************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

public class GUIPanel extends JPanel
{
   private Graphics g;
   private BufferedImage buffer;
   public GUIPanel()
   {
      try{
   //welcome panel
      JPanel welcome = new JPanel();
      ImageIcon title = new ImageIcon("welcome.png");
      buffer =  new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
      g = buffer.getGraphics();
      g.drawImage(title.getImage(), 400, 400, 200, 200, null);
   //add button and listener and add onto welcome (JPanel)
      JButton play = new JButton("Play");
      play.addActionListener(new PlayListener());
      welcome.add(play);
      }
      catch(Exception e){
      }
   }

   private class PlayListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
      try{
      JFrame playFrame = new JFrame();
      //creating the panel
         JPanel panel = new JPanel();
         add(panel);
      /*creating the Easy button which will allow the
        user to play the EASY LEVEL of Karelman */
         JButton easyButton = new JButton("Easy");
         easyButton.setBackground(Color.BLUE);
         easyButton.setForeground(Color.WHITE);
         easyButton.addActionListener(new EasyListener());
         panel.add(easyButton);
      /*creating the Medium button which will allow the
        user to play the MEDIUM LEVEL of Karelman */
         JButton mediumButton = new JButton("Medium");
         mediumButton.setBackground(Color.BLUE);
         mediumButton.setForeground(Color.WHITE);
         mediumButton.addActionListener(new MediumListener());
         panel.add(mediumButton);
      /*creating the random button which will allow the
        user to play the HARD LEVEL of Karelman*/   
         JButton hardButton = new JButton("Hard");
         hardButton.setBackground(Color.BLUE);
         hardButton.setForeground(Color.WHITE);
         hardButton.addActionListener(new HardListener());
         hardButton.setBounds(40, 100, 100, 60);
         panel.add(hardButton);
      /*creating the quit button for exiting the program*/
         JButton quitButton = new JButton("QUIT");
         quitButton.setBackground(Color.BLUE);
         quitButton.setForeground(Color.WHITE);
         quitButton.addActionListener(new QuitListener());
         quitButton.setBounds(150, 300, 25, 25);
         panel.add(quitButton);
         }
         catch(Exception i){
         }
      }
   }

   /*
      EasyListener is attached to the Easy Button
      It will open the Easy Level by creating a
      new frame which will use Easy Ghosts.
   */
   private class EasyListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         try{

            JFrame frame = new JFrame("PLAY KARELMAN -- EASY");
            frame.setSize(708, 738);
            frame.setLocation(350, 0);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
            frame.setVisible(true);
         //create panel for Easy Level within frame      
            EasyPanel easy = new EasyPanel();
            frame.setContentPane(easy);
         }
         catch(IOException i){
         }

      }
   }

   /*
      MediumListener is attached to the Medium Button
      It will open the Medium Level by creating a
      new frame which will use Medium Ghosts.
   */
   private class MediumListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         try{
            JFrame frame = new JFrame("PLAY KARELMAN -- MEDIUM");
            frame.setSize(708, 738);
            frame.setLocation(350, 0);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
            frame.setVisible(true);
         //create panel for Medium Level within frame      
            MediumPanel medium = new MediumPanel();
            frame.setContentPane(medium);
         }
         catch(IOException i){
         }
      }
   }
   /*
      HardListener is attached to the Hard Button
      It will open the Hard Level by creating a
      new frame which will use Hard Ghosts.
   */
   private class HardListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         try{
            JFrame frame = new JFrame("PLAY KARELMAN -- HARD");
            frame.setSize(708, 738);
            frame.setLocation(350, 0);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
            frame.setVisible(true);
         //create panel for Hard Level within frame      
            HardPanel hard = new HardPanel();
            frame.setContentPane(hard);
         }
         catch(IOException i){
         }
      }
   }
   /*
      QuitListener is attached to the Quit Button
      It will make a system call to exit the program.
   */
   private class QuitListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {

         System.exit(0);      

      }
   }
}

0 个答案:

没有答案