bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Java Swing 之 SplashScreen 歡迎視窗
查看: 5654|回復: 1
go

Swing 之 SplashScreen 歡迎視窗 [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2007-11-28 10:49 |只看該作者 |倒序瀏覽 |打印
從啟動程式到顯示主畫面的期間,常常會需要處理花費較長時間的作業‧若是沒有提供比較人性化的訊息告知執行狀況,可能會讓使用者誤解成沒有執行而重複啟動,更是雪上加霜‧

我們可以利用下列程式碼,先顯示歡迎視窗讓使用者知道程式已啟動,來改善這個問題:
  1. import java.awt.*;
  2. import javax.swing.*;

  3. public class SplashScreen extends Window {
  4.         JLabel jLabel1 = new JLabel();
  5.         JLabel jLabelDWN = new JLabel();
  6.         public SplashScreen() {
  7.                 super(new Frame());
  8.                 try {
  9.                         jbInit();
  10.                 }
  11.                 catch (Exception e) {
  12.                         e.printStackTrace();
  13.                 }
  14.         }

  15.         private void jbInit() throws Exception {
  16.                 this.setSize(new Dimension(400, 200));
  17.                 this.setVisible(false);
  18.                 //this.setVisible(true);
  19.                 jLabel1.setDoubleBuffered(true);
  20.                 //此例是假設我的色彩循環動畫只有400x10的大小罷了
  21.                 jLabel1.setPreferredSize(new Dimension(400,190));
  22.                 jLabelDWN.setDoubleBuffered(true);
  23.                 jLabelDWN.setPreferredSize(new Dimension(400,10));
  24.                 jLabelDWN.setOpaque(true);
  25.                 jLabel1.setIcon(new ImageIcon(exgui.UIHelper.getFolderURL(你的上半部畫面圖像檔)));
  26.                 jLabelDWN.setIcon(new ImageIcon(exgui.UIHelper.getFolderURL(你的下半部畫面圖像檔)));
  27.                 this.add(jLabel1, BorderLayout.CENTER);
  28.                 this.add(jLabelDWN, BorderLayout.SOUTH);
  29.         }
  30. }
複製代碼


3. 重點來了,啟動點 main() 的所在

  1. import java.awt.*;
  2. import javax.swing.JFrame;
  3. class Starter {
  4.         static SplashScreen splasher = new SplashScreen();
  5.         JFrame frame;
  6.         Starter() {
  7.                 frame = new JFrame();
  8.                 ...
  9.                 ...
  10.                 ...
  11.                 ...
  12.                 frame.setVisible(true);
  13.                 splasher.hide(); //application已完成顯示程序,快閃視窗功成身退.
  14.         }

  15.         public static void main(String[] args) {
  16.                 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  17.                 splasher.setLocation(
  18.                         (screenSize.width - 400) / 2,
  19.                         (screenSize.height - 200) / 2); //假設快閃畫面的大小是400x200,欲置於螢幕中央
  20.                 splasher.setSize(new Dimension(400,200));
  21.                 splasher.setSize(400,200);
  22.                 splasher.show(); //快閃畫面顯示
  23.                 ...
  24.                 ...其他資源分配,如jdbc,或是其他吃重的資源RMI/EJB,或是其他暫存性資料的準備
  25.                 ...
  26.                 Starter myStart = new Starter(); //application顯示
  27.         }
  28. }
複製代碼
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw

Rank: 9Rank: 9Rank: 9

2#
發表於 2007-11-28 11:32 |只看該作者
具有進度條的程式碼

  1. package test;

  2. import javax.swing.*;
  3. import java.awt.*;
  4. import com.borland.jbcl.layout.*;
  5. import javax.swing.border.*;

  6. class ProgressPanel extends JPanel {
  7.         JLabel jTip = new JLabel();
  8.         JProgressBar jProgress = new JProgressBar();
  9.         XYLayout xYLayout1 = new XYLayout();
  10.         TitledBorder titledBorder1;
  11.         public ProgressPanel() {
  12.                 try {
  13.                         jbInit();
  14.                 }
  15.                 catch(Exception e) {
  16.                         e.printStackTrace();
  17.                 }
  18.         }

  19.         private void jbInit() throws Exception {
  20.                 titledBorder1 = new TitledBorder("");
  21.                 jTip.setText("處理中...");
  22.                 jTip.setFont(new java.awt.Font("Dialog", 0, 12));
  23.                 this.setLayout(xYLayout1);
  24.                 jProgress.setOrientation(JProgressBar.HORIZONTAL);
  25.                 jProgress.setForeground(new Color(0, 0, 105));
  26.                 jProgress.setBorder(BorderFactory.createLoweredBevelBorder());
  27.                 jProgress.setStringPainted(true);
  28.                 xYLayout1.setWidth(258);
  29.                 xYLayout1.setHeight(75); bitsCN.Com
  30.                 this.setDebugGraphicsOptions(0);
  31.                 this.add(jTip, new XYConstraints(10, 1, -1, -1));
  32.                 this.add(jProgress, new XYConstraints(5, 16, 249, 24));
  33.         }

  34.         public static void main(String[] args) {
  35.                 ProgressPanel progressPanel1 = new ProgressPanel();
  36.         }
  37. }
複製代碼
  1. package test;

  2. import java.awt.Window;
  3. import java.awt.Dimension;
  4. import java.awt.Toolkit;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import java.awt.Rectangle;
  8. import javax.swing.SwingConstants;
  9. import java.awt.GridBagLayout;

  10. public class WaitingFrame {
  11.         protected static Window winInc;
  12.         private static WaitingFrame inc;

  13.         private static JLabel jWaitTip;
  14.         private static ProgressPanel proPanel;

  15.         private static final String WAITING_TIP = "系統正在處理中,請稍後...";
  16.         private static final String PROGRESS_TIP = "處理中...";

  17.         private static final int DEFAULT_WIDTH = 260;
  18.         private static final int DEFAULT_HEIGHT = 45;

  19.         private static int DEFAULT_LOCATEX;
  20.         private static int DEFAULT_LOCATEY;

  21.         private static boolean resetFlg = false;

  22.         private static int maxProgress = 100;
  23.         //當前顯示的視窗類型
  24.         private int curType;

  25.         /**
  26.         * 等待視窗
  27.         */
  28.         public static final int WAITING = 0;

  29.         /**
  30.         * 進度條視窗
  31.         */
  32.         public static final int PROGRESS = 1;

  33.         private WaitingFrame() {
  34.         }

  35.         /**
  36.         * 獲取提示視窗實例
  37.         * @param aType:視窗類型
  38.         * @return
  39.         */
  40.         public synchronized static WaitingFrame getInstance(int aType){
  41.                 if(aType==PROGRESS){
  42.                         if(null==proPanel){
  43.                                 proPanel = new ProgressPanel();
  44.                         }
  45.                 }else{
  46.                         if(null==jWaitTip){
  47.                                 jWaitTip = new JLabel();
  48.                                 jWaitTip.setFont(new java.awt.Font("Dialog", 0, 16));
  49.                                 jWaitTip.setHorizontalAlignment(SwingConstants.CENTER);
  50.                                 jWaitTip.setText(WAITING_TIP);
  51.                                 jWaitTip.setBounds(new Rectangle(0, 0, 280, 98));
  52.                         }
  53.                 }
  54.                 if(null==inc){
  55.                         inc = new WaitingFrame();
  56.                         JFrame frm = new JFrame();
  57.                         frm.getContentPane().setLayout(new GridBagLayout());
  58.                         winInc = new Window(frm);
  59.                         if(aType==PROGRESS){
  60.                                 inc.curType = aType;
  61.                                 proPanel.jProgress.setMaximum(maxProgress);
  62.                                 proPanel.jProgress.setValue(0);
  63.                                 winInc.add(proPanel);
  64.                         }else{
  65.                                 inc.curType = WAITING;
  66.                                 winInc.add(jWaitTip);
  67.                         }
  68.                         winInc.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  69.                         Dimension sysSize = Toolkit.getDefaultToolkit().getScreenSize();
  70.                         DEFAULT_LOCATEX = (sysSize.width-DEFAULT_WIDTH)/2;
  71.                         DEFAULT_LOCATEY = (sysSize.height-DEFAULT_HEIGHT)/2;
  72.                 }else{
  73.                         if (aType == PROGRESS) {
  74.                                 winInc.removeAll();
  75.                                 proPanel.jTip.setText(PROGRESS_TIP);
  76.                                 proPanel.jProgress.setValue(0);
  77.                                 winInc.add(proPanel);
  78.                         }else{
  79.                                 winInc.removeAll();
  80.                                 jWaitTip.setText(WAITING_TIP);
  81.                                 winInc.add(jWaitTip);
  82.                         }
  83.                 }
  84.                 inc.curType = aType;
  85.                 inc.resetFlg = false;
  86.                 return inc;
  87.         }

  88.         /**
  89.          * 關閉顯示的等待視窗
  90.          */
  91.         public void close(){
  92.                 if(null != winInc && winInc.isShowing())
  93.                         winInc.dispose();
  94.         }

  95.         /**
  96.          * 關閉顯示的等待視窗
  97.          */
  98.         public void dispose(){
  99.                 close();
  100.         }

  101.         /**
  102.          * 設置提示文本
  103.          * @param aTip:提示文本
  104.          */
  105.         public void setTip(String aTip){
  106.                 if(this.curType==PROGRESS){
  107.                         proPanel.jTip.setText(aTip);
  108.                 }else{
  109.                         jWaitTip.setText(aTip);
  110.                 }
  111.                 resetFlg = true;
  112.                 winInc.validate();
  113.         }

  114.         /**
  115.          * 設置進度條的最大值
  116.          * @param aValue 最大值
  117.          */
  118.         public void setMaxValue(int aValue){
  119.                 if (curType == PROGRESS) {
  120.                         if(aValue>0)
  121.                                 maxProgress = aValue;
  122.                         else
  123.                                 maxProgress = 100;
  124.                         proPanel.jProgress.setMaximum(maxProgress);
  125.                 }
  126.                 else {
  127.                         throw new RuntimeException("等待視窗不支持此函數,只有進度條視窗才支持!");
  128.                 }
  129.         }

  130.         /**
  131.          * 設置當前進度
  132.          * @param aValue 當前進度值
  133.          */
  134.         public void setProgress(int aValue){
  135.                 proPanel.jProgress.setValue(aValue); bitscn.com
  136.                 winInc.validate();
  137.         }

  138.         /**
  139.          * 顯示視窗
  140.          * @param aTipText 文本提示
  141.          * @param aLocx 視窗位置x
  142.          * @param aLocy 視窗位置y
  143.          */
  144.         public void show(String aTipText,int aLocx,int aLocy){
  145.                 setTip(aTipText);
  146.                 winInc.setLocation(aLocx,aLocy);
  147.                 winInc.show();
  148.         }

  149.         /**
  150.          * 顯示視窗
  151.          * @param aTipText 文本提示
  152.          */
  153.         public void show(String aTipText){ www_bitscn_com
  154.                 show(aTipText,this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
  155.         }

  156.         /**
  157.          * 顯示視窗
  158.          * @param aLocx 視窗位置x
  159.          * @param aLocy 視窗位置y
  160.          */
  161.         public void show(int aLocx,int aLocy){
  162.                 if(curType==PROGRESS)
  163.                         show(this.PROGRESS_TIP,aLocx,aLocy);
  164.                 else
  165.                         show(this.WAITING_TIP,aLocx,aLocy);
  166.         }

  167.         /**
  168.          * 顯示視窗
  169.          */
  170.         public void show(){
  171.                 if(resetFlg){
  172.                         if(curType==PROGRESS)
  173.                                 show(proPanel.jTip.getText(),this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
  174.                         else
  175.                                 show(jWaitTip.getText(),this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
  176.                 }else{
  177.                         if(curType==PROGRESS)
  178.                                 show(this.PROGRESS_TIP,this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
  179.                         else
  180.                                 show(this.WAITING_TIP,this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
  181.                 }
  182.         }

  183.         public static void main(String[] args) {
  184.           //進度條測試
  185.                 WaitingFrame frm = WaitingFrame.getInstance(WaitingFrame.PROGRESS);
  186.                 frm.setMaxValue(100);
  187.                 frm.show();
  188.                 int i = 0;
  189.                 while (i++ < 100) {
  190.                         frm.setProgress(i);
  191.                         try {
  192.                                 Thread.sleep(100);
  193.                         }
  194.                         catch (InterruptedException ex) {
  195.                         }
  196.                 }
  197.                 frm.dispose();

  198.            //等待視窗測試
  199.                 frm = WaitingFrame.getInstance(WaitingFrame.WAITING);
  200.                 frm.show();
  201.                 try {
  202.                   Thread.sleep(10000);
  203.                 }
  204.                 catch (InterruptedException ex1) {
  205.                 }
  206.                 frm.dispose();
  207.         }
  208. }
複製代碼
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-5-17 20:19 , Processed in 0.014759 second(s), 10 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.