在SwingWorker线程内更新JLabel的行为不正常

时间:2013-09-07 08:15:03

标签: java swing jlabel repaint swingworker

我正在尝试更新JLabel的某些预期行为,例如 -

COND 1:JLabel应该显示5秒钟,然后它应该在接下来的3秒内不可见,然后它应该可见5秒钟,依此类推。

COND 2:JLabel应该显示0.5秒,然后它对于接下来的3个secons应该是不可见的。

任何排列组合的种类,行为应该有效。 以下是示例代码 -

import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

public class Main
{
    static CounterTask task;

    public static void main ( String[] args )
    {

        final JLabel label = new JLabel( "TEST ME " );
        JButton startButton = new JButton( "Start" );
        startButton.addActionListener( new ActionListener()
        {
            public void actionPerformed ( ActionEvent e )
            {
                task = new CounterTask( label );
                task.execute();
            }
        } );

        JButton cancelButton = new JButton( "Cancel" );
        cancelButton.addActionListener( new ActionListener()
        {
            public void actionPerformed ( ActionEvent e )
            {
                task.cancel( true );
            }
        } );

        JPanel buttonPanel = new JPanel();
        buttonPanel.add( startButton );
        buttonPanel.add( cancelButton );

        JPanel cp = new JPanel();
        LayoutManager layout = new BoxLayout( cp, BoxLayout.Y_AXIS );
        cp.setLayout( layout );
        cp.add( buttonPanel );
        cp.add( label );

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setContentPane( cp );
        frame.pack();
        frame.setVisible( true );
    }
}

class CounterTask
    extends SwingWorker< Integer, Integer >
{
    int DELAY = 3000;

    JLabel label;

    int WAIT = 5000;

    public CounterTask ( JLabel label )
    {
        this.label = label;
    }

    @Override
    protected Integer doInBackground () throws Exception
    {
        int i = 0;
        int count = 1000;
        while ( !isCancelled() && i < count )
        {
            i++;
            publish( new Integer[] { i } );
            label.setVisible( false );
            Thread.sleep( DELAY );
        }
        return count;
    }

    protected void process ( List< Integer > chunks )
    {
        Integer strContent = chunks.get( chunks.size() - 1 );
        label.setVisible( true );
        label.validate();
        label.setText( "DISPLAYING " + strContent );
        try
        {
            Thread.sleep( WAIT );
        }
        catch ( InterruptedException e )
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void done ()
    {
        if ( isCancelled() )
            System.out.println( "Cancelled !" );
        else
            System.out.println( "Done !" );
    }
}

我肯定做错了,因为这种组合适用于某些数据,并且大多数JLabel在整个过程中变得不可见,但是如果我放置SOP,我会在适当的延迟和等待时间获得正确的数据。

上面的Swing worker代码有两个变量WAIT,DELAY。 WAIT使JLabel可见X秒,DELAY使JLAbel在Y秒内不可见。但如果尝试不同的烫发和梳理,你会发现JLabel的可见性不能按预期工作。这可能是因为标签的EDT更新或与重绘有关的事情。不确定根本原因。

预期:

  
      
  1. 希望使代码像SOP一样工作或理想情况。
  2.   
  3. 需要知道不正常行为的原因。
  4.   

2 个答案:

答案 0 :(得分:4)

SwingWorker对于一个闪烁的标签来说太过分了。尝试以下基于计时器的方法,它根本不需要线程魔法:

public class Main {
private static Timer setVisibleTimer;
private static Timer setInvisibleTimer;

public static void main(String[] args) {

    final JLabel label = new JLabel("TEST ME ");
    JButton startButton = new JButton("Start");
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setVisibleTimer = new Timer(8000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    label.setVisible(true);
                }
            });
            setInvisibleTimer = new Timer(5000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    label.setVisible(false);
                }
            });
            setVisibleTimer.start();
            setInvisibleTimer.start();
        }
    });

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            label.setVisible(true);
            if (setVisibleTimer != null){
                setInvisibleTimer.stop();
                setVisibleTimer.stop();
            }
        }
    });

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(startButton);
    buttonPanel.add(cancelButton);

    JPanel cp = new JPanel();
    LayoutManager layout = new BoxLayout(cp, BoxLayout.Y_AXIS);
    cp.setLayout(layout);
    cp.add(buttonPanel);
    cp.add(label);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}
}

答案 1 :(得分:4)

  不,你永远不会在EDT上等待:而是把所有的逻辑放进去   doInBackground,并发布标签状态

用一些代码充实我的评论(可以在Timer中完成,但实际上我喜欢工人:)

static class LabelState {
    int count;
    boolean visible;

    public LabelState(boolean visible, int count) {
        this.count = count;
        this.visible = visible;
    }
}
static class CounterTask extends SwingWorker<Void, LabelState> {
    int DELAY = 3000;

    JLabel label;

    int WAIT = 5000;

    public CounterTask(JLabel label) {
        this.label = label;
    }

    @Override
    protected Void doInBackground() throws Exception {
        int i = 0;
        int count = 1000;
        while (!isCancelled() && i < count) {
            i++;
            publish(new LabelState(false, i));
            Thread.sleep(DELAY);
            i++;
            publish(new LabelState(true, i));
            Thread.sleep(WAIT);
        }
        return null;
    }

    protected void process(List<LabelState> chunks) {
        LabelState strContent = chunks.get(chunks.size() - 1);
        label.setVisible(strContent.visible);
        label.validate();
        label.setText("DISPLAYING " + strContent.count);
    }

    @Override
    protected void done() {
        if (isCancelled())
            System.out.println("Cancelled !");
        else
            System.out.println("Done !");
    }
}