2017-10-22 1 views
0

Ich baue ein Raster 50x50 mit JButtons. Ich möchte, dass die Farben zufällig ausgewählt werden (die Auswahlmöglichkeiten liegen zwischen Blau und Rot). Und das zu wiederholen insgesamt 3 mal (Ich habe buildGUI verwendet, um das erste Raster zu erstellen). Aber aus irgendeinem Grund funktioniert es nicht. Es zeigt den anfänglichen Aufbau des Gitters, wird aber nicht dreimal aktualisiert. Ich habe auch versucht, neu zu streichen(). Aber das hat auch nicht geholfen.Warum funktioniert der Swing Timer nicht?

import java.awt.*; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

import javax.swing.*; 
import javax.swing.Timer; 

import java.util.Random; 

class Counter { 

    int n = 0; 
    JFrame frame = new JFrame("GridLayout demo"); 
    JPanel panel = new JPanel(); 
    JButton[][] buttons = new JButton[50][50]; 
    int[][] isCooperating = new int[50][50]; 
    Random rand = new Random(); 

    void buildGUI() { 
     for (int x = 0; x < 50; x++) { 
      for (int y = 0; y < 50; y++) { 
       int randomNumber = rand.nextInt(2); // randomly chooses between 1 and 0; for cooperating status 
       isCooperating[x][y] = randomNumber; // 1 is cooperating 
      } 
     } 

     panel.setLayout(new GridLayout(50,50)); 
     for (int x = 0; x < 50; x++) { 
      for (int y = 0; y < 50; y++) { 
       JButton btn = new JButton(); 
       if (isCooperating[x][y] == 0) { 
        btn.setBackground(Color.RED); 
        btn.setOpaque(true); 
        btn.setBorder(null); 

       } else if (isCooperating[x][y] == 1) { 
        btn.setBackground(Color.BLUE); 
        btn.setOpaque(true); 
        btn.setBorder(null); 

       } 
       btn.setPreferredSize(new Dimension(10, 10));   
       panel.add(btn); 
       buttons[x][y] = btn; 
      } 
     } 

     frame.add(panel); 

     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
     frame.setSize(800,800); 
     frame.setVisible(true); 
    } 
    public void solve() { 
     n = 0; 
     buildGUI(); 

     ActionListener actListner = new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent event) { 
       while (n!=3) { 
        buildGUI(); 
        n++; 
       } 
      } 
     }; 

     Timer timer = new Timer(500, actListner); 

     timer.start(); 
    } 

    public static void main(String[]args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       (new Counter()).solve(); 
      } 
     }); 
    } 
} 
+0

Ihre Zuhörer eng Looping halten, bis 'n' 3. An welchem ​​Punkt ist es Sie' n' glauben wird 3 sein? (Ich kann nichts sehen, um es von 0 zu ändern ...) –

+0

Ihr Zuhörer läuft in eine endlose Schleife. Und da es auf dem Swing-Event-Thread läuft (das ist der Sinn eines Swing-Timers), hat Swing nie die Möglichkeit, die GUI zu aktualisieren. –

+0

Ja, ich habe das gerade hinzugefügt, aber es funktioniert immer noch nicht richtig. Es fügt nur mehr Quadrate mit zufälligen Farben hinzu, während ich möchte, dass die Quadrate gleich bleiben, nur die zu ändernden Farben. –

Antwort

1

Bitte lesen Sie Kommentare hinzugefügt:

public class Counter { 

    private JFrame frame; 
    private JPanel panel; 
    private JButton[][] buttons; 
    private int[][] isCooperating; 
    private Random rand; 
    private ActionListener actListner; 
    private Timer timer; 
    private int n = 0; 

    Counter(){ 
     //do the initialization in the constructor 
     buttons = new JButton[50][50]; 
     isCooperating = new int[50][50]; 
     rand = new Random(); 
     frame = new JFrame("GridLayout demo"); 
     frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 
     panel = new JPanel(); 
     panel.setLayout(new GridLayout(50,50)); 
     //no need to re construct action listener with each buildGUI run 
     actListner = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 

        buildGUI(); 
        System.out.println("building gui " +n); 
        n++; 
        if(n >=3) {timer.stop();} 
      } 
     }; 
     //no need to re construct timer with each buildGUI run 
     timer = new Timer(1500, actListner); 
     timer.setRepeats(true); 
     buildGUI(); 
    } 

    void buildGUI() { 

     //remove all components from panel, if any 
     //otherwise you keep adding components 
     panel.removeAll(); 
     for (int x = 0; x < 50; x++) { 
      for (int y = 0; y < 50; y++) { 
       int randomNumber = rand.nextInt(2); // randomly chooses between 1 and 0; for cooperating status 
       isCooperating[x][y] = randomNumber; // 1 is cooperating 
      } 
     } 

     for (int x = 0; x < 50; x++) { 
      for (int y = 0; y < 50; y++) { 
       JButton btn = new JButton(); 
       if (isCooperating[x][y] == 0) { 
        btn.setBackground(Color.RED); 
        btn.setOpaque(true); 
        btn.setBorder(null); 

       } else if (isCooperating[x][y] == 1) { 
        btn.setBackground(Color.BLUE); 
        btn.setOpaque(true); 
        btn.setBorder(null); 
       } 
       btn.setPreferredSize(new Dimension(10, 10)); 
       panel.add(btn); 
       buttons[x][y] = btn; 
      } 
     } 

     frame.add(panel); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     //let layout set size by using pack 
     //frame.setSize(800,800); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public void solve() { 

     timer.start(); 
    } 

    public static void main(String[]args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Counter().solve(); 
      } 
     }); 
    } 
}