`
杨胜寒
  • 浏览: 284607 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java Swing创建自定义闪屏:在闪屏上添加Swing进度条控件(二)

阅读更多

本文将讲解如何做一个类似MyEclipse启动画面的闪屏,为Java Swing应用程序增添魅力。

 

首先看一下效果图吧,

 

 

 

原理很简单,就是创建一个Dialog,Dialog有一个进度条和一个Label用来分别显示进度和进度信息,而Dialog的宽度和高度正是闪屏图片的宽度和高度。然后将闪屏图片贴到Dialog中作为整个窗体的背景,Dialog显示时覆盖闪屏所处的区域。由于Dialog显示时闪屏并没有消失,且Dialog的X、Y及宽高都与闪屏图片一致,因此实际切换时,非常流畅,用户感觉不到有异常,就像从闪屏上“长”了两个Java Swing控件出来一样,非常自然。

 

看一下代码调用:

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ysh.studio.gui.window;

import javax.swing.JFrame;
import javax.swing.UIManager;

/**
 *
 * @author 杨胜寒
 */
public class SplashProcess {

    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
        //初始化闪屏Dialog时指定闪屏图片
        final BackgroundDialog splashWindow = new BackgroundDialog("/cn/ysh/studio/gui/resources/splash.jpg");
        //启动一个线程来加载数据
        new Thread() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 10; i++) {
                        splashWindow.updateProcess("正在进行第" + i + "次缓存数据加载. . .", i * 9);
                        Thread.sleep(300);
                    }
                } catch (InterruptedException ex) {
                    //异常不做处理
                }
                JFrame window = new MainWindow();
                splashWindow.updateProcess("正在启动主窗体. . .", 100);
                SwingUtils.moveToScreenCenter(window);
                splashWindow.setVisible(false);
                //数据加载完成,显示主窗体
                window.setVisible(true);
                //释放资源
                splashWindow.dispose();
            }
        }.start();
        //显示闪屏Dialog
        splashWindow.setVisible(true);
    }
}

 

 

上述代码分四部分,
一、设置Java Swing外观风格;
二、创建一个闪屏Dialog;
三、启动一个加载数据的线程;
四、显示闪屏Dialog

 

下面看看闪屏Dialog BackgroundDialog的代码

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ysh.studio.gui.window;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.LayoutStyle;

/**
 *
 * @author 杨胜寒
 */
public class BackgroundDialog extends JDialog {

    private ImageIcon background;
    private JProgressBar progressBar;
    private JLabel progressInfo;

    public BackgroundDialog(String splashPath) {
        super(new JFrame(), true);
        //鼠标形状为等待,告知用户程序已经在很努力的加载了,此时不可操作
        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        //背景图片
        background = new ImageIcon(BackgroundDialog.class.getResource(splashPath));
        JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
        //把标签的大小位置设置为图片刚好填充整个面板
        label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
        //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
        ((JPanel) getContentPane()).setOpaque(false);
        //初始化窗体布局
        initUI();
        //取消窗体默认装饰
        this.setUndecorated(true);
        //把背景图片添加到分层窗格的最底层作为背景
        getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
        setSize(background.getIconWidth(), background.getIconHeight());
        //移至屏幕中央,覆盖闪屏区域
        SwingUtils.moveToScreenCenter(this);
    }

    /**
     * 初始化窗体UI,可以在这个方法中创建复杂的UI布局
     */
    private void initUI() {
        progressBar = new JProgressBar();
        progressInfo = new JLabel();
        progressInfo.setText(" ");
        progressInfo.setForeground(new java.awt.Color(204, 0, 204));
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(progressBar, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE).addComponent(progressInfo, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap(265, Short.MAX_VALUE).addComponent(progressInfo, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)));

    }

    public void updateProcess(String info, int value) {
        progressInfo.setText(info);
        progressBar.setValue(value);
    }
}

 

 

注释写的很清楚,不再赘述,且BackgroundDialog支持更复杂和更有创意的界面设计,比如动画等特效,在initUI方法中实现即可。

 

 


原创文章,转载请注明出处: http://yshjava.iteye.com/blog/1326539

 

 

 

  • 大小: 50 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics