2012-05-09 23 views
2

我正在使用JprogressBar的應用程序,當我正在執行某些操作時,進度條正在運行。我還在那裏添加了逃生功能。當任何人單擊鍵盤上的退出按鈕然後集中Jpanel/Jdialog/JFrame處置。它工作正常。我的問題是JprogressBar沒有停止。我想這樣做,如果任何Jpanel/Jdialog/JFrame通過轉義按鈕被關閉,那麼如果它的父母有JProgressBar那麼它應該被停止。我怎樣才能做到這一點?所有Jpanel/Jdialog/JFrame的父母可以有所不同?如何停止父級上的JProgressBar?

我的逃生功能如上:

public static void setDisposeOnEsc(JComponent c, final Object promptControl) { 
     Action escape = new AbstractAction() { 

      { 
       putValue(NAME, "escape"); 
      } 

      public void actionPerformed(ActionEvent e) { 
        JComponent source = (JComponent) e.getSource(); 
        try { 
         Window window = SwingUtilities.getWindowAncestor(source); 
         window.dispose(); 
        } catch (Exception ex) { 
        } 
        try { 
         Dialog dialog = (Dialog) source.getFocusCycleRootAncestor(); 
         dialog.dispose(); 
        } catch (Exception ex) { 
        } 
        try { 
         JFrame jFrame = (JFrame) source.getFocusCycleRootAncestor(); 
         jFrame.dispose(); 
        } catch (Exception ex) { 
        } 
      } 
     }; 
     Object name = escape.getValue(Action.NAME); 
c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"),name); 
     c.getActionMap().put(name, escape); 
    } 

我想這樣做,如果它得到安置,通過上述方法,那麼如果它的父任何的JPanel /的JDialog/JFrame中具有的JProgressBar並運行,然後它應該停止。

我的英語不太好,所以請忽略語法錯誤。

在此先感謝。

+5

更好儘快用[SSCCE](http://sscce.org/)編輯您的問題,這表明您在[KeyBindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding。 html)和[JProgressBar](http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html),因爲在代碼中我看不到任何人 – mKorbel

回答

1

我不能完全肯定我理解,但你可以使用類似

public static <T extends Component> List<T> findAllChildren(JComponent component, Class<T> clazz) { 

    List<T> lstChildren = new ArrayList<T>(5); 
    for (Component child : component.getComponents()) { 

     if (clazz.isInstance(child)) { 

      lstChildren.add((T) child); 

     } 

     if (child instanceof JComponent) { 

      lstChildren.addAll(findAllChildren((JComponent)child, clazz)); 

     } 

    } 

    return Collections.unmodifiableList(lstChildren); 

} 

要找到所有JProgressBar的引用,從那裏你可以做你想做的......

相關問題