当计数器达到指定的数字时,它应停止在(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。看到这里一些很好的教程和解释



