gpt4 book ai didi

java - 不确定的进度条不会显示

转载 作者:行者123 更新时间:2023-11-29 03:32:33 25 4
gpt4 key购买 nike

我有一个将文件(通过 ADB)复制到 Android 平板电脑的应用程序。这需要一些时间,所以我想显示一个带有不确定进度条的弹出窗口。当复制任务完成后,我希望能够停止进度条并让用户关闭对话框。

目前我还没有添加额外的对话框,只是想让进度条正常工作。我遇到的问题是任务开始时没有显示进度条,但我不知道为什么。进度条会在显示同步完成的对话框出现时显示。代码是:

        progress = new JProgressBar(0, 100);
progress.setForeground(new Color(255, 99, 71));
progress.setIndeterminate(true);
progress.setValue(0);
progress.setPreferredSize( new Dimension( 300, 20 ) );
progress.setBounds( 278, 12, 260, 20 );
progress.setVisible(false);
progress.setString("Sync in progress");
progress.setStringPainted(true);
contentPane.add(progress);
pushtotab = new JButton("");
pushtotab.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent arg0) {
if (buildpathset==1){
try{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
progress.setVisible(true);
wiredsync();
}finally{
JOptionPane.showMessageDialog(null, "sync complete. ",null, buildpathset);
setCursor(Cursor.getDefaultCursor());
progress.setVisible(false);
}}else{
//warning in here later - TO Do
}
}
});

public void wiredsync(){

try {

Process process = Runtime.getRuntime().exec("adb" + " push "+ buildpath + " " + adbtabletsync);
InputStreamReader reader = new InputStreamReader(process.getInputStream());
Scanner scanner = new Scanner(reader);
scanner.close();
int exitCode = process.waitFor();
System.out.println("Process returned: " + exitCode);

} catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//end

感谢您的帮助,

安迪

最佳答案

pooyan 的想法是正确的——在后台线程中执行长时间运行的进程——但给出了错误的库示例,因为您的程序是 Swing 程序而不是 Android 程序。对于 Swing,对此的规范回答是在 SwingWorker 的 doInBackground() 方法中执行长时间运行的任务。

等我找到更好的例子...

像这样:

if (buildpathset == 1) {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
progress.setVisible(true);

// create my SwingWorker object
final SwingWorker<Void, Void> myWorker = new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
// here is my long running task, calling in background
// thread
wiredsync();
return null;
};
};

// this allows me to be notified when the SwingWorker has
// finished
myWorker.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
// if the SwingWorker is done
if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
// notify the user
JOptionPane.showMessageDialog(null, "sync complete. ",
null, buildpathset);
setCursor(Cursor.getDefaultCursor());
progress.setVisible(false);

try {
// one way to catch any errors that occur in
// SwingWorker
myWorker.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}

}
}
});
// run my SwingWorker
myWorker.execute();
} else {
// warning in here later - TO Do
}

有关此的更多信息,请查看:Lesson: Concurrency in Swing

关于java - 不确定的进度条不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17382014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com