2012-10-30 30 views
1

我有一个程序,通过我在主函数中调用的函数通过swing获取用户的输入。提交按钮具有附加的操作执行方法。我试图在删除输入文件并设置文本以通知用户后重新绘制屏幕。它不会重新绘制,直到try/catch中带有自定义函数。不知道我在做什么错我虽然它会按顺序执行?以下是我的行动预先附加到我的提交按钮。一个注意事项是,如果我做一个frame.dispose()或setVisibility(假)它将删除框架,任何帮助,将不胜感激。谢谢!!JFrame不会重绘,直到try/catch完成后

button.addActionListener(new ActionListener(){ 

       public void actionPerformed(ActionEvent e) { 
        loc = FileLoc.getText(); 
        name = FileName.getText(); 

        //inform user 
        area.setText("Attempting To Run Test...."); 
        //backGroundPane contains the user input fields and button  
        frame.remove(backGroundPane); 
        frame.repaint(); 

        if(loc != null && name != null && 
          !loc.equals("") && !name.equals("")) 
        { 
         try { 
          CallDrivers(); 
         } catch (InterruptedException e1) { 
          System.out.println("Error Running Function"); 
          //e1.printStackTrace(); 
         } 
        } 
        else{ 
         area.setText("There are Blank Fields"); 
         System.out.println("test"); 
        } 
       }}); 

回答

3

您正在阻止EDT(事件调度线程)。

事件调度线程负责按发布的顺序一次调度一个UI事件。事件可以是其中包括:

  • 关键事件(用户按下的琴键)
  • 鼠标事件(用户移动鼠标为例)
  • 调用事件(你叫SwingUtilities.invokeLater()或JComponent.repaint()为例如)
  • Paint事件(请求于涂抹部件)
  • 动作事件(逻辑事件通过中发生的InputEvent的触发)

当您调用repaint时,您正在队列上推送一个事件,但只要当前事件(actionPerformed的一个事件)未完成,重绘就不会发生。这就是为什么你的try/catch已完成

后您的重绘只发生在这里阅读更多:http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html

+0

这似乎只是想我需要谢谢! – Lazadon

相关问题