2017-03-25 3 views
0

Das Spiel, das ich baue, beinhaltet das Wechseln von Panels, nachdem der Startknopf geklickt wurde, wenn startGame() aufgerufen wird. Das Programm friert ein, sobald der Button angeklickt wird und tut nichts.kann Schalttafeln für mein Spiel nicht wechseln

Hier ist Code-Schnipsel:

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(); 
    } 
} 

Antwort

0

ein CardLayout verwenden. Mit der CardLayout können Sie Panels einfach austauschen.

Lesen Sie den Abschnitt aus dem Swing-Tutorial auf How to Use CardLayout für weitere Informationen und Arbeitsbeispiele.

+0

vielen Dank –

Verwandte Themen