2016-11-30 15 views
0

を通じて切り替える必要があります...私はランダムな色のプログラムを持っていると私はそれが急速に私は私の「瘤モード」ボタンのActionListenerを持っているが、私はそれを押したときに、それは何もしない色

はのためのコードですActionListener:

private class AneurysmMode implements ActionListener { 


      AneurysmMode() {} 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       while (true) { 

        try { 
         Thread.sleep(100); 
         frame.getContentPane().setBackground(colours[(int)(Math.random()*(9)+0)]); 
        } 
       catch (InterruptedException ex) { 

       } 
      } 
     }   
    } 
+1

'try..catch'は、任意の有用な警告を抑制したり、あなたが得るかもしれないエラーであることを。 – usr2564301

+0

InterruptedExceptionにprintステートメントを入れて、何が起こるかを見てください。 :) – DejaVuSansMono

+0

このコードをどのように実行しますか? 'AneurysmMode'はボタンであることを意図していますか? – ItamarG3

答えて

1

enter image description here

あなたがそのようなあなたがすべてのレンダリングの上に多くの制御を必要とするゲームとして洗練された何かを作っている場合を除き。そうでない場合は、独自のレンダリングループを実装する代わりに、javax.swing.timerをフルに使用できます。

EDTでレンダリングループまたはスリープを適用しないでください。 actionPerformed内のアクションは、通常、「1回限り」のアクションです。

この場合はボタンであなたのactionPerformedが故にあなたがする必要があるすべては、それがクリックされたときに(アニメーションを制御)、タイマーをオン/オフすることで、単に「オン/オフ」ボタンのように動作します:

class DrawingSpace extends JPanel{ 

    private JButton button; 
    private Timer timer; 
    private int idx; 
    private Color[] colors;  

    public DrawingSpace(){ 
     setPreferredSize(new Dimension(200, 200)); 
     initComponents(); 
     add(button);  
    } 

    public void initComponents(){ 
     idx = 0; 
     colors = new Color[]{Color.RED, Color.YELLOW, Color.ORANGE, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN}; 
     button = new JButton("Let the colors rock!"); 
     timer = new Timer(100, new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       //Change color every (approx) 100 milliseconds 
       idx = (idx + 1) % colors.length; //cycle through the colours 
       setBackground(colors[idx]); 
      }    
     });  

     button.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       if(!timer.isRunning()) 
        timer.start(); 
       else 
        timer.stop(); 
      } 
     }); 
    } 
} 

コードを実行するランナークラス:

class RainbowRunner{ 
    public static void main(String[] args){  
     // Run the GUI code on the EDT 
     SwingUtilities.invokeLater(new Runnable() {  
      @Override 
      public void run() { 
       JFrame frame = new JFrame("Rainbow Frame"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new DrawingSpace()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true);    
      } 
     }); 
    } 
} 
+0

変数idxは何をしますか? – Herseept72

+0

これを使用して、どの色のターンを繰り返すかを指定します。 – user3437460

+0

あなたはただランダムを使用していますか? – Herseept72

4

あなたはイベントのディスパッチスレッドを眠っている、それは悪い考えです。新しい色をイベントキューに追加しますが、ボタンアクションリスナーが戻ってこないため、アクションキューに入れません。 whileループをボタンハンドラから取り除くと、ボタンが押されるたびに変更されるはずです。

より良いオプションは、タイマーアクションイベントの色を変更し、ボタンを押してタイマーを開始することです。

+0

タイマーでどうすればいいですか? – Herseept72

+0

バックグラウンドカラーを設定するアクションリスナを持つタイマーオブジェクトを用意します。ボタンのアクションリスナーがタイマーを開始します。 java.swing.timerのドキュメントを見てください。そこにはかなり冗長なチュートリアルがあります。 –

+0

これは私が今まで持っていたものですが、私がそれを押したときに何もしません... 'タイマ時間=新しいタイマ(100、新規のActionListener(){ \t \t \t \t @Override \t \t \t \t公共ボイドのactionPerformed(のActionEvent e)の{ \t \t \t \t \t \t \t \t \t \t一方(真){ \t \t \t \t \t \t \t \t \t \t time.start(); \t \t \t \t \t \t \t frame.getContentPane()setBackground(色[(INT)(Math.random()*(9)+0)])。 \t \t \t \t \t \t \t} \t \t \t \t} が\t \t \t});あなたは必要ありません – Herseept72

0

actionPerformedメソッドにゲームループ全体(またはそれを呼び出すもの)を投げたくないでしょう。それが開始されていない場合で何ができ、すべてのボタンのクリックで、タイマーをトリガー:

button.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e){ 
      if(!timer.isRunning()) 
       timer.start(); 
      else 
       timer.stop(); 
     } 
    }); 

は、あなた自身のループを書くのではなく、javax.swing.timerを使用して検討することができます。あなたのjavax.swing.timer

は、あなたがこのようなものを持つことができます。

timer = new Timer(100, new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e){ 
      //Change color every (approx) 100 milliseconds 
      idx = (idx + 1) % colours.length; //cycle through the colours 
      frame.setBackground(colours[idx]); 
     }    
    }); 
+0

コードを追加して実装する場所を示していますか? – Herseept72

+0

ええと、これについての完全な例を書きましょう。 – user3437460

+0

本当にありがとうございました – Herseept72

関連する問題