bestlong 怕失憶論壇
標題:
Swing 之 SplashScreen 歡迎視窗
[打印本頁]
作者:
bestlong
時間:
2007-11-28 10:49
標題:
Swing 之 SplashScreen 歡迎視窗
從啟動程式到顯示主畫面的期間,常常會需要處理花費較長時間的作業‧若是沒有提供比較人性化的訊息告知執行狀況,可能會讓使用者誤解成沒有執行而重複啟動,更是雪上加霜‧
我們可以利用下列程式碼,先顯示歡迎視窗讓使用者知道程式已啟動,來改善這個問題:
import java.awt.*;
import javax.swing.*;
public class SplashScreen extends Window {
JLabel jLabel1 = new JLabel();
JLabel jLabelDWN = new JLabel();
public SplashScreen() {
super(new Frame());
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(new Dimension(400, 200));
this.setVisible(false);
//this.setVisible(true);
jLabel1.setDoubleBuffered(true);
//此例是假設我的色彩循環動畫只有400x10的大小罷了
jLabel1.setPreferredSize(new Dimension(400,190));
jLabelDWN.setDoubleBuffered(true);
jLabelDWN.setPreferredSize(new Dimension(400,10));
jLabelDWN.setOpaque(true);
jLabel1.setIcon(new ImageIcon(exgui.UIHelper.getFolderURL(你的上半部畫面圖像檔)));
jLabelDWN.setIcon(new ImageIcon(exgui.UIHelper.getFolderURL(你的下半部畫面圖像檔)));
this.add(jLabel1, BorderLayout.CENTER);
this.add(jLabelDWN, BorderLayout.SOUTH);
}
}
複製代碼
3. 重點來了,啟動點 main() 的所在
import java.awt.*;
import javax.swing.JFrame;
class Starter {
static SplashScreen splasher = new SplashScreen();
JFrame frame;
Starter() {
frame = new JFrame();
...
...
...
...
frame.setVisible(true);
splasher.hide(); //application已完成顯示程序,快閃視窗功成身退.
}
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
splasher.setLocation(
(screenSize.width - 400) / 2,
(screenSize.height - 200) / 2); //假設快閃畫面的大小是400x200,欲置於螢幕中央
splasher.setSize(new Dimension(400,200));
splasher.setSize(400,200);
splasher.show(); //快閃畫面顯示
...
...其他資源分配,如jdbc,或是其他吃重的資源RMI/EJB,或是其他暫存性資料的準備
...
Starter myStart = new Starter(); //application顯示
}
}
複製代碼
作者:
bestlong
時間:
2007-11-28 11:32
具有進度條的程式碼
package test;
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;
class ProgressPanel extends JPanel {
JLabel jTip = new JLabel();
JProgressBar jProgress = new JProgressBar();
XYLayout xYLayout1 = new XYLayout();
TitledBorder titledBorder1;
public ProgressPanel() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
titledBorder1 = new TitledBorder("");
jTip.setText("處理中...");
jTip.setFont(new java.awt.Font("Dialog", 0, 12));
this.setLayout(xYLayout1);
jProgress.setOrientation(JProgressBar.HORIZONTAL);
jProgress.setForeground(new Color(0, 0, 105));
jProgress.setBorder(BorderFactory.createLoweredBevelBorder());
jProgress.setStringPainted(true);
xYLayout1.setWidth(258);
xYLayout1.setHeight(75); bitsCN.Com
this.setDebugGraphicsOptions(0);
this.add(jTip, new XYConstraints(10, 1, -1, -1));
this.add(jProgress, new XYConstraints(5, 16, 249, 24));
}
public static void main(String[] args) {
ProgressPanel progressPanel1 = new ProgressPanel();
}
}
複製代碼
package test;
import java.awt.Window;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.SwingConstants;
import java.awt.GridBagLayout;
public class WaitingFrame {
protected static Window winInc;
private static WaitingFrame inc;
private static JLabel jWaitTip;
private static ProgressPanel proPanel;
private static final String WAITING_TIP = "系統正在處理中,請稍後...";
private static final String PROGRESS_TIP = "處理中...";
private static final int DEFAULT_WIDTH = 260;
private static final int DEFAULT_HEIGHT = 45;
private static int DEFAULT_LOCATEX;
private static int DEFAULT_LOCATEY;
private static boolean resetFlg = false;
private static int maxProgress = 100;
//當前顯示的視窗類型
private int curType;
/**
* 等待視窗
*/
public static final int WAITING = 0;
/**
* 進度條視窗
*/
public static final int PROGRESS = 1;
private WaitingFrame() {
}
/**
* 獲取提示視窗實例
* @param aType:視窗類型
* @return
*/
public synchronized static WaitingFrame getInstance(int aType){
if(aType==PROGRESS){
if(null==proPanel){
proPanel = new ProgressPanel();
}
}else{
if(null==jWaitTip){
jWaitTip = new JLabel();
jWaitTip.setFont(new java.awt.Font("Dialog", 0, 16));
jWaitTip.setHorizontalAlignment(SwingConstants.CENTER);
jWaitTip.setText(WAITING_TIP);
jWaitTip.setBounds(new Rectangle(0, 0, 280, 98));
}
}
if(null==inc){
inc = new WaitingFrame();
JFrame frm = new JFrame();
frm.getContentPane().setLayout(new GridBagLayout());
winInc = new Window(frm);
if(aType==PROGRESS){
inc.curType = aType;
proPanel.jProgress.setMaximum(maxProgress);
proPanel.jProgress.setValue(0);
winInc.add(proPanel);
}else{
inc.curType = WAITING;
winInc.add(jWaitTip);
}
winInc.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
Dimension sysSize = Toolkit.getDefaultToolkit().getScreenSize();
DEFAULT_LOCATEX = (sysSize.width-DEFAULT_WIDTH)/2;
DEFAULT_LOCATEY = (sysSize.height-DEFAULT_HEIGHT)/2;
}else{
if (aType == PROGRESS) {
winInc.removeAll();
proPanel.jTip.setText(PROGRESS_TIP);
proPanel.jProgress.setValue(0);
winInc.add(proPanel);
}else{
winInc.removeAll();
jWaitTip.setText(WAITING_TIP);
winInc.add(jWaitTip);
}
}
inc.curType = aType;
inc.resetFlg = false;
return inc;
}
/**
* 關閉顯示的等待視窗
*/
public void close(){
if(null != winInc && winInc.isShowing())
winInc.dispose();
}
/**
* 關閉顯示的等待視窗
*/
public void dispose(){
close();
}
/**
* 設置提示文本
* @param aTip:提示文本
*/
public void setTip(String aTip){
if(this.curType==PROGRESS){
proPanel.jTip.setText(aTip);
}else{
jWaitTip.setText(aTip);
}
resetFlg = true;
winInc.validate();
}
/**
* 設置進度條的最大值
* @param aValue 最大值
*/
public void setMaxValue(int aValue){
if (curType == PROGRESS) {
if(aValue>0)
maxProgress = aValue;
else
maxProgress = 100;
proPanel.jProgress.setMaximum(maxProgress);
}
else {
throw new RuntimeException("等待視窗不支持此函數,只有進度條視窗才支持!");
}
}
/**
* 設置當前進度
* @param aValue 當前進度值
*/
public void setProgress(int aValue){
proPanel.jProgress.setValue(aValue); bitscn.com
winInc.validate();
}
/**
* 顯示視窗
* @param aTipText 文本提示
* @param aLocx 視窗位置x
* @param aLocy 視窗位置y
*/
public void show(String aTipText,int aLocx,int aLocy){
setTip(aTipText);
winInc.setLocation(aLocx,aLocy);
winInc.show();
}
/**
* 顯示視窗
* @param aTipText 文本提示
*/
public void show(String aTipText){ www_bitscn_com
show(aTipText,this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
}
/**
* 顯示視窗
* @param aLocx 視窗位置x
* @param aLocy 視窗位置y
*/
public void show(int aLocx,int aLocy){
if(curType==PROGRESS)
show(this.PROGRESS_TIP,aLocx,aLocy);
else
show(this.WAITING_TIP,aLocx,aLocy);
}
/**
* 顯示視窗
*/
public void show(){
if(resetFlg){
if(curType==PROGRESS)
show(proPanel.jTip.getText(),this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
else
show(jWaitTip.getText(),this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
}else{
if(curType==PROGRESS)
show(this.PROGRESS_TIP,this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
else
show(this.WAITING_TIP,this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
}
}
public static void main(String[] args) {
//進度條測試
WaitingFrame frm = WaitingFrame.getInstance(WaitingFrame.PROGRESS);
frm.setMaxValue(100);
frm.show();
int i = 0;
while (i++ < 100) {
frm.setProgress(i);
try {
Thread.sleep(100);
}
catch (InterruptedException ex) {
}
}
frm.dispose();
//等待視窗測試
frm = WaitingFrame.getInstance(WaitingFrame.WAITING);
frm.show();
try {
Thread.sleep(10000);
}
catch (InterruptedException ex1) {
}
frm.dispose();
}
}
複製代碼
歡迎光臨 bestlong 怕失憶論壇 (http://www.bestlong.idv.tw/)
Powered by Discuz! X1.5