如何在 Java (Netbeans GUI) 中使用进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12008565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use the Progress Bar in Java (Netbeans GUI)
提问by Matthew Mills
I am not too experienced with Java and am having trouble getting a progress bar to work. I am using Netbeans' built-in GUI maker. Currently my program reads in images from a directory into an image array. Obviously this takes a bit of time, thus I would like a simple progress bar to update the user to know how far the loading is. I have used one of those drag-and-drop progress bar components.
我对 Java 不太熟悉,并且无法让进度条正常工作。我正在使用 Netbeans 的内置 GUI 制作器。目前,我的程序将目录中的图像读入图像数组。显然这需要一些时间,因此我想要一个简单的进度条来更新用户以了解加载的程度。我使用了其中一种拖放式进度条组件。
-I currently can get the number of files in a directory and can calculate the percentage of files loaded -The files successfully load into the array, but the progress bar is only updated after all files are loaded. I have tried revalidating and repainting while I loop but this does not work -I have tried the various tutorials on docs.oracle but can't seem to get anything working. -I believe SwingWorker is the class I should be using, is this correct?
-我目前可以获取目录中的文件数量,并可以计算加载文件的百分比 -文件成功加载到数组中,但只有在加载所有文件后才会更新进度条。我在循环时尝试重新验证和重新绘制,但这不起作用 - 我已经尝试了 docs.oracle 上的各种教程,但似乎无法获得任何工作。- 我相信 SwingWorker 是我应该使用的类,这是正确的吗?
Any help with an example or some advice would be greatly appreciated! Thanks!
任何有关示例或建议的帮助将不胜感激!谢谢!
采纳答案by MadProgrammer
Yes, you have run into the trap of all new Swing programers - the Event Dispatching Thread
是的,您遇到了所有新 Swing 程序员的陷阱——事件调度线程
Basically speaking, this Thread
is the heart of any Swing system. Is is responsible for dispatching events to all the UI components, as well as making requests to the repaint manager to update the UI.
基本上来说,这Thread
是任何 Swing 系统的核心。Is 负责将事件分派到所有 UI 组件,并向重绘管理器发出请求以更新 UI。
If you do any time consuming tasks while in the EDT
, you will prevent the UI from been updated.
如果您在 中执行任何耗时的任务EDT
,您将阻止 UI 更新。
I'd suggest you start by having a read through Worker Threads and SwingWorkerand Concurrency in Swingand, because I know you're going to ask, have a read of this answer, it has a number of examples of using SwingWorker
, JTextArea appending problems
我建议你通过具有通过读开始工作线程和SwingWorker类和并发在Swing和,因为我知道你要问,这个答案的读,它有许多的使用实例SwingWorker
,JTextArea的附加问题