在很多應用中,假如某一處理需要花費的時間比較長,應用應該提供一個比較人性化的提示。假如這個處理是由一個循環來實現的,則可以采用一個任務進度提示,其它情況可能就采用彈出一個類似于"系統正在處理,請稍后..."提示框的方式來達到目的。以下的代碼簡單實現了這一功能:
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();
}
}
public static void main(String[] args) {
ProgressPanel progressPanel1 = new ProgressPanel();
}
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);
this.setDebugGraphicsOptions(0);
this.add(jTip, new XYConstraints(10, 1, -1, -1));
this.add(jProgress, new XYConstraints(5, 16, 249, 24));
}
}
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;
}
新聞熱點
疑難解答