Java – making a flash screen with an eclipse progress bar

My main class loads the configuration from the file and then displays a framework I want to use a progress bar like eclipse to initialize the screen so that the progress will increase when the file is loaded and the splash will disappear after the file is loaded Then my main frame loads

Mainclass Code:

public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "classpath:/Meta-INF/spring/applicationContext.xml");
  // splash with progress load till this file is loaded
  UserDao userDao = context.getBean(UserDao.class);
  isRegistered = userDao.isRegistered();
  System.out.println("registered: " + isRegistered);
  if (isRegistered) {
    // progress finish and hide splash
    log.debug("user is registered"); // show frame1
  } else {
    // progress finish and hide splash
    log.debug("user is not registered"); // show frame2
  }
}

I don't have much swing experience, so please teach me how to do this

Update: I have found the following example, but it has no problem:

>When the counter reaches the specified number, it should stop at (300), and it will count forever without stopping the timer and hiding the flash screen. > I want to bind the counter to the file load, so when loading the file, the process will be loaded until the file is loaded, then the process completes and the startup screen disappears

@SuppressWarnings("serial")
@Component
public class SplashScreen extends JWindow {

    static boolean isRegistered;

    static Log log = LogFactory.getLog(SplashScreen.class);

    private static JProgressBar progressBar = new JProgressBar();
    private static SplashScreen execute;
    private static int count;
    private static Timer timer1;

    public SplashScreen() {

        Container container = getContentPane();
        container.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new javax.swing.border.EtchedBorder());
        panel.setBackground(new Color(255,255,255));
        panel.setBounds(10,10,348,150);
        panel.setLayout(null);
        container.add(panel);

        JLabel label = new JLabel("Hello World!");
        label.setFont(new Font("Verdana",Font.BOLD,14));
        label.setBounds(85,25,280,30);
        panel.add(label);

        progressBar.setMaximum(50);
        progressBar.setBounds(55,180,250,15);
        container.add(progressBar);
        loadProgressBar();
        setSize(370,215);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void loadProgressBar() {
        ActionListener al = new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                count++;
                progressBar.setValue(count);
                if (count == 300) {
                    timer1.stop();
                    execute.setVisible(false);
                    return;
                }
            }
        };
        timer1 = new Timer(50,al);
        timer1.start();
    }

    public static void main(String[] args) {

        execute = new SplashScreen();

        ApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:/Meta-INF/spring/applicationContext.xml");

        UserDao userDao = context.getBean(UserDao.class);

        isRegistered = userDao.isRegistered();


        if (isRegistered) {
             // show frame 1
        } else {
                                                    // show frame 2

        }

    }

}

Solution

The following code seems to work well (there are fatal defects, counters may take longer, files load, and vice versa):

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionListener;
import javax.swing.*;

public class SplashScreen extends JWindow {

    static boolean isRegistered;
    private static JProgressBar progressBar = new JProgressBar();
    private static SplashScreen execute;
    private static int count;
    private static Timer timer1;

    public SplashScreen() {

        Container container = getContentPane();
        container.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new javax.swing.border.EtchedBorder());
        panel.setBackground(new Color(255,215);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void loadProgressBar() {
        ActionListener al = new ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                count++;

                progressBar.setValue(count);

                System.out.println(count);

                if (count == 300) {

                    createFrame();

                    execute.setVisible(false);//swapped this around with timer1.stop()

                    timer1.stop();
                }

            }

            private void createFrame() throws HeadlessException {
                JFrame frame = new JFrame();
                frame.setSize(500,500);
                frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        };
        timer1 = new Timer(50,al);
        timer1.start();
    }

    public static void main(String[] args) {
        execute = new SplashScreen();
    }
};

You should use the task to view progressmonitor and progressmonitorinputstream, and then you can check when the file is fully read and end splashScreen See here for some great tutorials and explanations

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>