4
I’m having a problem trying to make the buttons blink.
Using the Thread.sleep()
, by clicking the button, Thread.sleep()
ignores what comes before it, executes the sleep
and performs only what comes next, in this case the setBackground(Color.GRAY)
. I’ve looked for solutions here, and either I didn’t understand what they meant or didn’t work. I also tried with the class Timer
, but I could not.
package exstackoverflow;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ExStackOverflow extends JFrame{
JButton b = new JButton();
JButton b2 = new JButton();
JButton b3 = new JButton();
JButton b4 = new JButton();
int numBotao=0;
public ExStackOverflow(){
Container c = getContentPane();
c.setLayout(new GridLayout(2,2));
b.setBackground(Color.GRAY);
b2.setBackground(Color.GRAY);
b3.setBackground(Color.GRAY);
b4.setBackground(Color.GRAY);
b.addActionListener(new botaoListener());
b2.addActionListener(new botaoListener());
b3.addActionListener(new botaoListener());
b4.addActionListener(new botaoListener());
c.add(b);
c.add(b2);
c.add(b3);
c.add(b4);
numBotao = new Random().nextInt(4);
piscar();
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class botaoListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(b)){
numBotao=1;
piscar();
}else if(ae.getSource().equals(b2)){
numBotao=2;
piscar();
}else if(ae.getSource().equals(b3)){
numBotao=3;
piscar();
}else if(ae.getSource().equals(b4)){
numBotao=4;
piscar();
}
}
}
public void piscar(){
try{
switch(numBotao){//switch baseado nos valores inseridos na lista Sequencia
case 1:
b.setBackground(Color.BLUE);
Thread.sleep(1500);
b.setBackground(Color.GRAY);
break;
case 2:
b2.setBackground(Color.RED);
Thread.sleep(1500);
b2.setBackground(Color.GRAY);
break;
case 3:
b3.setBackground(Color.YELLOW);
Thread.sleep(1500);
b3.setBackground(Color.GRAY);
break;
case 4:
b4.setBackground(Color.GREEN);
Thread.sleep(1500);
b4.setBackground(Color.GRAY);
break;
}
}catch(Exception e){
}
}
public static void main(String[] args) {
new ExStackOverflow().piscar();
}
}
Please add a [mcve] so that it is possible to simulate the problem.
– user28595
Another thing, you need to work with swingworker to do this, because in swing, the entire graphical interface runs on top of a thread called Event-Dispatch-thread, and the screen only updates after finishing the changes in this interface. That is, when the EDT updates its screen, the two changes will have already been made and will not flash the component.
– user28595
the idea was to make the game Genius. There are already several questions for this but I saw a solution that worked. And also it’s the first time I see someone talk about swingworker.
– TheJ
I understand, but without one [mcve] it’s even hard to tell if swingworker is what you need.
– user28595
diegofm, I edited and put a new example. Please see if it’s enough to help you understand the problem.
– TheJ
my answer answered you? You can accept it by clicking on
✅
, this way it will serve as reference for other users. :)– user28595