2015-05-14 60 views
0

所以我有這個項目,我正在研究用戶將在哪裏選擇5場比賽。每當我點擊一個按鈕來玩這個遊戲,它都不會加載遊戲。請提前幫助感謝!按下按鈕時,JFrame不會加載新的JFrame

主要JFrame類:

import javax.swing.UIManager.LookAndFeelInfo; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseWheelEvent; 
import java.awt.event.MouseWheelListener; 
import javax.swing.border.Border; 
import java.io.RandomAccessFile; 
import javax.swing.*; 
import java.awt.BorderLayout; 
import java.io.IOException; 
import java.util.*; 
import java.io.*; 
public class JavaGames extends JFrame 
{ 
    private JMenuBar menuBar; 
    public static int height; 
    public static int width; 
    private JTextField test; 
    private JButton BrickBreaker; 
    private JButton Exit; 
    private JButton Trapped; 
    private JButton PacMan; 
    private JButton PinBall; 
    private JButton Pong; 
    private JTextField textfield1; 
    private JPanel contentPane; 
    public JavaGames() throws IOException 
    { 
     this.setTitle("Application Project"); 
     this.setSize(449,350); 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     height = screenSize.height; 
     width = screenSize.width; 
     generateMenu(); 
     this.setJMenuBar(menuBar); 
     contentPane = new JPanel(null); 
     contentPane.setPreferredSize(new Dimension(500,400)); 
     contentPane.setBackground(new Color(192,192,192)); 
     BrickBreaker = new JButton(); 
     BrickBreaker.setBounds(108,69,95,35); 
     BrickBreaker.setBackground(new Color(214,217,223)); 
     BrickBreaker.setForeground(new Color(0,0,0)); 
     BrickBreaker.setEnabled(true); 
     BrickBreaker.setFont(new Font("SansSerif",0,11)); 
     BrickBreaker.setText("Brick Breaker"); 
     BrickBreaker.setVisible(true); 
     BrickBreaker.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       BrickBreaker(evt); 
      } 
     }); 
     Exit = new JButton(); 
     Exit.setBounds(219,175,95,35); 
     Exit.setBackground(new Color(214,217,223)); 
     Exit.setForeground(new Color(0,0,0)); 
     Exit.setEnabled(true); 
     Exit.setFont(new Font("sansserif",0,12)); 
     Exit.setText("Exit"); 
     Exit.setVisible(true); 
     Exit.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       Exit(evt); 
      } 
     }); 
     Trapped = new JButton(); 
     Trapped.setBounds(219,69,95,35); 
     Trapped.setBackground(new Color(214,217,223)); 
     Trapped.setForeground(new Color(0,0,0)); 
     Trapped.setEnabled(true); 
     Trapped.setFont(new Font("SansSerif",0,12)); 
     Trapped.setText("Trapped"); 
     Trapped.setVisible(true); 
     Trapped.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       Trapped(evt); 
      } 
     }); 
     PacMan = new JButton(); 
     PacMan.setBounds(219,120,95,35); 
     PacMan.setBackground(new Color(214,217,223)); 
     PacMan.setForeground(new Color(0,0,0)); 
     PacMan.setEnabled(true); 
     PacMan.setFont(new Font("SansSerif",0,11)); 
     PacMan.setText("Pac Man Demo"); 
     PacMan.setVisible(true); 
     PacMan.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       PacMan(evt); 
      } 
     }); 
     PinBall = new JButton(); 
     PinBall.setBounds(108,175,95,35); 
     PinBall.setBackground(new Color(214,217,223)); 
     PinBall.setForeground(new Color(0,0,0)); 
     PinBall.setEnabled(true); 
     PinBall.setFont(new Font("sansserif",0,12)); 
     PinBall.setText("Pin Ball"); 
     PinBall.setVisible(true); 
     PinBall.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) 
      { 
       try{ 
       PinBall(evt); 
       }catch(IOException io) {} 
      } 
     }); 
     Pong = new JButton(); 
     Pong.setBounds(108,120,95,35); 
     Pong.setBackground(new Color(214,217,223)); 
     Pong.setForeground(new Color(0,0,0)); 
     Pong.setEnabled(true); 
     Pong.setFont(new Font("sansserif",0,12)); 
     Pong.setText("Pong"); 
     Pong.setVisible(true); 
     Pong.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       Pong(evt); 
      } 
     }); 
     textfield1 = new JTextField(); 
     textfield1.setBounds(160,12,100,49); 
     textfield1.setBackground(new Color(255,255,255)); 
     textfield1.setForeground(new Color(0,0,0)); 
     textfield1.setEnabled(false); 
     textfield1.setFont(new Font("SansSerif",0,18)); 
     textfield1.setText("Welcome!"); 
     textfield1.setVisible(true); 
     contentPane.add(BrickBreaker); 
     contentPane.add(Exit); 
     contentPane.add(Trapped); 
     contentPane.add(PacMan); 
     contentPane.add(PinBall); 
     contentPane.add(Pong); 
     contentPane.add(textfield1); 
     this.add(contentPane); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 
     this.pack(); 
     this.setVisible(true); 
    } 
    public static String[] q = new String[1]; 
    private void BrickBreaker(ActionEvent evt) 
    { 
     BrickGUI temp = new BrickGUI(); 
     temp.main(q); 
    } 
    private void Exit (ActionEvent evt) 
    { 
     int n=0; 
     System.exit(n); 
    } 
    private void Trapped(ActionEvent evt) 
    { 
     TrappedGUI run = new TrappedGUI(); 
     run.main(q); 
    } 
    private void PacMan (ActionEvent evt) 
    { 
     PacManGUI run = new PacManGUI(); 
     run.main(q); 
    } 
    private void PinBall (ActionEvent evt) throws IOException 
    { 
     PinBallGUI run = new PinBallGUI(); 
     run.main(q); 
    } 
    private void Pong (ActionEvent evt) 
    { 
     PongGUI run = new PongGUI(); 
     run.main(q); 
    } 
    public void generateMenu() 
    { 
     menuBar = new JMenuBar(); 
     JMenu file = new JMenu("File"); 
     JMenu tools = new JMenu("Tools"); 
     JMenuItem openHS = new JMenuItem("High Scores"); 
     JMenuItem save = new JMenuItem("Save"); 
     JMenuItem exit = new JMenuItem("Exit"); 
     JMenuItem about = new JMenuItem("About"); 
     file.add(openHS); 
     file.add(save); 
     file.add(about); 
     file.addSeparator(); 
     file.add(exit); 
     menuBar.add(file); 
     openHS.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0){ 
       //HighScores run = new HighScores(); 
       //run.main(q); 
      } 
     }); 
     exit.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0){ 
       int n = 0; 
       System.exit(n); 
      } 
     });   
     about.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0){ 
       //about action 
      } 
     }); 
    } 
    public static void main(String[] args){ 
     System.setProperty("swing.defaultlaf",  "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       try{ 
       new JavaGames(); 
       }catch(IOException io) {} 
      } 
     }); 
    } 
} 

