Java frame not displaying properly during a while loop

后端 未结 2 2075
無奈伤痛
無奈伤痛 2020-12-12 06:33

Here\'s a serious issue im having with a jFrame. I have one \"Main Frame\" where everything is done from, however i have another frame with a gif image in it (Loading, that

相关标签:
2条回答
  • 2020-12-12 06:39

    You use Timer Class in java.util package and schedule timertask to close the splash screen after some time say 2 min or the task is completed.

     final Processing nc = new Processing();
     nc.setVisible(true);
     Timer timer = new Timer():
     TimerTask task = new TimerTask() {
          public void run() { 
              nc.setVisible( false );
              // to do disposing nc
          }
     };
     timer.schedule( task, 1000 * 3 ); // dispose the processing frame after 3 minutes
    
    0 讨论(0)
  • 2020-12-12 06:47

    Without knowing the full extent of what you are trying to do, it's not entirely possible to you an exact answer.

    From the sounds of things your trying to run long running tasks within the context of the Event Dispatching Thread. This thread is responsible for, amongst other things, processing repaint requests.

    Blocking this thread, prevents Swing from repainting itself.

    From what I can tell, you want to use a SwingWorker. This will allow you to performing your work in a background thread, while re syncing updates back to the EDT

    Updated with example

    I know you said didn't "want" to use a SwingWorker, but to be honest, it's just simpler and safer...

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingWorker;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class LongRunningTask {
    
        public static void main(String[] args) {
            new LongRunningTask();
        }
    
        public LongRunningTask() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    new BackgroundWorker().start();
    
                }
            });
        }
    
        public class BackgroundWorker extends SwingWorker<Object, Object> {
    
            private JFrame frame;
    
            public BackgroundWorker() {
            }
    
            // Cause exeute is final :P
            public void start() {
                ImageIcon icon = new ImageIcon(getClass().getResource("/spinner.gif"));
                JLabel label = new JLabel("This might take some time");
                label.setIcon(icon);
    
                frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(label);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
    
                execute();
            }
    
            @Override
            protected Object doInBackground() throws Exception {
    
                System.out.println("Working hard...");
                Thread.sleep(1000 + (((int)Math.round(Math.random() * 5)) * 1000));
                System.out.println("Or hardly working...");
    
                return null;
    
            }
    
            @Override
            protected void done() {
                frame.dispose();
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题