繁体   English   中英

JLabel在入睡前没有出现

[英]JLabel doesn't appear before sleep

我正在开发一个简单的Swing程序,该程序将一个标签放置在框架上,休眠一秒钟,然后将另一个标签放置在框架上,如下所示:

import javax.swing.*;
import java.util.concurrent.*;
public class SubmitLabelManipulationTask {
  public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame("Hello Swing");
    final JLabel label = new JLabel("A Label");
    frame.add(label);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 100);
    frame.setVisible(true);
    TimeUnit.SECONDS.sleep(1);
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        label.setText("Hey! This is Different!");
      }
    }); 
  }
} 

但是,我无法在睡觉前在屏幕上看到第一个标签。 睡眠时屏幕空白。 之后,我会立即看到原始标签,然后立即看到最终标签“嘿!这不一样!”。 在屏幕上。 为什么原始标签没有出现在JFrame上?

使用Swing计时器代替睡眠代码会更好,更安全,因为睡眠调用可能会在事件线程上完成,这会使整个GUI进入睡眠状态,而不是您想要的。 您还需要注意确保GUI实际上确实在Swing事件线程上启动。 例如

import javax.swing.*;
import java.util.concurrent.*;

public class SubmitLabelManipulationTask {
    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Hello Swing");
            final JLabel label = new JLabel("A Label", SwingConstants.CENTER);
            frame.add(label);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 100);
            frame.setVisible(true);
            Timer timer = new Timer(1000, e -> {
                label.setText("Try this instead");
            });
            timer.setRepeats(false);
            timer.start();
        });
    }
}

您编写的代码运行正常,在我的机器上没有任何问题。

import javax.swing.*;
import java.util.concurrent.*;
public class SubmitLabelManipulationTask {
  public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame("Hello Swing");
    final JLabel label = new JLabel("A Label");
    frame.add(label);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 100);
    frame.setVisible(true);
    TimeUnit.SECONDS.sleep(1);
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        label.setText("Hey! This is Different!");
      }
    }); 
  }
} 

马特(Matt)评论说,在GUI加载时睡眠正在发生,这为我解决了这个问题。 事实证明,尽管JFrame立即加载,但加载其他组件大约需要一秒钟。 因此,在正确完成标签时,随后便完成了睡眠,之后几乎立即切换了标签。 将睡眠(或计时器)更改为一秒钟以上,可以让我在切换之前看到那里的原始标签更长的时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM