The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
SwingWorker
supports bound properties, which are useful for communicating with other threads. SwingWorker
支持绑定属性,这对于与其他线程通信很有用。Two bound properties are predefined: 预定义了两个绑定属性:progress
and state
. progress
和state
。As with all bound properties, 与所有绑定属性一样,progress
and state
can be used to trigger event-handling tasks on the event dispatch thread.progress
和state
可用于触发事件分派线程上的事件处理任务。
By implementing a property change listener, a program can track changes to 通过实现属性更改侦听器,程序可以跟踪对progress
, state
, and other bound properties. progress
、state
和其他绑定属性的更改。For more information, refer to How to Write a Property Change Listener in Writing Event Listeners.有关详细信息,请参阅编写事件侦听器中的如何编写属性更改侦听器。
progress
Bound Variableprogress
绑定变量The progress
bound variable is an int
value that can range from 0 to 100. progress
绑定变量是一个范围从0到100的int
值。It has a predefined setter method (the protected 它具有预定义的setter方法(受保护的SwingWorker.setProgress
) and a predefined getter method (the public SwingWorker.getProgress
).SwingWorker.setProgress
)和预定义的getter方法(公共SwingWorker.getProgress
)。
The ProgressBarDemo
example uses progress
to update a ProgressBar
control from a background task. ProgressBarDemo
示例使用progress
从后台任务更新ProgressBar
控件。For a detailed discussion of this example, refer to How to Use Progress Bars in Using Swing Components.有关此示例的详细讨论,请参阅使用Swing组件中的如何使用进度条。
state
Bound Variablestate
绑定变量The state
bound variable indicates where the SwingWorker
object is in its life cycle. state
绑定变量指示SwingWorker
对象在其生命周期中的位置。The bound variable contains an enumeration value of type 绑定变量包含SwingWorker.StateValue
. SwingWorker.StateValue
类型的枚举值。Possible values are:可能的值为:
PENDING
doInBackground
is invoked.doInBackground
之前这段时间内的状态。STARTED
doInBackground
is invoked until shortly before done
is invoked.doInBackground
之前不久到调用done
之前不久的状态。DONE
The current value of the state
bound variable is returned by SwingWorker.getState
.SwingWorker.getState
返回state
绑定变量的当前值。
Two methods, part of the Future
interface, also report on the status of the background task. Future
接口的两个方法也报告后台任务的状态。As we saw in Canceling Background Tasks, 正如我们在撤消后台任务中看到的,如果任务被取消,isCancelled
returns true
if the task has been canceled. isCancelled
将返回true
。In addition, 此外,如果任务正常完成或被取消,isDone
returns true
if the task has finished, either normally, or by being cancelled.isDone
将返回true
。