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发行说明。
Most Swing components have models. 大多数Swing组件都有模型。A button (例如,按钮(JButton
), for example, has a model (a ButtonModel
object) that stores the button's state what its keyboard mnemonic is, whether it's enabled, selected, or pressed, and so on. JButton
)具有存储按钮状态的模型(ButtonModel
对象);它的键盘助记符是什么,是否启用、选择或按下等等。Some components have multiple models. 某些组件具有多个模型。A list (例如,列表(JList
), for example, uses a ListModel
to hold the list's contents, and a ListSelectionModel
to track the list's current selection.JList
)使用ListModel
保存列表的内容,使用ListSelectionModel
跟踪列表的当前选择。
You often don't need to know about the models that a component uses. 您通常不需要知道组件使用的模型。For example, programs that use buttons usually deal directly with the 例如,使用按钮的程序通常直接处理JButton
object, and don't deal at all with the ButtonModel
object.JButton
对象,而根本不处理ButtonModel
对象。
Why then do models exist? 那么,模型为什么存在呢?The biggest reason is that they give you flexibility in determining how data is stored and retrieved. 最大的原因是它们为您提供了确定数据存储和检索方式的灵活性。For example, if you're designing a spreadsheet application that displays data in a sparsely populated table, you can create your own table model that is optimized for such use.例如,如果您正在设计一个Electron表格应用程序,该应用程序在一个稀疏填充的表中显示数据,则可以创建自己的表模型,该模型针对这种用途进行了优化。
Models have other benefits, too. 模型还有其他好处。They mean that data isn't copied between a program's data structures and those of the Swing components. 这意味着数据不会在程序的数据结构和Swing组件的数据结构之间复制。Also, models automatically propagate changes to all interested listeners, making it easy for the GUI to stay in sync with the data. 此外,模型自动将更改传播给所有感兴趣的监听器,使GUI易于与数据保持同步。For example, to add items to a list you can invoke methods on the list model. 例如,要向列表添加项,可以调用列表模型上的方法。When the model's data changes, the model fires events to the 当模型的数据更改时,模型将向JList
and any other registered listeners, and the GUI is updated accordingly.JList
和任何其他注册的监听器触发事件,并相应地更新GUI。
Although Swing's model architecture is sometimes referred to as a Model-View-Controller (MVC) design, it really isn't. 尽管Swing的模型架构有时被称为模型-视图-控制器(MVC)设计,但事实并非如此。Swing components are generally implemented so that the view and controller are indivisible, implemented by a single UI object provided by the look and feel. Swing组件通常实现为视图和控制器不可分割,由外观提供的单个UI对象实现。The Swing model architecture is more accurately described as a separable model architecture. Swing模型架构更准确地描述为可分离模型架构。If you're interested in learning more about the Swing model architecture, see A Swing Architecture Overview, an article in The Swing Connection.如果您有兴趣了解更多关于Swing模型架构的信息,请参阅Swing连接中的文章Swing架构概述。
This section features an example called Converter, which is an application that continuously converts distance measurements between metric and U.S. units. 本节介绍了一个名为Converter的示例,它是一个连续转换公制和US之间距离测量值的应用程序。单位。You can run Converter ( download JDK 7 or later). 您可以运行转换器(下载JDK 7或更高版本)。Or, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
As the following picture shows, Converter features two sliders, each tied to a text field. 如下图所示,转换器具有两个滑块,每个滑块都绑定到文本字段。The sliders and text fields all display the same data a distance but using two different units of measure.滑块和文本字段都显示相同的数据;距离;但是使用两个不同的测量单位。
The important thing for this program is ensuring that only one model controls the value of the data. 对于这个程序来说,重要的是确保只有一个模型控制数据的值。There are various ways to achieve this; we did it by deferring to the top slider's model. 实现这一点有多种方法;我们通过遵循顶部滑块的模型来实现。The bottom slider's model (an instance of a custom class called 底部滑块的模型(称为FollowerRangeModel
) forwards all data queries to the top slider's model (an instance of a custom class called ConverterRangeModel
). FollowerRangeModel
的自定义类的实例)将所有数据查询转发到顶部滑块的模型中(称为ConverterRangeModel
的自定义类实例)。Each text field is kept in sync with its slider, and vice versa, by event handlers that listen for changes in value. 每个文本字段通过侦听值变化的事件处理程序与其滑块保持同步,反之亦然。Care is taken to ensure that the top slider's model has the final say about what distance is displayed.注意确保顶部滑块的模型对显示的距离具有最终决定权。
When we started implementing the custom slider models, we first looked at the API section of How to Use Sliders. 当我们开始实现自定义滑块模型时,我们首先查看了如何使用滑块的API部分。It informed us that all slider data models must implement the 它通知我们,所有滑块数据模型必须实现BoundedRangeModel
interface. BoundedRangeModel
接口。The BoundedRangeModel
API documentation tells us that the interface has an implementing class named DefaultBoundedRangeModel
. BoundedRangeModel
API文档告诉我们,接口有一个名为DefaultBoundedRangeModel
的实现类。The API documentation for DefaultBoundedRangeModel
shows that it's a general-purpose implementation of BoundedRangeModel
.DefaultBoundedRangeModel
的API文档显示它是BoundedLangeModel
的通用实现。
We didn't use 我们没有直接使用DefaultBoundedRangeModel
directly because it stores data as integers, and Converter uses floating-point data. DefaultBoundedRangeModel
,因为它将数据存储为整数,而转换器使用浮点数据。Thus, we implemented 因此,我们将ConverterRangeModel
as a subclass of Object
. ConverterRageModel
实现为Object
的子类。We then implemented 然后,我们将FollowerRangeModel
as a subclass of ConverterRangeModel
.FollowerRangeModel
实现为ConverterRangeModel
的子类。
To find out about the models for individual components, see the "How to" pages and API documentation for individual components. 要了解单个组件的模型,请参阅“如何”页面和单个组件的API文档。Here are some of our examples that use models directly:下面是一些直接使用模型的示例:
CrayonPanel
class directly uses the color selection model to set the current color.CrayonPanel
面板类直接使用颜色选择模型来设置当前颜色。DefaultTreeModel
), interacts directly with it, and listens for changes to it.DefaultTreeModel
的实例),直接与它交互,并侦听对它的更改。DefaultListModel
) and interacts directly with it.DefaultListModel
的实例)并直接与其交互。SharedDataModel
class that extends DefaultListModel
and implements TableModel
. DefaultListModel
并实现TableModel
的SharedDataModel
类。JList
and JTable
share an instance of SharedDataModel
, providing different views of the model's data.JList
和JTable
共享SharedDataModel
的实例,提供模型数据的不同视图。DefaultListModel
directly.DefaultListModel
。