当前页面: 开发资料首页 → Java 专题 → 等待提示框 
等待提示框 
摘要: 等待提示框  
                     </td>
              </tr>
              <tr>
                <td height="35" valign="top" class="ArticleTeitle"> import javax.swing.*; 
                  import java.awt.*; 
                  import java.awt.event.*; 
                  public class TestFrame extends JFrame{ 
                    public JPanel testPanel = null; 
                    public JButton testButton = null; 
                    public JFrame testFrame = null; 
                    public TestFrame(){ 
                       try 
                          { 
                            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
                       }catch (Exception ex) { 
                             System.out.println("Exception: " + ex); 
                       } 
                       testFrame = this; 
                       Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize(); 
                       setSize(dimensions.width /2, dimensions.height /2); 
                       setLocation(dimensions.width/2-dimensions.width/4,dimensions.height/2-dimensions.height/4); 
                       testPanel= new JPanel(); 
                       testButton= new JButton("开始线程"); 
                       testPanel.add(testButton); 
                       getContentPane().add(testPanel); 
                       testButton.addActionListener(new java.awt.event.ActionListener() { 
                         public void actionPerformed(ActionEvent e) { 
                             TestThread testThread = new TestThread();//新生成一个处理事务线程 
                             testThread.start(); 
                             (new ThreadDiag(testFrame,testThread,"正在执行程序,请等待")).start(); 
                         } 
                       }); 
                       addWindowListener(new WindowAdapter(){ 
                           public void windowClosing(WindowEvent e) { 
                            System.exit(0); 
                           } 
                        }); 
                   } 
                     public static void main(String[] args) { 
                          TestFrame testFrame2 = new TestFrame(); 
                          testFrame2.setTitle("线程等待测试"); 
                           testFrame2.show(); 
                     } 
                  } 
                  
                  class TestThread extends Thread{ 
                   public void run(){ 
                          for (int i = 1; i < 100000 ; i++ ){ 
                                 System.out.println(i); 
                          } 
                    } 
                  } 
                  
                  class ThreadDiag extends Thread{ 
                    private Thread currentThread = null;//实际调用时就是TestThread事务处理线程 
                    private String messages = "";//提示框的提示信息 
                    private JFrame parentFrame = null;//提示框的父窗体 
                    private JDialog clueDiag = null;// "线程正在运行"提示框 
                    private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize(); 
                    private int width = dimensions.width / 4, height = 60; 
                    private int left = 0, top = 0; 
                    public ThreadDiag(JFrame parentFrame, Thread currentThread, String messages){ 
                      this.parentFrame = parentFrame; 
                      this.currentThread = currentThread; 
                      this.messages = messages; 
                      initDiag();//初始化提示框 
                     } 
                  
                    protected void initDiag(){ 
                      clueDiag = new JDialog(parentFrame,"正在执行,请等待...",false); 
                      clueDiag.setCursor(new Cursor(Cursor.WAIT_CURSOR)); 
                      JPanel testPanel = new JPanel(); 
                      JLabel testLabel = new JLabel(messages); 
                      clueDiag.getContentPane().add(testPanel); 
                      testPanel.add(testLabel); 
                      (new DisposeDiag()).start();//启动关闭提示框线程 
                    } 
                    public void run(){ 
                     //显示提示框 
                     int left = (dimensions.width - width)/2; 
                     int top = (dimensions.height - height)/2; 
                     clueDiag.setSize(new Dimension(width,height)); 
                     clueDiag.setLocation(left, top); 
                     clueDiag.show(); 
                    } 
                    
                   class DisposeDiag extends Thread{ 
                     public void run(){ 
                     try{ 
                           currentThread.join();//等待事务处理线程结束 
                     }catch(InterruptedException e){ 
                          System.out.println("Exception:" + e); 
                     }  
                     clueDiag.dispose();//关闭提示框 
                    } 
                   } 
                } </td>
              </tr>
              <tr>
                
                    
 
↑返回目录 
前一篇: 使用安全管理器运行程序  
 
后一篇: 椭园按钮