java - 将 JButton 传递给 Utility 类。可以接受吗?

标签 java swing static jframe jbutton

我有一个可以做事的 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
    }
}

最佳答案

  1. 请勿通过 doInBackground() 的实现来更新 GUI。

  2. 务必process()done() 的实现中更新 GUI,如 here 所示,和here .

  3. 您可能需要重构您的 checkNDownloadFile() 方法,以提供合理的进度显示所需的粒度。

  4. 另请参阅 Watching a Directory for Changes .

关于java - 将 JButton 传递给 Utility 类。可以接受吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028825/

相关文章:

java - 让 Java EE InitialContext 找到合格的主机名? (针对 JBoss EJB 服务器)

java - java中将json对象转换为tsv格式

java - 已处理的 JFrame 仍从 Window.getWindows() 返回

java - 为什么内部类允许 "static final"字段?

java - 从另一个类测试泛型类的 main 方法

java - 如何刷新 Activity ? map View 刷新失败

java - 如何将 JPanel 用作 JButton?

类似 Java Visual Studio 的菜单栏

java - JScrollPane 没有滚动超过设定的大小

delphi - Delphi中的静态属性