JFrame類加載遊戲:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
public class BrickGUI extends JFrame 
{ 
    public BrickGUI() 
    { 
     BrickGame gui = new BrickGame(); 
     this.setSize(600,600+20); 
     this.setTitle("Brick Breaker"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 
     this.setVisible(true); 
     this.getContentPane().add(new BrickGame()); 
     this.add(gui); 
     gui.init(600,600); 
     gui.start(); 
     gui.run(); 
    } 
    public static void main(String[] args) 
    { 
     new BrickGUI(); 
    } 
} 

類在遊戲被繪製並且這樣的:

import java.awt.*; 
import javax.swing.*; 
import java.applet.*; 
import java.util.*; 
import java.io.*; 
import java.awt.event.*; 
import java.awt.event.KeyListener; 
public class BrickGame extends JPanel implements Runnable,KeyListener 
{  
    Thread runner; 
    private Image Buffer; 
    private Graphics gBuffer; 
    private char c = 'e'; 
    int width,height; 
    BrickBall B1; 
    ArrayList BrickBallList; 
    BrickBall temp,temp2; 
    ArrayList bricks; 
    BrickRectangle rTemp; 
    int bx,by,bw,bh; 
    BrickRectangle R1,R2; 
    boolean leftKey; 
    boolean rightKey; 
    boolean spaceKey; 
    Paddle p; 
    int points; 
    public void init(int W,int H) 
    { 
     width=590; 
     height=590; 
     Buffer=createImage(width,height); 
     gBuffer=Buffer.getGraphics(); 
     B1 = new BrickBall(-1); 
     p = new Paddle(); 
     bricks = new ArrayList(); 
     bx = 5; 
     by = 5; 
     bw = 20; 
     bh = 10; 
     int count = 0; 
     for(int i = 0; i < 160;i++) 
     { 
      rTemp = new BrickRectangle(bx,by,bw,bh,0,0); 
      bx = bx + 25; 
      count++; 
      if(count == 32) 
      { 
       by = by + 15; 
       bx = 5; 
       count = 0; 
      } 
     bricks.add(rTemp); 
    } 
    addKeyListener(this); 
    points = 0; 
} 
public void start() 
{ 
    if (runner == null) 
    { 
     runner = new Thread (this); 
     runner.start(); 
    } 
} 
public void stop() 
{ 
    runner = null; 
} 
public void run() 
{ 
    while(true) 
    { 
     if(rightKey){p.moveRight();} 
     if(leftKey){p.moveLeft();} 
     repaint(); 
     try {runner.sleep(25);} 
     catch (Exception e) { }  
     gBuffer.setColor(Color.black); 
     gBuffer.fillRect(0,0,width,height); 
     B1.moveBrickBall(width,height); 
     p.ballCollision(B1); 
     for(int j = 0; j < bricks.size();j++) 
     { 
      rTemp = (BrickRectangle)bricks.get(j); 
      if(rTemp.BrickBallCollision(B1)) 
      { 
       bricks.remove(j); 
       points = points + 1; 
       break; 
      } 
     } 
     String P = String.valueOf(points); 
     gBuffer.setColor(Color.blue); 
     gBuffer.setFont(new Font("Calibri",20,60)); 
     gBuffer.drawString(P,0,540); 
     for(int i = 0; i < bricks.size();i++) 
     { 
      rTemp = (BrickRectangle)bricks.get(i); 
      rTemp.paint(gBuffer); 
     } 
     B1.paint(gBuffer); 
     p.paint(gBuffer); 
     if(B1.getY()>height) 
     { 
      String s1 = "Game Over"; 
      String s2 = "You Lose!"; 
      String s3 = "You got " +points +" Points"; 
      String s4 = "Hit space key to continue"; 
      gBuffer.drawString(s1,50,90); 
      gBuffer.drawString(s2,50,160); 
      gBuffer.drawString(s3,50,230); 
      gBuffer.setFont(new Font("Calibri",20,50)); 
      gBuffer.drawString(s4,50,300); 
      BrickHS temp = new BrickHS(); 
      try{ 
       boolean t = temp.checkHS(points); 
       if(t) 
       { 
        gBuffer.drawString("New High Score",50,360); 
       } 
      }catch(IOException io) {} 
      if(spaceKey){System.exit(1);} 
     } 
     repaint(); 
    }//while(true) 
} //run  
public void update(Graphics g) 
{ 
    paint(g); 
} 
public void paint(Graphics g) 
{ 
    g.drawImage(Buffer,0,0, this); 
} 
public void addNotify() 
{ 
    super.addNotify(); 
    requestFocus(); 
} 
public void keyPressed(KeyEvent e) 
{ 
    switch(e.getKeyCode()) 
     { 
      case KeyEvent.VK_LEFT: 
      leftKey = true; 
      break; 
      case KeyEvent.VK_RIGHT: 
      rightKey = true; 
      break; 
      case KeyEvent.VK_SPACE: 
      spaceKey = true; 
      break; 
     } 
} 
public void keyReleased(KeyEvent e) 
{ 
    switch(e.getKeyCode()) 
     { 
      case KeyEvent.VK_LEFT: 
      leftKey = false; 
      break; 
      case KeyEvent.VK_RIGHT: 
      rightKey = false; 
      break; 
      case KeyEvent.VK_SPACE: 
      spaceKey = false; 
      break; 
     } 
} 
public void keyTyped(KeyEvent e) 
{ 
    c = e.getKeyChar(); 
    repaint(); 
} 
} 

還有幾其他類與這個遊戲一起去,但那些主要只是爲了製作物體而不是什麼。再次感謝提前!

+0

不要調用事件處理程序的線程運行遊戲。不要爲變量和方法使用相同的名稱。不要使用以變量的上級字母開頭的名稱。不要通過該類的對象在某個類中調用靜態方法。 – laune

+1

這是一個讓很多志願者遊過的很多代碼 - 你可能會考慮把它修剪成只能再現你遇到的問題的部分(例如[MCVE](http://stackoverflow.com/help/mcve) )) – copeg

+0

請參閱[使用多個JFrames,好的/壞的實踐?](http://stackoverflow.com/q/9554636/418556) –

回答

2

每次創建BrickGame,它進入一個無限循環,同時通過調用run - 這是在Event Dispatch Thread (EDT)發生並導致凍結的用戶界面爲EDT不能畫畫,創建活動等......你的代碼開始撥打start的新線程 - 沒有理由額外致電run

建議的其他一些作品:

  1. 覆蓋paintComponent而不是paint
  2. 呼叫super.paintComponent如(1)
  3. 實施中的第一個電話你不應該重寫update之一,也不addNotify
  4. 將線程的引用設置爲null不會停止線程 - 而是使用一個標誌,該標誌將使用while循環來終止吃了(這個標誌可能需要被宣佈爲不穩定)。
  5. 變量名稱應以小寫字母開頭。
  6. 避免null佈局 - 使用適當的LayoutManager
+0

當它調用運行時,我拿出它,它現在起作用了!非常感謝您的建議和您的幫助 – Zackc95

+0

@ Zackc95很高興聽到它,但請不要忘記認真聽取其他建議 - 它們可能會導致與此相比的頭痛。 – copeg

+1

如果幫助解決問題,請[接受答案](http://meta.stackexchange.com/a/5235/155831)。 –