等待窗体的一种实现
2008-01-05 08:28:16 来源:WEB开发网 在很多应用中,假如某一处理需要花费的时间比较长,应用应该提供一个比较人性化的提示。假如这个处理是由一个循环来实现的,则可以采用一个任务进度提示,其它情况可能就采用弹出一个类似于"系统正在处理,请稍后..."提示框的方式来达到目的。以下的代码简单实现了这一功能:
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;
}
更多精彩
赞助商链接