2013-01-24 194 views
1

我正在研究這個基本的Pong-style遊戲,但我在GUI方面遇到了一些困難。基本上我有一個叫做窗口的JFrame,它包含了所有內容,還有一個叫做內容的JPanel。內容應該是指我擁有的ContentPanel。這是我的源代碼到目前爲止(對不起,這是很長的時間,很多它只涉及遊戲,而不是這個問題,但我不確定我的問題需要源代碼的哪些部分是有意義的):如何更改JFrame內部的JPanel?

import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList; 
import javax.swing.*; 

public class Pong extends JApplet { 


    public static void main(String[] args) { 
     JFrame window = new JFrame("Pong: The Game of Champions"); 
     JPanel content = new JPanel(); 
     window.setContentPane(content); 
     window.setSize(1000,600); 
     window.setLocation(100,100); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setVisible(true); 
     window.setResizable(false); 

     JMenuBar settings = new JMenuBar(); 
     ButtonHandler listener = new ButtonHandler(); 
     window.setJMenuBar(settings); 
     JMenu toolsMenu = new JMenu("Settings"); 
     settings.add(toolsMenu); 
     JMenuItem preferences = new JMenuItem("Preferences"); 
     JMenuItem about = new JMenuItem("About"); 
     toolsMenu.add(preferences); 
     toolsMenu.add(about);  
     about.addActionListener(listener); 
     preferences.addActionListener(listener); 

    } 

    public void init() { 
     setContentPane(new ContentPanel()); 
    } 

    public static class ButtonHandler implements ActionListener { 
     public void actionPerformed(ActionEvent evt) { 

      String command = evt.getActionCommand(); 

      if (command.equals("About")) { 
       JOptionPane.showMessageDialog(null, "Made by Kyle Evans for Mr. Richardson's AP Computer Science class. Aww yeah."); 
      } 

      else if (command.equals("Preferences")) { 

      } 
     } 
    } 

    public static class ContentPanel extends JPanel implements KeyListener, FocusListener, MouseListener, ActionListener { 

     private Timer timer = new Timer(10, this); 
     private static final int PADDLE_WIDTH = 10; // Length of side of square. 
     private static final int PADDLE_HEIGHT = 100; 
     private static final int BALL_WIDTH = 10; 
     private static final int UP = 1, DOWN = 2; 

     private Color squareColor; // The color of the square. 

     private int paddleOneDirection = -1; 
     private int paddleTwoDirection = -1; 

     private int paddleOneTop, paddleOneLeft, paddleTwoTop, paddleTwoLeft, paddleSpeed, 
          ballTop, ballLeft, ballSpeedHorizontal, ballSpeedVertical; 



     public ContentPanel() { 

     paddleOneTop = 230; // Initial position of top-left corner of square. 
     paddleOneLeft = 900; 
     paddleTwoTop = 230; 
     paddleTwoLeft = 90; 
     paddleSpeed = 4; 
     ballTop = 270; 
     ballLeft = 490; 
     ballSpeedHorizontal = (int)(Math.random()*2); 
      if (ballSpeedHorizontal == 0) { 
       ballSpeedHorizontal = -1; 
      } 
     ballSpeedVertical = (int)(Math.random()*6) - 3; 
     squareColor = Color.WHITE; // Initial color of square. 

     setBackground(Color.BLACK); 

     addKeyListener(this);  // Set up event listening. 
     addFocusListener(this); 
     addMouseListener(this); 

     } // end init(); 

     public void paintComponent(Graphics g) { 

     super.paintComponent(g); // Fills the panel with its 
            // background color, which is white. 

     g.setColor(squareColor); 
     g.fillRect(paddleOneLeft, paddleOneTop, PADDLE_WIDTH, PADDLE_HEIGHT); 
     g.fillRect(paddleTwoLeft, paddleTwoTop, PADDLE_WIDTH, PADDLE_HEIGHT); 
     g.fillRect(ballLeft, ballTop, BALL_WIDTH, BALL_WIDTH); 
     String messageH = "false"; 
     String messageV1 = "false"; 
     String messageV2 = "false"; 
     if (isHittingPaddleHorizontal()) { 
      messageH = "true"; 
     } 
     if (!isHittingPaddleHorizontal()) { 
      messageH = "false"; 
     } 
     if (isHittingPaddleOneVertical()) { 
      messageV1 = "true"; 
     } 
     if (!isHittingPaddleOneVertical()) { 
      messageV1 = "false"; 
     } 
     if (isHittingPaddleTwoVertical()) { 
      messageV2 = "true"; 
     } 
     if (!isHittingPaddleTwoVertical()) { 
      messageV2 = "false"; 
     } 
     g.drawString("Is hitting horizontal: " + messageH,7,20); 
     g.drawString("Is hitting vertical right: " + messageV1,7,40); 
     g.drawString("Is hitting vertical left: " + messageV2,7,60); 

     } // end paintComponent() 

     // ------------------- Game controlling methods ------------------------- 

     public void moveBall() { 

      ballLeft -= ballSpeedHorizontal; 
      ballTop -= ballSpeedVertical; 

      if (isHittingPaddleHorizontal()) { 
       if (isHittingPaddleOneVertical()) { 
         ballSpeedHorizontal = -ballSpeedHorizontal; 
        // ballSpeedHorizontal++; 
       } 
       else if (isHittingPaddleTwoVertical()) { 
        // ballSpeedHorizontal++; 
         ballSpeedHorizontal = -ballSpeedHorizontal; 
       } 
      } 

      if ((ballTop <= 0) || (ballTop >= 565))  // Sets the vertical boundaries so the ball bounces off the top and bottom. 
       ballSpeedVertical = -ballSpeedVertical; 

      if ((ballLeft > 1000) || ballLeft < 0) { 
       gameOver(); 
      } 
     } 

