我看遍了整個網絡,發現沒有解決我的問題。對於AP Comp Sci項目,我製作了一套遊戲,這些遊戲將從JFrame和JButtons運行。我已經準備好了遊戲,還有動作聽衆,但遊戲不會正常啓動。 JFrame和JButton都安裝正確。ActionListener和SystemUtil.invokeLater的問題
private static class TetListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
GameCenter.quit();
GameCenter.startTetris();
}
}
GameCenter.quit()什麼也不做,但運行JFrame.dispose(),和GameCenter.startTetris();構造一個新的Tetris對象,然後運行play()方法來啓動遊戲。所有的俄羅斯方塊都編碼正確,並且在主方法中運行時(在actionlistener之外)正常工作。但是一旦我將它放入ActionListener中,它就無法正確構建。我將問題追查到:
public BlockDisplay(BoundedGrid<Block> board)
{
this.board = board;
grid = new JPanel[board.getNumRows()][board.getNumCols()];
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() // <<<<<<<<<<------------------- Problem Here
{
public void run()
{
createAndShowGUI(); // <<<<<<<<<<<<-------- Never Run
}
});
//Wait until display has been drawn
try
{
while (frame == null || !frame.isVisible()) // <<<<<<<-------- Never Resolved
{
Thread.sleep(1);
}
}
catch(InterruptedException e)
{
e.printStackTrace();
System.exit(1);
}
}
所以程序總是掛起。我也做了一個使用這個SwingUtilities.invokeLater的Pacman遊戲,所以它也不起作用。我無法弄清楚爲什麼會發生這種情況,或者如何解決這個問題。
任何幫助表示讚賞。讓我知道你是否需要更多信息。
''公共BlockDisplay(BoundedGrid板)'有沒有被調用? –
2011-05-20 00:36:04
是的。俄羅斯方塊在ActionListener之外完美工作,就像它應該那樣。 – 2011-05-20 01:05:45