1

I've been trying to program a sleep method for Java with WindowsBuilder without success. The program runs the opposite way of how I wanted it to, thus being said, it sleeps and then shows the graphics.

How would I make the program sleep (Delay, wait) some time?

package me.fractional.main;

import java.awt.EventQueue;

import javax.swing.JFrame;
import me.fractional.main.Studio;
import me.fractional.main.GUI;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import java.awt.Color;



public class main {

public static boolean gamerunning = true;

private JFrame frmFractionalc;
static JLabel logo = new JLabel("");

public static void Sleep(int seconds)
{
    try {
        Thread.sleep(1000 * seconds);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                main window = new main();
                window.frmFractionalc.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public main() {
    initialize();
    intro();
}

private void initialize() {
    frmFractionalc = new JFrame();
    frmFractionalc.getContentPane().setBackground(new Color(0, 139, 139));
    frmFractionalc.setBackground(new Color(0, 128, 128));
    frmFractionalc.setTitle("Fractional (C) - GameDevStory");
    frmFractionalc.setBounds(100, 100, 500, 300);
    frmFractionalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmFractionalc.getContentPane().setLayout(null);

    logo.setHorizontalAlignment(SwingConstants.CENTER);
    logo.setBounds(35, 50, 400, 150);
    frmFractionalc.getContentPane().add(logo);

}

public void intro()
{
    logo.setIcon(new ImageIcon(main.class.getResource("/me/fractional/main/logo.png")));
    Sleep(4);
}

}

1 Answers1

0

Standard answer to this type of question:

  • Do not call Thread.sleep(...) on the main Swing thread, the Event Dispatch Thread or EDT, since putting this thread to sleep prevents it from doing its job of drawing the app and interacting with the user effectively putting the entire app to sleep.
  • Instead use a Swing Timer. The Swing Timer tutorial can show you how.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373