Set.visible isn't actually doing anything for me, or thread.sleep is screwing me over

Ryan S :

My pulseRed (pulse a red jButton) method should make the button visible, wait one second, then make it invisible again. I have the button started as invisible by the way. However when i run it, the button is never set to be visible. I have print statements that say that it should be setting them visible, but it doesn't. I don't know if it is a problem with the timer or the set.visible part.

I have tried different timers, but they are all kinda complicated and I'm sorta new to java. I did try instead of making the button visible then invisible, make the button bigger and smaller to simulate a pulse. But that also didn't work, which makes me think its a timer problem.

Heres the code:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class redButtonPulseQuestionCode {
    private static JButton redButton = redButton();
    private static JButton startButton = new JButton("Start");

    redButtonPulseQuestionCode() {
        JFrame colorFrame = new JFrame("Simon Says Game");
        JPanel colorPanel = new JPanel();

        colorPanel.setBounds(0, 0, 500, 500);
        colorPanel.setBackground(Color.DARK_GRAY);
        colorFrame.setLayout(null);
        colorPanel.setLayout(null);

        startButton.setBackground(Color.white);

        colorPanel.add(redButton);
        colorPanel.add(startButton);

        redButton.setBounds(50, 175, 100, 100);
        redButton.setVisible(false);

        startButton.setBounds(335, 400, 100, 50);

        colorFrame.add(colorPanel);
        colorFrame.setSize(500,500);
        colorFrame.setLocationRelativeTo(null); //sets the game to the center of the screen    
        colorFrame.setLayout(null);
        colorFrame.setVisible(true);
        colorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }

    public static void main(String [] args) {
        new redButtonPulseQuestionCode();
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    // does stuff when the start button is pressed
                        //setEveryButtonInvisible();
                        startGame();
                }
            });
    }

    public static JButton redButton() {
        JButton redButton = new JButton();
        redButton.setBackground(Color.red);
        return redButton;
    }

    public static void timer1() 
    {
        try 
        {
            Thread.sleep(1000);
        } catch(InterruptedException ie) 
        {

        }
    }

    public static void pulseRed() {
        redButton.setVisible(true);
        System.out.println("set red visible");
        timer1();
        redButton.setVisible(false);
        System.out.println("set red invisible");
    }

    public static void startGame() {
        boolean userAnswer = true;
        int round = 0;
        System.out.println("Start game");
        pulseRed();

}
}

It should, theoretically, when you press the start button, make the red button visible, and then turn it off after a 1 second delay. But in the case of this code, it doesn't appear, but the print statements do work.

Sweeper :

You shouldn't just call Thread.sleep on the UI thread of a GUI app like that.

What is happening is, you set the button to be visible, but before the screen has a chance to update the button's visibility, you tell the thread sleep. This stops the screen from updating the button. After the screen "wakes up", you set the button to be invisible again. This is why you never see the button appear.

What you should do is to use a Timer. You can use the java.swing.Timer, like this:

redButton.setVisible(true);
Timer t = new Timer(1000, e -> {
    redButton.setVisible(false);
});
t.setRepeats(false);
t.start();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=156676&siteId=1