Servis-Rhino 4211B Guia do Utilizador Página 256

  • Descarregar
  • Adicionar aos meus manuais
  • Imprimir
  • Página
    / 298
  • Índice
  • MARCADORES
  • Avaliado. / 5. Com base em avaliações de clientes
Vista de página 255
FXML – The JavaFX Markup Language
We can create JavaFX applications using standard Java programming techniques such as creating
instances of JavaFX objects and linking them together in code. However, an alternative technique
is available to us which we will shortly see has a wealth of power. Since a Scene Graph is a
hierarchical description of nodes what we can do is express this relationship within an XML
document. Such a document used to describe a JavaFX application is called an "FXML" file.
FXML files allow one to describe the relationship between UI nodes in a declarative as opposed to
programmatic fashion. Instead of coding node after node within a Java class, we can describe the
nodes in the FXML file and have JavaFX interpret that XML file for us at runtime. Parsing the file,
JavaFX will construct the corresponding Scene Graph as though we had coded it in Java.
Another important feature of FXML is that a freely available application called "Scene Builder" can
be downloaded off the web. This application allows one to visually compose what the UI will look
like. The input and output of Scene Builder is an FXML file. This means that the construction of
JavaFX UIs just became tremendously easier.
When we write a normal Java application that codes the construction of the nodes by hand, we are
free to intermix arbitrary controller logic. For example, if we define a button then at the same place
we can define an action to be performed when the button is pressed. When we are describing a
scene graph in FXML we don't have that option. This is where we can utilize a class that we will
consider to be our "Controller Class".
A controller class is a standard Java Bean class with fields corresponding to selected nodes within
the graph. Methods in the controller class can also be defined as invokable when certain events
happen in the UI.
Withing Java, we can parse the FXML document to create the tree of nodes using the static method
"FXMLLoader.load()". For example, to load an FXML file located beside the current class we
could code:
Parent root = FXMLLoader.load(getClass().getResource("MyFXML.fxml"));
See also:
Mastering FXML – Java SE 8
Introduction to FXML - 2013-09-10
FXML Architecture
Image a simple piece of FXML
<MyClass>
</MyClass>
The way to understand this is that the FXML parser will instantiate a new instance of the Java class
called "MyClass".
Now if we pair this with JavaFX, the FXML:
<Label />
will create a new instance of a Label object.
A JavaFX Label object has properties such as "text" to define the text of the label. In Java we
might code:
Label myLabel = new Label();
myLabel.setText("Hello world");
In FXML we might declare:
<Label text="Hello World" />
Page 256
Vista de página 255
1 2 ... 251 252 253 254 255 256 257 258 259 260 261 ... 297 298

Comentários a estes Manuais

Sem comentários