2016-03-25 17 views
1

'da InterrupedException gerektiren bir yöntemi nasıl çağırırım Bir düğmeye basabilmeyi ve Java 2d kullanarak küçük bir oyun oluşturmayı istiyorum.JButton ActionListner

: oluşturmak yönteminden kodudur i try/catch kullanmaya çalıştık ama burada

Button.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 

         game.create();/***is a new window with a small 2d game, 
         the 'create' method requires and InterruptedException to be thrown.***/ 




       } 

      }); 

(çünkü sanırım oluşturmak yönteminde ise döngü) sonsuz döngüye takılıyor

public void create() throws InterruptedException { 

    JFrame frame = new JFrame("Mini Tennis"); 
    GameMain gamemain = new GameMain(); 
    frame.add(gamemain); 
    frame.setSize(350, 400); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    while (true) { 
     gamemain.move(); 
     gamemain.repaint(); 
     Thread.sleep(10); 

    } 
} 

cevap

2

Sonsuz döngüsünüzün salınan parçayı düğmenize yanıt vermesini engellediğine inanıyorum.

ayrı bir konu sizin döngüsüne sahip deneyin: çalışmıştır

public void create() throws InterruptedException { 

    JFrame frame = new JFrame("Mini Tennis"); 
    GameMain gamemain = new GameMain(); 
    frame.add(gamemain); 
    frame.setSize(350, 400); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    (new Thread() { 
    public void run() { 
     while (true) { 
      gamemain.move(); 
      gamemain.repaint(); 
      Thread.sleep(10); 
     } 
    } 
    ).start(); 
} 
+0

evet mükemmel teşekkür ederim! – tamalon

+0

Yardım etmekten mutluluk duyuyorum! –

İlgili konular