gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:11:38 25 4
gpt4 key购买 nike

我正在尝试以某些预期行为更新 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 更新或与重绘有关。不确定根本原因。

预期:

  1. Want to make the code work like SOP working or in ideal case.
  2. Need to know the reason of not behaving properly.

最佳答案

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

关于java - 更新 JLabel 在 SwingWorker 线程内的行为不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18671290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com