     public boolean isHittingPaddleOneVertical() { 
      for (int i = 0; i < PADDLE_HEIGHT + (2 * BALL_WIDTH); i++) { 
       if (ballTop == (paddleOneTop - BALL_WIDTH + i)) { 
        return true; 
       } 
      } 
      return false; 
     } 

     public boolean isHittingPaddleTwoVertical() { 
      for (int i = 0; i < PADDLE_HEIGHT + (2 * BALL_WIDTH); i++) { 
       if (ballTop == (paddleTwoTop - BALL_WIDTH + i)) { 
        return true; 
       } 
      } 
      return false; 
     } 

     public boolean isHittingPaddleHorizontal() { 
      for (int i = 0; i < 50; i++) { 
       if ((ballLeft == paddleOneLeft + i) || (ballLeft == paddleTwoLeft + PADDLE_WIDTH - i)) { 
        return true; 
       } 
      } 
      return false; 
     } 

     public void robotControl() { 
      paddleTwoTop = ballTop - 45; 
     } 

     public void gameOver() { 
      paddleOneTop = 230; 
      paddleTwoTop = 230; 
      ballTop = 270; 
      ballLeft = 490; 
      ballSpeedHorizontal = (int)(Math.random()*2); 
      if (ballSpeedHorizontal == 0) { 
       ballSpeedHorizontal = -1; 
      } 
      ballSpeedVertical = (int)(Math.random()*6) - 3; 
     } 

     // ------------------- Event handling methods ---------------------- 

     public void focusGained(FocusEvent evt) { 
     repaint(); // redraw with cyan border 
     } 

     public void focusLost(FocusEvent evt) { 
     repaint(); // redraw without cyan border 
     } 

     public void keyTyped(KeyEvent evt) { 

     char ch = evt.getKeyChar(); // The character typed. 

     } // end keyTyped() 

     public void keyPressed(KeyEvent evt) { 

     int key = evt.getKeyCode(); // keyboard code for the pressed key 

     timer.start(); 

     if (key == KeyEvent.VK_UP) { // up arrow key 
      paddleOneDirection = UP; 
     } 
     else if (key == KeyEvent.VK_DOWN) { // down arrow key 
      paddleOneDirection = DOWN; 
     } 
     else { 
      paddleOneDirection = -1; // This prevents other keys from interfering with the paddle movement. 
     } 
     if (key == KeyEvent.VK_W) { 
      paddleTwoDirection = UP; 
     } 
     else if (key == KeyEvent.VK_S) { 
      paddleTwoDirection = DOWN; 
     } 
     else { 
      paddleTwoDirection = -1; 
     } 

     if ((paddleOneTop > 1000 - PADDLE_HEIGHT)) 
      paddleOneTop = 1000 - PADDLE_HEIGHT; 
     if (paddleTwoTop > 1000 - PADDLE_HEIGHT) 
      paddleTwoTop = 1000 - PADDLE_HEIGHT; 


     } // end keyPressed() 

     public void keyReleased(KeyEvent evt) { 
      paddleOneDirection = -1; 
      paddleTwoDirection = -1; 
     } 

     public void mousePressed(MouseEvent evt) { 
     requestFocus(); 
     } 

     public void mouseEntered(MouseEvent evt) { } // Required by the 
     public void mouseExited(MouseEvent evt) { } // MouseListener 
     public void mouseReleased(MouseEvent evt) { } //  interface. 
     public void mouseClicked(MouseEvent evt) { } 
     public void actionPerformed(ActionEvent evt) { 


      switch (paddleOneDirection) { 
      case UP: 
       paddleOneTop -= paddleSpeed; 
       break; 
      case DOWN: 
       paddleOneTop += paddleSpeed; 
       break; 
      } 
      switch (paddleTwoDirection) { 
      case UP: 
       paddleTwoTop -= paddleSpeed; 
       break; 
      case DOWN: 
       paddleTwoTop += paddleSpeed; 
       break; 
      } 
      robotControl(); 
      moveBall(); 
      repaint(); 
     } 
    } // end nested class ContentPanel 


} // end class 

(代碼是有點亂,我很抱歉,我很新的Java和總體規劃。)

這裏是我的問題:

  1. 如何初始化JFrame默認顯示JPanel內容?

  2. 如何開始創建一個可以在兩者之間切換的單獨的JPanel?我的最終目標是在用戶單擊「首選項」時單獨顯示JPanel,然後能夠在首選項JPanel和內容JPanel之間來回切換。

我將不勝感激任何幫助。

+0

嗯,因爲我看到它的遊戲,除非你只能使用Java ...看看['Slick2D'](http://www.slick2d.org/),你將擁有一個偉大的遊戲與好處的OpenGL(因此你也需要[LWGL](http://www.lwjgl.org),但這只是一個簡單的附加庫),以及一個易於使用的庫,內置了大多數所需的遊戲功能,例如音樂,遊戲循環,精靈動畫等。這也使得融入不會像[JBox2D](http://www.jbox2d.org/)或[Dyn4j](http://www.dyn4j.org/) - 我更喜歡後者。 –

回答

7

JFrame創建並初始化爲JFrame不同的內容的面板並將它們放在CardLayout。必要時切換卡片以顯示所需的面板。

+3

E.G. [這個答案](http://stackoverflow.com/a/14493395/418556)。它可以每秒處理150張卡片。 ;) –

+0

嗯,好吧。因此,在我的程序實例中,如果我有JPanel調用的內容,那麼我如何初始化JFrame以默認顯示JPanel? –

+0

@KyleEvans所以,frame.add(面板)有什麼問題? – MadProgrammer