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发行说明。
Every program has a set of threads where the application logic begins. 每个程序都有一组应用程序逻辑开始的线程。In standard programs, there's only one such thread: the thread that invokes the 在标准程序中,只有一个这样的线程:调用程序类的main
method of the program class. main
方法的线程。In applets the initial threads are the ones that construct the applet object and invoke its 在小程序中,初始线程是构造小程序对象并调用其init
and start
methods; these actions may occur on a single thread, or on two or three different threads, depending on the Java platform implementation. init
和start
方法的线程;这些操作可能发生在单个线程上,也可能发生在两个或三个不同的线程上,具体取决于Java平台实现。In this lesson, we call these threads the initial threads.在本课中,我们将这些线程称为初始线程。
In Swing programs, the initial threads don't have a lot to do. 在Swing程序中,初始线程没有太多工作要做。Their most essential job is to create a 他们最重要的工作是创建一个Runnable
object that initializes the GUI and schedule that object for execution on the event dispatch thread. Runnable
对象,该对象初始化GUI并计划该对象在事件分派线程上执行。Once the GUI is created, the program is primarily driven by GUI events, each of which causes the execution of a short task on the event dispatch thread. 创建GUI后,程序主要由GUI事件驱动,每个事件都会导致在事件分派线程上执行一个短任务。Application code can schedule additional tasks on the event dispatch thread (if they complete quickly, so as not to interfere with event processing) or a worker thread (for long-running tasks).应用程序代码可以在事件分派线程(如果它们很快完成,以免干扰事件处理)或工作线程(对于长时间运行的任务)上调度其他任务。
An initial thread schedules the GUI creation task by invoking 初始线程通过调用javax.swing.SwingUtilities.invokeLater
or javax.swing.SwingUtilities.invokeAndWait
. javax.swing.SwingUtilities.invokeLater
或javax.swing.SwingUtilities.invokeAndWait
来调度GUI创建任务。Both of these methods take a single argument: the 这两个方法都有一个参数:定义新任务的Runnable
that defines the new task. Runnable
。Their only difference is indicated by their names: 它们的唯一区别在于它们的名称:invokeLater
simply schedules the task and returns; invokeAndWait
waits for the task to finish before returning.invokeLater
只是调度任务并返回;invokeAndWait
等待任务完成,然后返回。
You can see examples of this throughout the Swing tutorial:您可以在Swing教程中看到这样的例子:
SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } });
In an applet, the GUI-creation task must be launched from the 在小程序中,必须使用init
method using invokeAndWait
; otherwise, init
may return before the GUI is created, which may cause problems for a web browser launching an applet. invokeAndWait
从init
方法启动GUI创建任务;否则,init
可能会在创建GUI之前返回,这可能会导致web浏览器启动applet时出现问题。In any other kind of program, scheduling the GUI-creation task is usually the last thing the initial thread does, so it doesn't matter whether it uses 在任何其他类型的程序中,调度GUI创建任务通常是初始线程做的最后一件事,因此它使用invokeLater
or invokeAndWait
.invokeLater
还是invokeAndWait
都无关紧要。
Why does not the initial thread simply create the GUI itself? 为什么初始线程不简单地创建GUI本身?Because almost all code that creates or interacts with Swing components must run on the event dispatch thread. 因为几乎所有创建Swing组件或与之交互的代码都必须在事件分派线程上运行。This restriction is discussed further in the next section.下一节将进一步讨论此限制。