javafx之FXML初探
2020-11-25 00:24
标签:javafx FXML作为XML-based,UI构造器。其相关的规则值得我们去理解。 FXML元素分类:
javafx之FXML初探 标签:javafx 原文地址:http://blog.csdn.net/xby1993/article/details/24813643
Class instance 元素
my_button.fxml
Property元素
Property Setters
Attributes:
Location resolution
注意@路径解析后面接的必须是已经被URL编码的字符。如My Image.jpg应该写成这样
Resource resolution
Variable resolution
转义处理:
表达式绑定:
"string"
‘string‘
A string constant
true
false
A boolean constant
null
A constant representing the null value
50.0
3e5
42
A numerical constant
-
(unary operator)
Unary minus operator, applied on a number
!
(unary operator)
Unary negation of a boolean
+ -
* / %
Numerical binary operators
&& ||
Boolean binary operators
> >=
== !=
Binary operators of comparison.
Both arguments must be of type Comparable
EventHandlers
脚本化处理方式:
...
控制器类Controller方法处理方式:
public class MyController {
@FXML public void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
}
}
下面这种方式也是有效的public class MyController {
public void handleButtonAction() {
System.out.println("You clicked me!");
}
}
对于Collections与properties的特殊的处理
public class MyController {
public void handleChildrenChange(ListChangeListener.Change c) {
System.out.println("Children changed!");
}
}
public class MyController {
public void handleParentChange(ObservableValue value, Parent oldValue, Parent newValue) {
System.out.println("Parent changed!");
}
}
Scripting脚本化
example.js:function handleButtonAction(event) {
java.lang.System.out.println(‘You clicked me!‘);
}
...
Controllers
public class MyController {
public void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
}
}
当然Controllers类还可以实现Initializable接口,以便在被加载时可以调用初始化方法initialize()package com.foo;
public class MyController implements Initializable {
public Button button;
@Override
public void initialize(URL location, Resources resources)
button.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent event) {
System.out.println("You clicked me!");
}
前面介绍的都是通过public修饰field与方法,但是这破坏了封装性原则。public class MyController {
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
}
}
public class MyController implements Initializable {
@FXML private Button button;
@FXML
protected void initialize()
button.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent event) {
System.out.println("You clicked me!");
}
FXMLLoader
URL location = getClass().getResource("example.fxml");
ResourceBundle resources = ResourceBundle.getBundle("com.foo.example");
FXMLLoader fxmlLoader = new FXMLLoader(location, resources);
Pane root = (Pane)fxmlLoader.load();
MyController controller = (MyController)fxmlLoader.getController();
使用FXML结合FXMLLoader自定义UI组件
自定义的UI组件:package fxml;
import java.io.IOException;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
public class CustomControl extends VBox {
@FXML private TextField textField;
public CustomControl() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("custom_control.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
public String getText() {
return textProperty().get();
}
public void setText(String value) {
textProperty().set(value);
}
public StringProperty textProperty() {
return textField.textProperty();
}
@FXML
protected void doSomething() {
System.out.println("The button was clicked!");
}
}
HBox hbox = new HBox();
CustomControl customControl = new CustomControl();
customControl.setText("Hello World!");
hbox.getChildren().add(customControl);