【问题标题】:Pass JButton to Utility class. Acceptable?将 JButton 传递给 Utility 类。可以接受吗?
【发布时间】:2014-04-12 09:55:30
【问题描述】:

我有一个做事的 JFrame。我在该 JFrame 中隐藏了一个 JButton。在 SwingWorker 中,我有一个实用程序类,例如 checkNDownloadFile,我将 JButton 传递给它。因此,它可以在流程完成时使其可见/可用。

我的问题是,这是否可以接受。我不知道有任何其他方法可以做到这一点。 (请记住,checkNDownloadFile 类都是静态的。它只需要/运行一次。)


须藤代码

-----------------------------------------------------------------
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
    }
}

【问题讨论】:

    标签: java swing static jframe jbutton


    【解决方案1】:
    1. 不要doInBackground() 的实现中更新 GUI。

    2. process()done() 的实现更新GUI,如herehere 所示。

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

    4. 另见Watching a Directory for Changes

    【讨论】:

    • 如果我将 checkNDownloadFile.start() 放入 done() 中,这不是重点吗?
    • 正确;关于从doInBackground() 下载的问题,您应该publish() 中间结果。
    • 啊,对了。那么在这个 SwingWorker 中创建 JFrame 仍然可以吗?只要我稍后使用 done/publish 或另一个 swingworker 更新它。
    • checkNDownloadFile 不会有进度。只有四个状态,执行-好-坏
    • 否;在EDT 上创建框架;另请参阅相关的example
    猜你喜欢
    • 2015-04-27
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2020-02-18
    • 2014-06-08
    相关资源
    最近更新 更多