我有一个可以做事的 JFrame。我在该 JFrame 中隐藏了一个 JButton。在 SwingWorker 中,我有一个实用程序类,例如 checkNDownloadFile,我将 JButton 传递给它。因此,当进程完成时,它可以使其可见/可用。
我的问题是,这可以接受吗?我不知道还有什么其他方法可以达到这种效果。 (请记住,checkNownloadFile 类都是静态的。它只需要/运行一次。)
<小时/>须藤代码
-----------------------------------------------------------------
myWorker Class
protected Void doInBackground() throws Exception {
//Loading time consuming data.
//Execute Dialog of the question variety.
//Loading more time consuming data.
//Create JFrame
AtomFrame frame = new AtomFrame();
frame.start();
checkNDownloadFile.setButton(frame.fileButton)
checkNDownloadFile.start();
return null;
}
-----------------------------------------------------------------
checkNDownloadFile Class
public static void start() {
//Do the other task at hand
if (complete && good) {
fileButton.setVisible(true);
} else {
//other stuff
}
}
<小时/>
答案代码
-----------------------------------------------------------------
myWorker Class
protected Void doInBackground() throws Exception {
//Loading time consuming data.
//Execute Dialog of the question variety.
//Loading more time consuming data.
//Create JFrame
//Moved to Main Method to be created by EDT.
//AtomFrame frame = new AtomFrame();
//frame.start();
publish("Executing");
boolean returnedB = checkNDownloadFile.start();
if (returnedB) {
publish("Good");
} else {
//Maybe implement
//checkNDownloadFile.getError();
publish("Bad");
}
return null;
}
-----------------------------------------------------------------
checkNDownloadFile Class
public static void start() {
//Do the other task at hand
if (complete && good) {
return true
} else {
//Maybe implement
//setError("");
return false
}
}
最佳答案
请勿通过
doInBackground()
的实现来更新 GUI。您可能需要重构您的
checkNDownloadFile()
方法,以提供合理的进度显示所需的粒度。
关于java - 将 JButton 传递给 Utility 类。可以接受吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028825/