Introduction to FXML

本文介绍了用于创建Java视图对象的FXML标记语言,它基于XML,适合定义JavaFX应用程序的用户界面。文中说明了在FXML中XML元素的使用场景,详细阐述了类实例的构建方式,如实例声明等,还提及了类的导入、属性设置以及内部使用的Map相关内容。

Overview

概述

FXML is a scriptable, XML-based markup language for constructing Java object graphs. It provides a convenient alternative to constructing such graphs in procedural code, and is ideally suited to defining the user interface of a JavaFX application, since the hierarchical structure of an XML document closely parallels the structure of the JavaFX scene graph.

FXML是一款用于创建Java视图对象,基于XML的脚本语言。由于XML文档的分层结构与JavaFX场景图的结构非常相似,因此它为在程序代码中构建此类图提供了一种方便的替代方法,而且非常适合定义 JavaFX 应用程序的用户界面。

This document introduces the FXML markup language and explains how it can be used to simplify development of JavaFX applications.

本文档介绍 FXML 标记语言,并说明如何使用它来简化 JavaFX 应用程序的开发。

Elements

元素

In FXML, an XML element represents one of the following:

在FXML中,XML元素使用场景如下:

  • A class instance
  • 类实例
  • A property of a class instance
  • 类实例的属性
  • A "static" property
  • static属性
  • A "define" block
  • 定义块
  • A block of script code
  • 脚本块

Class instances, instance properties, static properties, and define blocks are discussed in this section below. Scripting is discussed in a later section.

本节将讨论类实例、实例属性、静态属性和定义块。脚本将在后面的章节中讨论。

Class Instance Elements

类实例元素

Class instances can be constructed in FXML in several ways. The most common is via instance declaration elements, which simply create a new instance of a class by name. Other ways of creating class instances include referencing existing values, copying existing values, and including external FXML files. Each is discussed in more detail below.

在FXML中,类实例可以通过几种方式构建。最常见的是通过实例声明元素,它只需按名称创建一个类的新实例。创建类实例的其他方法包括引用现有值、复制现有值和包含外部FXML文件。下面将详细讨论每种方法。

Instance Declarations

实例声明

If an element's tag is considered an instance declaration if the tag begins with uppercase letter (and the class is imported) or, as in Java, it denotes a fully-qualified (including the package name) name of a class. When the FXML loader (also introduced later) encounters such an element, it creates an instance of that class.

如果一个元素的标签以大写字母开头(且类已被导入),或像 Java 中那样表示一个类的全称(包括包名),则该元素的标签被视为实例声明。当 FXML 加载器(稍后也会介绍)遇到这样的元素时,就会创建该类的实例。

Importing a class is done using the "import" processing instruction (PI). For example, the following PI imports the javafx.scene.control.Label class into the current FXML document’s namespace:

使用 "导入 "处理指令(PI)可以导入类。例如,以下 PI 将 javafx.scene.control.Label 类导入当前 FXML 文档的命名空间:

<?import javafx.scene.control.Label?>

This PI imports all classes from the javafx.scene.control package into the current namespace:

此 PI 将 javafx.scene.control 包中的所有类导入到当前命名空间:

<?import javafx.scene.control.*?>

Any class that adheres to JavaBean constructor and property naming conventions can be readily instantiated and configured using FXML. The following is a simple but complete example that creates an instance of javafx.scene.control.Label and sets its "text" property to "Hello, World!":

任何符合JavaBean构造函数和属性命名约定的类都可以使用FXML方便地实例化和配置。下面是一个简单而完整的示例,它创建了一个javafx.scene.control.Label 实例,并将其"text"属性设置为"Hello, World!":

<?import javafx.scene.control.Label?>
<Label text="Hello, World!"/>

Note that the Label’s "text" property in this example is set using an XML attribute. Properties can also be set using nested property elements. Property elements are discussed in more detail later in this section. Property attributes are discussed in a later section.

注意本例中标签的"文本"属性是通过XML属性设置的。也可以使用嵌套的属性元素来设置属性。本节后面将详细讨论属性元素。属性属性将在后面的章节中讨论。

Classes that don't conform to Bean conventions can also be constructed in FXML, using an object called a "builder". Builders are discussed in more detail later.

不符合 Bean 规则的类也可以用 FXML 构建,使用一种称为 "构建器 "的对象。稍后将详细讨论构建器。

Maps

Internally, the FXML loader uses an instance of com.sun.javafx.fxml.BeanAdapter to wrap an instantiated object and invoke its setter methods. This (currently) private class implements the java.util.Map interface and allows a caller to get and set Bean property values as key/value pairs.

在内部,FXML 加载器使用 com.sun.javafx.fxml.BeanAdapter 的实例来封装实例化对象并调用其设置器方法。这个(目前)私有类实现了 java.util.Map 接口,允许调用者以键/值对的形式获取和设置 Bean 属性值。

If an element represents a type that already implements Map (such as java.util.HashMap), it is not wrapped and its get() and put() methods are invoked directly. For example, the following FXML creates an instance of HashMap and sets its "foo" and "bar" values to "123" and "456", respectively:

如果一个元素代表的类型已经实现了 Map(如 java.util.HashMap),它就不会被封装,而是直接调用其 get() 和 put() 方法。例如,下面的 FXML 创建了一个 HashMap 实例,并将其 "foo "和 "bar "值分别设置为 "123 "和 "456":

未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值