0

我使用 SwingWorker 用 Ja​​va Swing API 制作进度条。

我有一个扩展 SwingWorker 的类

    class Swinger extends SwingWorker {
private ClassAnalyzer classAnalyzer;

public Swinger(ClassAnalyzer classAnalyzer){
     this.classAnalyzer = classAnalyzer;
}
        @Override
        public Void doInBackground() throws InterruptedException {

            try
        {     
            int progress = 0;
            while (progress < 100) {

 // at this point I make certain elaboration on classAnalyzer                 

                progress++;

                //Call the process method to update the GUI
                publish(progress);

            }                       
        }
        catch(InterruptedException e)
        {
        }
        return null;
    }

    @Override
    protected void process(List chunks) {
     for (Integer chunk : chunks) {
        progressBar.setValue(chunk);

        //if the switchtype checkbox is selected then
        //change the progressbar to a determined type
        //once the progress has reached 50
        if (chunk > 49)
        {
            if (switchType.isEnabled() && switchType.isSelected())
            {
                progressBar.setStringPainted(true);

            }

        }
     }
 }

}

和第二堂课(我正在写一段)

 public Tester()
{
    JFrame guiFrame = new JFrame();

    //make sure the program exits when the frame closes
    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setTitle("Creating a Table Example");
    guiFrame.setSize(700,200);

    //This will center the JFrame in the middle of the screen
    guiFrame.setLocationRelativeTo(null);


    goButton = new JButton("Go");
    goButton.setActionCommand("Go");
    goButton.addActionListener(new ActionListener()
    {

        //When the button is clicked the SwingWorker class is executed and
        //the button is disabled
        @Override
        public void actionPerformed(ActionEvent event)
        {

            progressBar.setStringPainted(progressType.isSelected());
            ClassAnalyzer c = new ClassAnalyzer();
            Swinger task = new Swinger(c);
            task.execute();

            int methods = c.getNumberOfMethods();

            if(methods == 0){
            JOptionPane.showMessageDialogo(null, "methods not found");
            }

            goButton.setEnabled(false);
        }
    });

    }

当我在第二个类的测试器中启动时,在进度条出现之前显示消息“未找到方法”,而我希望出现消息以防万一。该怎么办?

4

2 回答 2

3

该类SwingWorker还定义了一个done方法,您可以在其中定义任务完成后要做什么。

class Swinger extends SwingWorker {

    // all the rest of your code ^^

    @Override
    protected void done() {
        if(this.classAnalyzer.getNumberOfMethods() == 0)
            JOptionPane.showMessageDialog(null, "methods not found");            
    }
}

您不必担心线程问题,因为此方法也在 EDT 上调用。

于 2013-02-18T01:46:32.160 回答
3

task.execute()将启动一个后台(将在其中doInBackground调用该方法)并且程序将继续执行。

task.execute()不是阻塞方法,这是使用它的原因,所以不要阻塞事件调度线程

您可以SwingWorker使用PropertyChangeListener

final ClassAnalyzer c = new ClassAnalyzer();
Swinger task = new Swinger(c);
task.addPropertyChangeListener(new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("state") && evt.getNewValue().equals(SwingWorker.StateValue.DONE)) {
            int methods = c.getNumberOfMethods();

            if(methods == 0){
                JOptionPane.showMessageDialogo(null, "methods not found");
            }
        }
    }
});
task.execute();
于 2013-02-18T01:49:26.200 回答