日期:2014-05-20 浏览次数:20634 次
import java.awt.*; import javax.swing.*; public class MainApp { public static void main(String args[]){ GameControl gameControl = new GameControl(); Environment gameBackground = new Environment(); JFrame frame = new JFrame(); frame.add(gameBackground,BorderLayout.CENTER); frame.setResizable(false); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(475,494); frame.setTitle("是男人就下一百层"); } } class Environment extends JPanel{ private Image background = new ImageIcon("backgroud.gif").getImage(); Environment(){ setLayout(null); add(GameControl.boardPanel); //注释了这行再运行的效果是我想达到的效果。。。 //想知道为什么会出现这个BUG,而不仅仅是怎么修复这个bug } public void paintComponent(Graphics g){ super.paintComponent(g); g.drawImage(background,0,0,getWidth(),getHeight(),this); } } class BoardPanel extends JPanel{ BoardPanel(){ setBounds(24,67,422,379); } public void paint(Graphics g){} } class GameControl implements Runnable{ public static BoardPanel boardPanel = new BoardPanel(); GameControl(){ new Thread(this).start(); } public void run(){ while(true){ try{ Thread.sleep(30); } catch(InterruptedException e){ } boardPanel.repaint(); } } }