java set delay to change imageicon

前端 未结 2 496
猫巷女王i
猫巷女王i 2021-01-29 10:11

i\'m trying to set a delay when a button is pressed to set an imageicon to a certain image then set another delay so that another image would be set, all of this by single click

相关标签:
2条回答
  • 2021-01-29 10:59

    You should try executing the code that sets the image in the event dispatch thread using InvokeLater.

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            lblNewLabel.setIcon(and);
        }
    });
    
    sleeep();
    
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            lblNewLabel.setIcon(and);
        }
    });
    
    0 讨论(0)
  • 2021-01-29 11:15
    1. don't add MouseListener to JButton, nor for mouseClicked(), add ActionListener instead, btw all Mouse and Key events are implemented in JButton API and correctly

    2. don't to use Thread.sleep(n); you have an issue with Concurency in Swing, use Swing Timer instead,

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