使用XML自定义控件(Custom Control Designed by XML)
来源:互联网 发布:随身带着淘宝去异界 编辑:程序博客网 时间:2024/05/16 11:38
关键是其中的逻辑结构设计:自定义的控件很简单:
<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.*?><!-- fx:root is used primarily when creating custom controls --><fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml"stylesheets="customcontrol/customcontrol.css" styleClass="v-box"><TextField fx:id="textField"/><Button fx:id="button" text="Click Me" onAction="#doSomething"/></fx:root>
其中使用的CSS样式表:
.v-box {-fx-spacing: 5;}.text-field {-fx-highlight-fill: linear-gradient(orange, orangered);}
Package-info:
/** * An implementation of custom control. * * @author HAN */package customcontrol;
模型建立(充当Controller和Root):
package customcontrol;import java.io.IOException;import javafx.beans.property.StringProperty;import javafx.fxml.FXML;import javafx.fxml.FXMLLoader;import javafx.scene.control.Button;import javafx.scene.control.TextField;import javafx.scene.layout.VBox;/** * For custom control creation in XML, it is assured by the associated use of * <code>fxmlLoader.setController(this);</code> and * <code>fxmlLoader.setRoot(this);</code> * * @author HAN * */public class CustomControl extends VBox {@FXMLprivate TextField textField;@FXMLprivate Button button;public CustomControl() {FXMLLoader fxmlLoader = new FXMLLoader();fxmlLoader.setController(this);fxmlLoader.setRoot(this);fxmlLoader.setLocation(CustomControl.class.getResource("View.xml"));try {fxmlLoader.load();} catch (IOException e) {e.printStackTrace();}}public final String getTextFieldText() {return textFieldTextProperty().get();}public final void setTextFieldText(String text) {textFieldTextProperty().set(text);}public StringProperty textFieldTextProperty() {return textField.textProperty();}public final String getButtonText() {return buttonTextProperty().get();}public final void setButtonText(String text) {buttonTextProperty().set(text);}public StringProperty buttonTextProperty() {return button.textProperty();}@FXMLprivate void doSomething() {System.out.println("The button was clicked!");}}
在Java中的使用样例:
1.若此样例不使用自定义的CSS样式表,则默认为开发者定义的风格:
package customcontrol;import javafx.application.Application;import javafx.scene.Scene;import javafx.stage.Stage;public class UseInJava extends Application {public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage stage) throws Exception {CustomControl customControl = new CustomControl();customControl.setTextFieldText("Hello!");customControl.setButtonText("MyButton");customControl.getStyleClass().add("custom-control");Scene scene = new Scene(customControl);//scene.getStylesheets().add(//UseInJava.class.getResource("useinjava.css").toExternalForm());stage.setScene(scene);stage.setTitle("Custom Control");stage.setWidth(300);stage.setHeight(200);stage.show();}}
2. 然而强大之处在于用户可以Override开发者定义的控件内部各自Region的风格:
package customcontrol;import javafx.application.Application;import javafx.scene.Scene;import javafx.stage.Stage;public class UseInJava extends Application {public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage stage) throws Exception {CustomControl customControl = new CustomControl();customControl.setTextFieldText("Hello!");customControl.setButtonText("MyButton");customControl.getStyleClass().add("custom-control");Scene scene = new Scene(customControl);scene.getStylesheets().add(UseInJava.class.getResource("useinjava.css").toExternalForm());stage.setScene(scene);stage.setTitle("Custom Control");stage.setWidth(300);stage.setHeight(200);stage.show();}}
.custom-control .button {-fx-base: #99bcfd;}.custom-control .text-field {-fx-highlight-fill: linear-gradient(greenyellow, limegreen);}
- 使用XML自定义控件(Custom Control Designed by XML)
- 自定义控件(custom control)的使用
- Custom Control控件的使用
- Custom Control 控件的使用
- Office 2010 已经正式不支持Custom Xml(自定义Xml)
- VC MFC(Custom Control)自定义控件
- VC MFC(Custom Control)自定义控件
- User control(用户控件) vs custom control(自定义控件)
- xml布局使用自定义控件
- Android使用xml文件自定义控件
- 使用自定义控件,xml报错
- 自定义控件用XML
- 自定义控件 attrs.xml
- 蚩尤战团宣传单(Designed by Casularm)
- Android自定义控件:如何使用自定义的XML属性
- Android自定义控件:如何使用自定义的XML属性
- android中自定义的控件,使用自定义属性attrs.xml
- Custom Control控件一个例子
- C++ 异常 与 ”为什么析构函数不能抛出异常“ 问题
- word2007 正文自动变为大纲一级 问题
- Web性能压力测试工具之ApacheBench(ab)详解
- mysql group by 处理方式的异同
- 如何在Windows的命令行cmd中运行vbs和js脚本文件
- 使用XML自定义控件(Custom Control Designed by XML)
- x86 cpu 寄存器 初始值
- 尝试写个爬虫(4)
- mysql5.5主从配置
- 结构体前向申明的小细节
- 同步控制 EvenGenerator
- android车机
- 函数指针与指针函数
- 用两个栈实现一个队列