3

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

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

条件 2:JLabel 应该显示 0.5 秒,然后在接下来的 3 秒内它应该是不可见的..

任何排列组合的种类,行为都应该起作用。以下是示例代码-

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 更新 Label 或与重绘有关。不确定根本原因。

预期的 :

  1. 想让代码像 SOP 一样工作或在理想情况下工作。
  2. 需要知道行为不当的原因。
4

2 回答 2

4

不,您永远不会在 EDT 上等待:而是将所有逻辑放入 doInBackground,并发布标签状态

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

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 !");
    }
}
于 2013-09-07T09:10:47.083 回答
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);
}
}
于 2013-09-07T09:01:59.743 回答