java - 如何在 Java 中延迟在按钮上显示图像

标签 java swing jframe

这是单击清洁器和污垢按钮后的初始状态。 Initial Image

输出将是这样的。 Final Image

但我想在每个按钮上显示图像,延迟 3 秒。即第二个真空吸尘器将在第一个真空吸尘器的 3 秒后出现,第三个真空吸尘器将在第二张图像的 3 秒后出现。等等。我使用了Thread.sleep()。但每次结果都会一次显示一个。 我已经寻找解决方案,但无法管理它。

抱歉英语不好。 这是我的代码。

package vacuumcleaner;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.lang.Math.abs;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Md.Rezwanul Islam
 */
public class VC {
    final int sz = 7;
    int board [][] = new int [sz*100][sz*100], dirtclick=0,cleanerclick=0,noway=0,cnt=0,s=0,vcnt=0;
    int dirtx[] = new int[sz*100], dirty[]=new int[sz*100], dis[][]=new int[sz*100][sz*100];
    int startx=0,starty=0;
    int x=0,y=0;
    JFrame frm = new JFrame("Clean It !");

    JPanel container = new JPanel();
    JPanel buttonholder = new JPanel();
    JPanel info = new JPanel();
    JPanel dirtP= new JPanel();
    JPanel cleanerP=new JPanel();
    JPanel playP = new JPanel();
    JPanel resetP= new JPanel();

    JButton btn[][] = new JButton[sz][sz];
    JButton dirt=new JButton("Dirt");
    JButton cleaner=new JButton("Cleaner");
    JButton play=new JButton("Clean it !");
    JButton reset=new JButton("Reset");

    ImageIcon dirt1= new ImageIcon("C:\\Users\\Md.Rezwanul Islam\\Documents\\NetBeansProjects\\VacuumCleaner\\Images\\Dirt.png");
    ImageIcon cleaner1=new ImageIcon("C:\\Users\\Md.Rezwanul Islam\\Documents\\NetBeansProjects\\VacuumCleaner\\Images\\VacuumCleaner.png");
    ImageIcon dirt2= new ImageIcon("C:\\Users\\Md.Rezwanul Islam\\Documents\\NetBeansProjects\\VacuumCleaner\\Images\\dirtBig.png");
    ImageIcon cleaner2=new ImageIcon("C:\\Users\\Md.Rezwanul Islam\\Documents\\NetBeansProjects\\VacuumCleaner\\Images\\cleanerBig.png");
    JLabel label_dirt=new JLabel(dirt1);
    JLabel label_cleaner=new JLabel(cleaner1);


