Java 在JButton中显示图标并将其隐藏

Java 在JButton中显示图标并将其隐藏,java,swing,jbutton,imageicon,thread-sleep,Java,Swing,Jbutton,Imageicon,Thread Sleep,我有一个JButton,我想当我点击这个按钮时,在其中显示一个图标,然后在3秒钟后隐藏图标并在按钮中显示一个文本 在action listener中,我尝试了以下代码: JButton clickedButton = (JButton) e.getSource(); clickedButton.setIcon(new ImageIcon(images.get(clickedButton.getName()))); try { Thread.sleep(3000);

我有一个JButton,我想当我点击这个按钮时,在其中显示一个图标,然后在3秒钟后隐藏图标并在按钮中显示一个文本

在action listener中,我尝试了以下代码:

JButton clickedButton = (JButton) e.getSource();
clickedButton.setIcon(new ImageIcon(images.get(clickedButton.getName())));
try {
    Thread.sleep(3000);                
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
clickedButton.setText("x");
clickedButton.setIcon(null);
问题是,当我点击按钮时,程序会阻塞3分钟,然后按钮中会显示文本“x”


如何解决此问题?

不要在Swing事件线程上调用
线程。睡眠(…)
,因为这会冻结线程并将其用于GUI。相反,请使用一个。例如:

final JButton clickedButton = (JButton) e.getSource();
clickedButton.setIcon(new ImageIcon(images.get(clickedButton.getName())));

new Timer(3000, new ActionListener(){
  public void actionPerformed(ActionEvent evt) {
    clickedButton.setText("x");
    clickedButton.setIcon(null);   
    ((Timer) evt.getSource()).stop(); 
  }
}).start();

不要在Swing事件线程上调用
Thread.sleep(…)
,因为这会冻结线程并使用它冻结GUI。相反,请使用一个。例如:

final JButton clickedButton = (JButton) e.getSource();
clickedButton.setIcon(new ImageIcon(images.get(clickedButton.getName())));

new Timer(3000, new ActionListener(){
  public void actionPerformed(ActionEvent evt) {
    clickedButton.setText("x");
    clickedButton.setIcon(null);   
    ((Timer) evt.getSource()).stop(); 
  }
}).start();

不要在Swing事件线程上调用
Thread.sleep(…)
,因为这会冻结线程并使用它冻结GUI。相反,请使用一个。例如:

final JButton clickedButton = (JButton) e.getSource();
clickedButton.setIcon(new ImageIcon(images.get(clickedButton.getName())));

new Timer(3000, new ActionListener(){
  public void actionPerformed(ActionEvent evt) {
    clickedButton.setText("x");
    clickedButton.setIcon(null);   
    ((Timer) evt.getSource()).stop(); 
  }
}).start();

不要在Swing事件线程上调用
Thread.sleep(…)
,因为这会冻结线程并使用它冻结GUI。相反,请使用一个。例如:

final JButton clickedButton = (JButton) e.getSource();
clickedButton.setIcon(new ImageIcon(images.get(clickedButton.getName())));

new Timer(3000, new ActionListener(){
  public void actionPerformed(ActionEvent evt) {
    clickedButton.setText("x");
    clickedButton.setIcon(null);   
    ((Timer) evt.getSource()).stop(); 
  }
}).start();

正如建议的那样,您不需要使用线程。Sleep使用
Swing Timer
来执行此任务

 // Declare button and assign an Icon.
 Icon icon = new ImageIcon("search.jpg");
 JButton button = new JButton(icon);    
 ChangeImageAction listener = new ChangeImageAction(button);
 button.addActionListener(listener);
单击按钮时,下面的ChangeImageAction类将执行必要的操作。 当您单击按钮时,将触发一个操作,在该操作中,我们将调用计时器的操作侦听器,在该侦听器中,我们将按钮的图标设置为null,并为按钮提供标题

class ChangeImageAction implements ActionListener {

    private JButton button;

    public ChangeImageAction(JButton button) {
        this.button = button;
    }

    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            button.setIcon(null);
            button.setText("Button");
        }
    };

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Timer timer = new Timer( 3000 , taskPerformer);
        timer.setRepeats(false);
        timer.start();
    }

}

p.S:我第一次尝试计时器,感谢@满是鳗鱼的气垫船的建议。

正如建议的那样,你不需要使用线程。睡眠使用
摆动计时器来执行此任务

 // Declare button and assign an Icon.
 Icon icon = new ImageIcon("search.jpg");
 JButton button = new JButton(icon);    
 ChangeImageAction listener = new ChangeImageAction(button);
 button.addActionListener(listener);
单击按钮时,下面的ChangeImageAction类将执行必要的操作。 当您单击按钮时,将触发一个操作,在该操作中,我们将调用计时器的操作侦听器,在该侦听器中,我们将按钮的图标设置为null,并为按钮提供标题

class ChangeImageAction implements ActionListener {

    private JButton button;

    public ChangeImageAction(JButton button) {
        this.button = button;
    }

    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            button.setIcon(null);
            button.setText("Button");
        }
    };

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Timer timer = new Timer( 3000 , taskPerformer);
        timer.setRepeats(false);
        timer.start();
    }

}

p.S:我第一次尝试计时器,感谢@满是鳗鱼的气垫船的建议。

正如建议的那样,你不需要使用线程。睡眠使用
摆动计时器来执行此任务

 // Declare button and assign an Icon.
 Icon icon = new ImageIcon("search.jpg");
 JButton button = new JButton(icon);    
 ChangeImageAction listener = new ChangeImageAction(button);
 button.addActionListener(listener);
单击按钮时,下面的ChangeImageAction类将执行必要的操作。 当您单击按钮时,将触发一个操作,在该操作中,我们将调用计时器的操作侦听器,在该侦听器中,我们将按钮的图标设置为null,并为按钮提供标题

class ChangeImageAction implements ActionListener {

    private JButton button;

    public ChangeImageAction(JButton button) {
        this.button = button;
    }

    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            button.setIcon(null);
            button.setText("Button");
        }
    };

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Timer timer = new Timer( 3000 , taskPerformer);
        timer.setRepeats(false);
        timer.start();
    }

}

p.S:我第一次尝试计时器,感谢@满是鳗鱼的气垫船的建议。

正如建议的那样,你不需要使用线程。睡眠使用
摆动计时器来执行此任务

 // Declare button and assign an Icon.
 Icon icon = new ImageIcon("search.jpg");
 JButton button = new JButton(icon);    
 ChangeImageAction listener = new ChangeImageAction(button);
 button.addActionListener(listener);
单击按钮时,下面的ChangeImageAction类将执行必要的操作。 当您单击按钮时,将触发一个操作,在该操作中,我们将调用计时器的操作侦听器,在该侦听器中,我们将按钮的图标设置为null,并为按钮提供标题

class ChangeImageAction implements ActionListener {

    private JButton button;

    public ChangeImageAction(JButton button) {
        this.button = button;
    }

    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            button.setIcon(null);
            button.setText("Button");
        }
    };

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Timer timer = new Timer( 3000 , taskPerformer);
        timer.setRepeats(false);
        timer.start();
    }

}
p.S:感谢装满鳗鱼的气垫船的建议,我第一次尝试计时