为什么我的JFrame不显示我的标签 [英] Why does my JFrame not show my label

查看:75
本文介绍了为什么我的JFrame不显示我的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序面板正在导出视频时,我可以使用此面板来显示给用户.因此,当单击导出按钮时,我将执行以下操作:

I have this loadpanel to show to the user when my program is exporting the video. So when the export button is clicked, I do this:

loadframe = new LoadFrame("Video Exporteren...");

这将创建对话框并将其显示在屏幕上.但是,大约需要5秒钟才能显示对话框上的实际标签.谁能告诉我为什么. 谢谢

This creates the dialog and shows it on the screen. Yet, it takes about 5 seconds before the actual label on the dialog is shown. Can anyone tell me why. Thanks

public LoadFrame(String operation) {

    mainloadframe = new JDialog();
    JLabel operationlabel = new JLabel(operation);
    loadpanel = new JPanel();

    mainloadframe.setSize(300, 75);
    mainloadframe.setLocationRelativeTo(null);
    mainloadframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    mainloadframe.setAlwaysOnTop(true);
    mainloadframe.setTitle(operation);
    mainloadframe.setResizable(false);

    loadpanel.add(operationlabel);
    mainloadframe.add(loadpanel);
    mainloadframe.setVisible(true);

}

推荐答案

Swing使用单线程模型.这意味着与UI的交互(创建/修改)是在单个线程(称为事件调度线程)中执行的.

Swing uses a single thread model. This means that interactions with the UI (create/modify) are executed within a single thread, known as the Event Dispatching Thread.

RepaintManager使用EDT安排重新绘制请求.如果某些东西阻止了EDT,则不会处理这些绘画请求(和其他事件).

The RepaintManager uses the EDT to schedule repaint requests. If something blocks the EDT, these paint requests (and other events) won't be processed.

Swing也不是线程安全的.这意味着只应在上下文EDT中创建和修改UI.

Swing is also not thread safe. This means you are expected to create and modify the UI only from within the context EDT.

您遇到的问题是,您要花费很长时间来阻止EDT,这意味着EDT在您长时间运行的任务完成之前无法处理任何事件.

The problem you have, is you are blocking the EDT with a time consuming process, meaning that the EDT can't process any events until your long-running task is complete.

有很多解决方案.

使用后台线程执行耗时的操作.这导致了下一个问题,即如何在发生某些更改(例如提供进度更新或过程已完成)时告诉UI.

Use a background thread to perform the time consuming operation. This leads to the next problem of how to you tell the UI when something has changed (ie, provide progress updates or that the process has completed).

您可以使用SwingUtilities.invokeLater与UI重新同步更新.这会将Runnable放在EDT上,确保run方法在EDT的上下文中执行.

You could use SwingUtilities.invokeLater to re-sync updates with UI. This places a Runnable onto the EDT, ensuring that the run method is executed within in the context of the EDT.

使用SwingWorker. SwingWorker具有自动与EDT重新同步的方法,允许您在EDT中进行publish/process更改,以及done方法,该方法在EDT doInBackground之后在EDT的上下文中执行.方法完成.

Use a SwingWorker. A SwingWorker has methods to automatically re-sync with EDT, allowing you to publish/process changes within the EDT as well as a done method which is executed within the context of the EDT after its doInBackground method completes.

这可能是最简单的方法,因为它们都很好地包含在其中.

This is probably the simplest to the methods as it all nicely self contained.

查看 Swing中的并发以了解更多详细信息

Check out Concurrency in Swing for more details

这篇关于为什么我的JFrame不显示我的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