    VC(){

        dirt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                 for (int i = 0; i < sz; i++) {
                        for (int j = 0; j < sz; j++) {
                            btnactiondirt(btn[i][j], i, j);                         
                        }                     
                    } 
            }
        });

        cleaner.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){

                    for (int i = 0; i < sz; i++) {
                        for (int j = 0; j < sz; j++) {
                            btnactioncleaner(btn[i][j], i, j);                         
                        }                     
                    }                                           
           }  
        });

        play.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
                noway = 1;
                disout();
           }  
        });
        reset.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
               frm.dispose();
               VC dis = new VC();
           }  
        });

      for(int i=0;i<sz;i++)
        {
            for(int j=0;j<sz;j++)
            {
                btn[i][j]=new JButton();
                buttonholder.add(btn[i][j]);
            }
        }

        buttonholder.setPreferredSize(new Dimension(700, 700));
        buttonholder.setLayout(new GridLayout(sz, sz));

        dirtP.setBackground(Color.LIGHT_GRAY);
        dirtP.add(dirt);
        dirtP.add(label_dirt);
        cleanerP.setBackground(Color.LIGHT_GRAY);
        cleanerP.add(cleaner);
        cleanerP.add(label_cleaner);
        playP.setBackground(Color.LIGHT_GRAY);
        playP.add(play);
        resetP.setBackground(Color.LIGHT_GRAY);
        resetP.add(reset);

        info.setLayout(new BoxLayout(info, BoxLayout.X_AXIS));
        info.add(dirtP);
        info.add(cleanerP);
        info.add(playP);
        info.add(resetP);

        container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
        container.add(buttonholder);
        container.add(info);

        frm.add(container);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setSize(700, 700);
        frm.setResizable(false);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    final void btnactiondirt(JButton tmpbtn, int a,int b) {
        tmpbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                dirtclick++;
                setdirt(a,b);
            }
        });
    }

    final void btnactioncleaner(JButton tmpbtn, int a,int b) {
        tmpbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
               cleanerclick+=1;
               setcleaner(a,b);
            }
        });
    }

    void setcleaner(int a,int b)
    {
        if(cleanerclick==1){
            btn[a][b].setIcon(cleaner2);
            board[a][b]=11;
            startx=a;
            starty=b;
        }

    }
    void setdirt(int a, int b)
    {
        if(dirtclick>0 & noway==0){
            cnt+=1;
            btn[a][b].setIcon(dirt2);
            dirtx[x]=a;
            dirty[y]=b;
            dis[dirtx[x]][dirty[y]]=cntdis(dirtx[x],dirty[y]);
             x++;
            y++;
        }
    }
    int cntdis(int a,int b){
        int dcnt=0;
        int tstartx=startx;
        int tstarty=starty;
         for(;;){
       if(tstartx==a)
       {
           if(tstarty==b)
           {
               dcnt++;
               break;
           }
           if(tstarty<b)
           {
               tstarty++;
           }
           else
               tstarty--;
           dcnt++;
       }
      else if(tstarty==b)
       {
           if(tstartx==a)
           {
               dcnt++;
               break;
           }
           if(tstartx<a)
           {
               tstartx++;
           }
           else
               tstartx--;
          dcnt++;
       }
      else if(tstartx>a && tstarty>b){
           tstartx--;
           tstarty--;
           dcnt++;
       }
       else if(tstartx>a && tstarty<b){
           tstartx--;
           tstarty++;
           dcnt++;
       }
       else if(tstartx<a && tstarty>b){
           tstartx++;
           tstarty--;
           dcnt++;
       }
      else if(tstartx<a && tstarty<b){
           tstartx++;
           tstarty++;
           dcnt++;
       }

        }
         return dcnt-1;
    }  
    void check(int desx, int desy)
    {       
        for(;;){
       if(startx==desx)
       {
           if(starty==desy)
           {
               vcnt++;
               break;
           }
           if(starty<desy)
           {
               starty++;
           }
           else
               starty--;

           btn[startx][starty].setIcon(cleaner2);
           board[startx][starty]=1;
       }
      else if(starty==desy)
       {
           if(startx==desx)
           {
               vcnt++;
               break;
           }
           if(startx<desx)
           {
               startx++;
           }
           else
               startx--;
           btn[startx][starty].setIcon(cleaner2);
           board[startx][starty]=1;
       }
      else if(startx>desx && starty>desy){
           startx--;
           starty--;
       }
       else if(startx>desx && starty<desy){
           startx--;
           starty++;
       }
       else if(startx<desx && starty>desy){
           startx++;
           starty--;
       }
      else if(startx<desx && starty<desy){
           startx++;
           starty++;
       }

           btn[startx][starty].setIcon(cleaner2);
           board[startx][starty]=1;
        }
    }
    void disout() {

        for (int ii = 0; ii < cnt; ii++) {
            int max = 100;
            int tx = 0;
            int ty = 0;
            for (int i = 0; i < sz; i++) {
                for (int j = 0; j < sz; j++) {
                    if (dis[i][j] > 0 && dis[i][j] < max) {
                        tx = i;
                        ty = j;
                        max = dis[i][j];
                    }
                }
            }
            dis[tx][ty] = 0;
            check(tx, ty);            
        }
    }
}

最佳答案

I used Thread.sleep().

不要使用Thread.sleep()。这将阻止 GUI 重新绘制自身。

相反,您需要使用 Swing Timer 来安排事件。因此,您将计时器设置为每 3 秒生成一个事件。当事件触发时,您将更改图像。

阅读 Swing 教程中关于 How to Use Swing Timer 的部分了解更多信息和工作示例。

关于java - 如何在 Java 中延迟在按钮上显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43968964/

相关文章:

java - HASHMAP - 阈值和负载因子以及容量

java - 使用 JavaFX 渲染窗口中的空像素

java - Storm 集群配置

java - JScrollPane 中 JTextPane 文本后面的静态图像

java - 不可扩展的窗口

用于关闭窗口的 Java Swing 非 Activity 按钮

java - J2me,我需要一个不是Zxing的数据矩阵解码器库

java - 更改 JCombobox 中的项目列表

java - JTabbedPane 滚动按钮位置

java - 在 JFrame 中显示外部文件内容的问题