当前位置: 首页 > 面试题库 >

使用进度条(如Eclipse)制作启动屏幕

太叔岳
2023-03-14
问题内容

我的主类从文件加载配置,然后显示一个框架。我想制作一个带有进度条(如Eclipse)的启动画面,以便在加载文件时进度会增加,并且在加载文件后启动画面会消失。然后我的主机被加载。

MainClass代码:

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
  }
}

我在Swing方面没有太多经验,所以请提出建议。

更新: 我发现了以下示例,但没有什么问题:

  • 当计数器达到指定的数字时,它应停止在(300)位置,直到不停止计时器并隐藏启动画面而永远计数。

  • 我想将计数器绑定到文件加载,因此,在加载文件时,将加载进度,直到加载文件,然后进度完成,并且初始屏幕消失。

    @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
    
        }
    
    }
    

    }


问题答案:

当计数器达到指定的数字时,它应停止在(300)位置,直到不停止计时器并隐藏启动画面时,它一直保持计数。

下面的代码似乎很好用(存在致命缺陷,计数器可能花费比文件加载更长的时间,反之亦然):

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, 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);
    }

    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();
    }
};

我想将计数器绑定到文件加载,因此,在加载文件时,将加载进度,直到加载文件,然后进度完成,并且初始屏幕消失。

您应该查看一下,ProgressMonitor然后ProgressMonitorInputStream使用,Task然后检查何时完全读取了文件并结束SplashScreen。看到这里一些很好的教程和解释



 类似资料:
  • 问题内容: 如何在html / css /javascript中制作进度条。我真的不想使用Flash。在这里可以找到类似的东西 我真正想要的只是一个“进度条”,它可以更改我在PHP中提供的值。您会如何处理?有什么好的教程吗? 问题答案: 您可以通过css控制div的宽度来实现。大致遵循以下原则: 如果您愿意,可以从php发送该宽度值。

  • 本文向大家介绍如何使用纯html制作一个进度条?相关面试题,主要包含被问及如何使用纯html制作一个进度条?时的应答技巧和注意事项,需要的朋友参考一下 HTML中的progress () 元素用来显示一项任务的完成进度.虽然规范中没有规定该元素具体如何显示,浏览器开发商可以自己决定,但通常情况下,该元素都显示为一个进度条形式. 所有不建议在生产业务中使用 MDN链接

  • 问题内容: 我正在尝试解决程序中的问题,而这个问题是当我开始下载视频时程序没有响应,我也看不到进度栏移动,因此我尝试使用线程模块,但无法解决问题,因此我可以解决问题 通过此代码,我可以下载视频并将数据发送到另一个函数,以检索用于将其连接到进度条的信息 该代码是从视频功能接收到的信息,用于与进度条连接 我使用; python3.5 pyqt5 pafy 问题答案: 转移到另一个线程的一种方法是创建一

  • 本文向大家介绍请使用纯HTML制作一个进度条相关面试题,主要包含被问及请使用纯HTML制作一个进度条时的应答技巧和注意事项,需要的朋友参考一下 progress是HTML5的一个新元素,表示定义一个进度条,用途很广泛,可以用在文件上传的进度显示,文件下载的进度显示,也可以作为一种loading的加载状态条使用。 max属性表示进度条的进度最大值,如果有此值,必须是大于0的有效浮点数。max的默认值

  • 我是swift和Xcode新手,我想知道是否有人愿意向我解释,我应该如何让我的启动屏幕显示3秒钟,然后在这段时间内稍微淡出? 我想做一个我以前构建的网页的web视图,但希望它在启动屏幕之前淡出。 我已经阅读了所有其他与该主题相关的问题,但我不理解它们。我也一直在关注一些关于这个主题的教程,但什么都没有。 有人吗,拜托?

  • 本文向大家介绍使用golang实现在屏幕上打印进度条的操作,包括了使用golang实现在屏幕上打印进度条的操作的使用技巧和注意事项,需要的朋友参考一下 GoSimplePrint 是一款用go写的开源简单进度条打印包。我可以利用它,在自己项目中需要加入进度条功能。 1、安装 2、初始化 bar:=goPrint.NewBar(20) 这里的20,是我们满进度条的数值。如果我们要让进度条走满的时候值