java 使用 Eclipse 等进度条制作启动画面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11399971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make splash screen with progress bar like Eclipse
提问by Mahmoud Saleh
My main class loads configuration from a file then shows a frame. I want to make a splash screen with a progress bar like Eclipse so that the progress will increase while the file is being loaded and the splash disappears after the file is loaded. Then my main frame gets loaded.
我的主类从文件加载配置然后显示一个框架。我想制作一个带有 Eclipse 之类的进度条的闪屏,以便在加载文件时进度会增加,并且在加载文件后闪屏消失。然后我的主框架被加载。
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 experience with Swing, so please advise how to accomplish that.
我对 Swing 没有太多经验,所以请建议如何实现。
UPDATE:i have found the following example, but it have little issue:
更新:我找到了以下示例,但几乎没有问题:
when the counter gets to the specified number it should stop at (300) it keeps counting for ever without stopping the timer and hiding the splash screen.
i want to bind the counter to the file loading, so while the file is loaded the progress gets loaded until the file gets loaded then the progress completes and the splash 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 } } }
当计数器达到指定的数字时,它应该在 (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 } } }
采纳答案by David Kroukamp
when the counter gets to the specified number it should stop at (300) it keeps counting for ever without stopping the timer and hiding the splash screen.
当计数器达到指定的数字时,它应该在 (300) 处停止,它会一直计数而不会停止计时器并隐藏启动画面。
The code below seems to work great (with the fatal flaw the counter may take longer then the file loading 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, 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();
}
};
i want to bind the counter to the file loading, so while the file is loaded the progress gets loaded until the file gets loaded then the progress completes and the splash screen disappears.
我想将计数器绑定到文件加载,因此在加载文件时加载进度,直到文件加载然后进度完成并且启动画面消失。
You should take a look at ProgressMonitor
and ProgressMonitorInputStream
using a Task
you may then check when the file is completely read and end the SplashScreen
. see herefor some great tutorial and explanation
您应该查看ProgressMonitor
并ProgressMonitorInputStream
使用 ,Task
然后您可以检查文件何时被完全读取并结束SplashScreen
. 看到这里的一些伟大的教程和说明