2017-03-25 3 views
0

Le jeu que je suis en train de créer consiste à changer de panneau après avoir cliqué sur le bouton Démarrer lorsque startGame() est appelé. Le programme se fige une fois que le bouton est cliqué et ne fait rien.Impossible de changer de panneau pour mon jeu

est ici extrait de code:

public class ArtilleryGame extends JFrame { 
    private static final long serialVersionUID = 1L; 
    //TODO: fix clouds from updating with background 

    static StartPanel startPanel = new StartPanel(); 
    public ArtilleryGame() throws InterruptedException 
    { 
     setSize(898,685); 
     setLocationRelativeTo(null); 
     setTitle("Tank Game"); 
     setResizable(false); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     URL sound = this.getClass().getResource("Sounds/Intro_Screen.wav"); 

     AudioClip drop = Applet.newAudioClip(sound); 
     drop.loop(); 
     drop.play(); 

     add(startPanel); 
     //add(new GamePanel()); 
     buttons(); 

     setVisible(true); 
    } 
    private void buttons() 
    { 
     URL startURL = this.getClass().getResource("imgg/StartBtn.png"); 
     BufferedImage startI = new BufferedImage(170, 62, BufferedImage.TYPE_INT_RGB); 
     try 
     { 
      startI = ImageIO.read(startURL); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     ImageIcon startImg = new ImageIcon(startI); 

     JButton startBtn = new JButton(startImg); 
     startBtn.setSize(170, 62); 
     startBtn.setBorder(BorderFactory.createEmptyBorder()); 
     startBtn.addActionListener(new ActionListener() 
     { 

      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       startGame(); 
      } 
     }); 

     startBtn.setLocation(365, 310); 
     StartPanel.background.add(startBtn); 
    } 

    public void startGame() 
    { 
     remove(startPanel); 
     add(new GamePanel()); 
    } 

    public static void displayCredits() 
    { 
     JOptionPane.showMessageDialog(null, new CreditsPanel(), "Tank Game", JOptionPane.PLAIN_MESSAGE); 
    } 
    public static void main(String[] args) throws InterruptedException 
    { 
     new ArtilleryGame(); 
     //displayCredits(); 
    } 
} 

Répondre

0

Utilisez un CardLayout. Le CardLayout vous permet d'échanger facilement des panneaux.

Lire la section du tutoriel Swing sur How to Use CardLayout pour plus d'informations et des exemples de travail.

+0

merci beaucoup –