How to repaint a jpanel every x seconds?

后端 未结 1 1724
南笙
南笙 2021-01-19 01:06

i would like to know how to repaint and update the background of a JPanel every x seconds...This is my code:

package view;

import java.awt.Color;
import jav         


        
1条回答
  •  囚心锁ツ
    2021-01-19 01:43

    Use Swing Timer,

    class GamePanel extends JPanel implements ActionListener{
    
     Timer timer=new Timer(1000, this);
    
     public GamePanel() {
       timer.start();// Start the timer here.
     }
    
     public void actionPerformed(ActionEvent ev){
        if(ev.getSource()==timer){
          repaint();// this will call at every 1 second
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题