如何刷新/更新/更改JFrame组件

时间:2013-12-21 12:29:22

标签: java swing jframe

我尝试过使用invalidate(),validate(),revalidate(),repaint(),但我的JFrame仍然不会刷新或更新或更改。我正在使用Thread.sleep()来添加一些延迟,但它不会工作。

import java.awt.*;
import javax.swing.*;    
import java.awt.event.*;


public class GameOfLife implements MouseListener, ActionListener {


public final int MAX_X = 10;
public final int MAX_Y = 10;
int[][] matrix_old = new int[MAX_X+2][MAX_Y+2];
int[][] matrix_new = new int[MAX_X+2][MAX_Y+2];


JFrame mainFrame = new JFrame();
  JPanel pnlheader = new JPanel();
    JLabel lblheader = new JLabel("Game of Life",JLabel.CENTER);
    JLabel lblmsg = new JLabel("Please place the cells:D",JLabel.CENTER);
  JPanel pnlgrid = new JPanel(new GridLayout(10,10));
  JButton btnstart = new JButton();


  JButton[][] grid;

//------------------------------------------------------------------------


public void GameOfLife(){

    mainFrame.setSize(600,500);
    mainFrame.setLayout(new BorderLayout());
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.getContentPane().setBackground(Color.WHITE);

       pnlheader.setLayout(new GridLayout(2,1));
       pnlheader.add(lblheader);
       pnlheader.add(lblmsg);

       pnlgrid.setLayout(new GridLayout(MAX_X,MAX_Y));

    grid = new JButton[MAX_X+2][MAX_Y+2];
    for(int x = 0 ; x < (MAX_X+2) ; x++){
        for(int y = 0 ; y < (MAX_Y+2) ; y++){
                grid[x][y] = new JButton(""+x+","+y+"");
                matrix_old[x][y] = 0 ;
            if(y != 0 && x != 0 && y != (MAX_Y+1) && x != (MAX_X+1)){
                     pnlgrid.add(grid[x][y]);
                             grid[x][y].setEnabled(false);
                     grid[x][y].addMouseListener(this);
                 }
            }
        }


    btnstart.setText("Start Generation");
    mainFrame.add(pnlheader,BorderLayout.NORTH);
    mainFrame.add(pnlgrid,BorderLayout.CENTER);
    mainFrame.add(btnstart,BorderLayout.SOUTH);

    btnstart.addActionListener(this);

    mainFrame.setVisible(true);
    }//end GameOfLife
//------------------------------------------------------------------------

    public void mouseClicked(MouseEvent e){
           for(int x = 0 ; x < MAX_X+2 ; x++){
                     for(int y = 0 ; y < MAX_Y+2 ; y++){
                              if(e.getSource().equals(grid[x][y])){
                                    if(grid[x][y].isEnabled() == false){
                                         grid[x][y].setEnabled(true);
                                         matrix_old[x][y] = 1;
                                     }
                                else if(grid[x][y].isEnabled() == true){
                                         grid[x][y].setEnabled(false);
                                         matrix_old[x][y] = 0;
                                     }
                                }//end if
                    }//end for
           }//end for
    }//end mouseClicked


    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
//------------------------------------------------------------------------

public void actionPerformed(ActionEvent z){
    if(z.getSource() == btnstart){
             generation();
    }
//------------------------------------------------------------------------

public void generation(){

try{

int checkerResult = 0;
    for(int gene = 1 ; gene <= 10 ; gene++){ // number of generations
            for(int rows = 0; rows < MAX_X+2 ; rows++){ //numer for row
        for(int cols = 0 ; cols < MAX_Y+2 ; cols++ ){ //number for columns
       if(matrix_old[rows][cols] == 1 && rows != 0 && cols != 0  && rows != (MAX_Y+1) && cols != (MAX_X+1)  ){ // if there is a velue
checkerResult = checker(rows,cols);

if(checkerResult >=4 || checkerResult <=1 )
matrix_new[rows][cols]=0;
}//end if
else if(matrix_old[rows][cols] == 0 && rows != 0 && cols != 0  && rows != (MAX_Y+1) && cols != (MAX_X+1)){ // if there is a velue
    checkerResult = checker(rows,cols);
        if(checkerResult == 3)
    matrix_new[rows][cols]=1;
                            }//end else if
                    }//end for
                }//end for

        exchangeValues();
        animateButtons();

        lblmsg.setText("Generation # " + gene);


        try{
            Thread.sleep(100);
        }catch(Exception E){
            JOptionPane.showMessageDialog(null, E);
        }
mainFrame.setEnabled(false);
mainFrame.revalidate();
mainFrame.validate();
mainFrame.repaint();
mainFrame.setEnabled(true);


        }//end for
}catch(Exception X){
        JOptionPane.showMessageDialog(null, X);
                }

    }//end generation

//------------------------------------------------------------------------
public int checker(int rows, int cols){
    int checkerResult = 0;

    //above row

    if(matrix_old[rows-1][cols-1]==1)
    checkerResult++;

    if(matrix_old[rows-1][cols]==1)
    checkerResult++;

    if(matrix_old[rows-1][cols+1]==1)
    checkerResult++;

    //same row

    if(matrix_old[rows][cols-1]==1)
    checkerResult++;

    if(matrix_old[rows][cols+1]==1)
    checkerResult++;

    //below row

    if(matrix_old[rows+1][cols-1]==1)
    checkerResult++;

    if(matrix_old[rows+1][cols]==1)
    checkerResult++;

    if(matrix_old[rows+1][cols+1]==1)
    checkerResult++;

    return  checkerResult;
   }

//------------------------------------------------------------------------
public void exchangeValues(){
for(int rows = 0 ; rows < MAX_X+2 ; rows++){
    for(int cols = 0 ; cols < MAX_Y+2 ; cols++){
            matrix_old[rows][cols] = matrix_new[rows][cols];
            System.out.print(matrix_old[rows][cols]);
        }
        System.out.println("");
    }
}
//------------------------------------------------------------------------
public void animateButtons(){
for(int rows = 0 ; rows < MAX_X+2 ; rows++){
    for(int cols = 0 ; cols < MAX_Y+2 ; cols++){
            if(matrix_new[rows][cols] == 1){
                    grid[rows][cols].setEnabled(true);
                }
            else if(matrix_new[rows][cols]== 0){
                    grid[rows][cols].setEnabled(false);
                }

        }
    }


}

//------------------------------------------------------------------------
public static void main(String [] args){
    GameOfLife start = new GameOfLife();
    start.GameOfLife();
    }// end main


}//end class2

1 个答案:

答案 0 :(得分:1)

使用Swing Timer代替Thread.sleep(阻止EDT)。像这样的东西

ActionListener listener = new ActionListener(){
    public void actionPerformed(ActionEvent e){
        System.out.println("Hello");
    }
}

int delay = 5000;

Timer timer = new Timer(delay, listener);
timer.setRepeats(false);
timer.start();

Timer javadoc | Timer tutorial