这是通过单击清洁器和污垢按钮的初始状态。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);
}
}
}
答案 0 :(得分:1)
我使用了Thread.sleep()。
不要使用Thread.sleep()。这将阻止GUI重新绘制自己。
相反,您需要使用Swing Timer
来安排活动。因此,您将Timer设置为每3秒生成一个事件。当事件触发时,您可以更改图像。
阅读How to Use Swing Timer上Swing教程中的部分,了解更多信息和工作示例。