| 发表于:2007-03-16 15:40:012楼 得分:50 |
/** * @author xuwanhong_sun@hotmail.com * @version 1.0.0 */ package org.mysoft.jsqgui; import java.awt.*; import javax.swing.*; import java.util.*; public class waitthread { public static jdialog dialog; private dimension dimensions = toolkit.getdefaulttoolkit().getscreensize(); private int width = dimensions.width * 3 / 10, height = 75; private window parent; private string message; private actionthread actionthread; private map threads; public waitthread(window parent,string message) { this.parent = parent; this.message = message; initgui(); } public waitthread(window parent,string message,map threads) { this.parent = parent; this.message = message; this.threads = threads; initgui(); } protected void initgui() { if(parent instanceof frame) { dialog = new jdialog((frame)parent, "请稍等 " + "... ", true); } else if(parent instanceof dialog) { dialog = new jdialog((dialog)parent, "请稍等 " + "... ", true); } else { dialog = new jdialog(); dialog.settitle( "请稍等 " + "... "); dialog.setmodal(true); } dialog.setcursor(new cursor(cursor.wait_cursor)); jlabel infolabel = new jlabel(message, jlabel.center); jpanel infopanel = new jpanel(new borderlayout()); infopanel.add(infolabel, borderlayout.center); dialog.getcontentpane().add(infopanel); int left = (dimensions.width - width)/2; int top = (dimensions.height - height)/2; dialog.setsize(new dimension(width,height)); dialog.setlocation(left, top); } public void start() { swingutilities.invokelater(new startthread()); } public void startmore() { swingutilities.invokelater(new startmorethread()); } public void actionperformed() { } class startthread extends thread { public void run() { new showdialog().start(); actionthread = new actionthread(); actionthread.start(); new closedialog().start(); } } class startmorethread extends thread { public void run() { new showdialog().start(); if (threads != null) { for (iterator iter = threads.values().iterator(); iter.hasnext();) { ((thread)iter.next()).start(); } } new closedialog().start(); } } class actionthread extends thread { public void run() { actionperformed(); } } class showdialog extends thread { public void run() { dialog.show(); } } class closedialog extends thread { public void run() { if (actionthread != null) { try { actionthread.join(); } catch(interruptedexception e) { e.printstacktrace(system.out); } } else if(threads != null) { thread tmpthread = null; while (true) { try { thread.currentthread().sleep(250); } catch(interruptedexception e) { e.printstacktrace(system.out); } for( iterator iter = threads.values().iterator(); iter.hasnext();) { tmpthread = (thread)iter.next(); if(!tmpthread.isalive()) { iter.remove(); } } if (threads.isempty()) { break; } } } dialog.dispose(); } } } 这个程序可以帮你完成你想要的功能,呵呵, | | |
|