标签:javafx
FXML作为XML-based,UI构造器。其相关的规则值得我们去理解。
FXML元素分类:
<?import javafx.scene.control.Label?> <Label text="Hello, World!"/>Maps
<HashMap foo="123" bar="456"/>
<String fx:value="Hello, World!"/> <Double fx:value="1.0"/> <Boolean fx:value="false"/>
<FXCollections fx:factory="observableArrayList"> <String fx:value="A"/> <String fx:value="B"/> <String fx:value="C"/> </FXCollections>
<Color red="1.0" green="0.0" blue="0.0"/>
<Color> <red>1.0</red> <green>0.0</green> <blue>0.0</blue> </Color>
<fx:include source="filename"/>
<?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <VBox xmlns:fx="http://javafx.com/fxml"> <children> <fx:include source="my_button.fxml"/> </children> </VBox>my_button.fxml
<?import javafx.scene.control.*?> <Button text="My Button"/>包含国际化资源文件
<fx:include source="filename" resources="resource_file" charset="utf-8"/>
<Button> <minHeight><Double fx:constant="NEGATIVE_INFINITY"/></minHeight> </Button>
<ImageView> <image> <fx:reference source="myImage"/> //用于替代ImageView的image属性 </image> </ImageView>
<ArrayList> <fx:reference source="element1"/> <fx:reference source="element2"/> <fx:reference source="element3"/> </ArrayList>
<?import javafx.scene.control.Label?> <Label> <text>Hello, World!</text> </Label>
<?import javafx.scene.control.Label?> <Label text="Hello, World!"/>
<Group xmlns:fx="http://javafx.com/fxml"> <children> <Rectangle fx:id="rectangle" x="10" y="10" width="320" height="240" fill="#ff0000"/> ... </children> </Group>
<?import javafx.scene.control.*?> <Button> <properties foo="123" bar="456"/> </Button>
<?import javafx.scene.*?> <?import javafx.scene.shape.*?> <VBox xmlns:fx="http://javafx.com/fxml"> <Button text="Click Me!"/> ... </VBox>
<GridPane> <children> <Label text="My Label"> <GridPane.rowIndex>0</GridPane.rowIndex> <GridPane.columnIndex>0</GridPane.columnIndex> </Label> </children> </TabPane>
<VBox> <fx:define> <ToggleGroup fx:id="myToggleGroup"/> </fx:define> <children> <RadioButton text="A" toggleGroup="$myToggleGroup"/> <RadioButton text="B" toggleGroup="$myToggleGroup"/> <RadioButton text="C" toggleGroup="$myToggleGroup"/> </children> </VBox>
<ImageView> <image> <Image url="@my_image.png"/> </image> </ImageView>注意@路径解析后面接的必须是已经被URL编码的字符。如My Image.jpg应该写成这样
<Image url="@My%20Image.png"/>
<Label text="%myText"/>
<fx:define> <ToggleGroup fx:id="myToggleGroup"/> </fx:define> ... <RadioButton text="A" toggleGroup="$myToggleGroup"/> <RadioButton text="B" toggleGroup="$myToggleGroup"/>
<Label text="\$10.00"/>
<TextField fx:id="textField"/> <Label text="${textField.text}"/>
"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 |
<Label text="My Label" GridPane.rowIndex="0" GridPane.columnIndex="0"/>
<?language javascript?> ... <VBox> <children> <Button text="Click Me!" onAction="java.lang.System.out.println(‘You clicked me!‘);"/> </children> </VBox>
<VBox fx:controller="com.foo.MyController" xmlns:fx="http://javafx.com/fxml"> <children> <Button text="Click Me!" onAction="#handleButtonAction"/> </children> </VBox>
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!"); } }
<VBox fx:controller="com.foo.MyController" xmlns:fx="http://javafx.com/fxml"> <children onChange="#handleChildrenChange"/> </VBox>
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!"); } }
<VBox fx:controller="com.foo.MyController" xmlns:fx="http://javafx.com/fxml" onParentChange="#handleParentChange"/>
<?language javascript?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <VBox xmlns:fx="http://javafx.com/fxml"> <fx:script> function handleButtonAction(event) { java.lang.System.out.println(‘You clicked me!‘); } </fx:script> <children> <Button text="Click Me!" onAction="handleButtonAction(event);"/> </children> </VBox>
<?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <VBox xmlns:fx="http://javafx.com/fxml"> <fx:script source="example.js" charset="cp1252"/> <children> <Button text="Click Me!" onAction="handleButtonAction(event);"/> </children> </VBox>example.js:
function handleButtonAction(event) { java.lang.System.out.println(‘You clicked me!‘); }
<fx:script> var myText = "This is the text of my label."; </fx:script> ... <Label text="$myText"/>
<VBox fx:controller="com.foo.MyController" xmlns:fx="http://javafx.com/fxml"> <children> <Button text="Click Me!" onAction="#handleButtonAction"/> </children> </VBox>
public class MyController { public void handleButtonAction(ActionEvent event) { System.out.println("You clicked me!"); } }当然Controllers类还可以实现Initializable接口,以便在被加载时可以调用初始化方法initialize()
<VBox fx:controller="com.foo.MyController" xmlns:fx="http://javafx.com/fxml"> <children> <Button fx:id="button" text="Click Me!"/> </children> </VBox>
package com.foo; public class MyController implements Initializable { public Button button; @Override public void initialize(URL location, Resources resources) button.setOnAction(new EventHandler<ActionEvent>() { @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<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("You clicked me!"); }
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();
<?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml"> <TextField fx:id="textField"/> <Button text="Click Me" onAction="#doSomething"/> </fx:root>自定义的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);
<HBox> <CustomControl text="Hello World!"/> </HBox>
标签:javafx
原文地址:http://blog.csdn.net/xby1993/article/details/24813643