Java 将JButton传递给实用程序类。可以接受吗?

Java 将JButton传递给实用程序类。可以接受吗?,java,swing,static,jframe,jbutton,Java,Swing,Static,Jframe,Jbutton,我有一个JFrame做事情。我在JFrame中隐藏了一个JButton。在SwingWorker中,我有一个实用程序类,比如checkNDownloadFile,我向它传递一个JButton。因此,它可以在流程完成时使其可见/可用 我的问题是,这是否可以接受。我不知道还有什么其他方法可以达到这个效果。(请记住checkNDownloadFile类都是静态的。它只需要/运行一次。) Sudo代码 应答码 不要从doInBackground()的实现中更新GUI 请从您的process()或do

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

我的问题是,这是否可以接受。我不知道还有什么其他方法可以达到这个效果。(请记住checkNDownloadFile类都是静态的。它只需要/运行一次。)


Sudo代码
应答码
  • 不要从
    doInBackground()
    的实现中更新GUI

  • 请从您的
    process()
    done()
    实现中更新GUI,如图所示,以及

  • 您可能需要重新考虑
    checkNDownloadFile()
    方法,以便为合理的进度显示提供所需的粒度

  • 另见


  • 如果我将checkNDownloadFile.start()放在done()中,这不是违背了要点吗?正确;您应该
    publish()
    doInBackground()
    下载中间结果。啊,马上。那么在这个SwingWorker中创建JFrame还可以吗?只要我稍后使用done/publish或其他swingworker进行更新,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
        }
    }