Java 如何在父级上停止JProgressBar?
我正在使用JprogressBar开发一个应用程序,当我做一些事情时,进度条正在运行。我还添加了转义功能。当任何人点击键盘上的Escape按钮时,焦点Jpanel/Jdialog/JFrame被释放。它工作正常。我的问题是JprogressBar没有停止。我想这样做,如果任何Jpanel/Jdialog/JFrame通过escape按钮被关闭,那么如果它的父级有JProgressBar,那么它应该被停止。我该怎么做?所有Jpanel/Jdialog/JFrame的父级可以不同吗 我的转义功能如上所述:Java 如何在父级上停止JProgressBar?,java,swing,jframe,jpanel,jprogressbar,Java,Swing,Jframe,Jpanel,Jprogressbar,我正在使用JprogressBar开发一个应用程序,当我做一些事情时,进度条正在运行。我还添加了转义功能。当任何人点击键盘上的Escape按钮时,焦点Jpanel/Jdialog/JFrame被释放。它工作正常。我的问题是JprogressBar没有停止。我想这样做,如果任何Jpanel/Jdialog/JFrame通过escape按钮被关闭,那么如果它的父级有JProgressBar,那么它应该被停止。我该怎么做?所有Jpanel/Jdialog/JFrame的父级可以不同吗 我的转义功能如上
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并且正在运行,那么它应该停止。
我的英语不是很好,所以请忽略语法错误
提前谢谢。我不完全确定我是否理解,但您可以使用以下内容
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);
}
公共静态列表findAllChildren(JComponent组件,类clazz){
列表lstChildren=新的ArrayList(5);
对于(组件子级:Component.getComponents()){
if(类实例(子类)){
lstChildren.add((T)children);
}
if(JComponent的子实例){
lstChildren.addAll(findAllChildren((JComponent)child,clazz));
}
}
返回集合。不可修改列表(lstChildren);
}
要查找对JProgressBar的所有引用,从那里您可以做您想做的事…为了获得更好的帮助,请尽快用a编辑您的问题,这表明了您与和的问题,因为在代码中我看不到任何人