javafx之FXML初探(一)

2014-11-23 22:15:36 · 作者: · 浏览: 1

FXML作为XML-based,UI构造器。其相关的规则值得我们去理解。

FXML元素分类:

    A class instanceA property of a class instanceA "static" propertyA "define" blockA block of script code FXML应该在根元素定义prefix : xmlns:fx=http://javafx.com/xml

    Class instance 元素

    实例声明
       
    
    Maps
       

    fx:value-对于没有默认构造器的类如String,Double等但是有valueOf方法的
       
    
       
    
       

    fx:factory-对于使用静态工厂方法的
       
        
        
        
        
        
        
    
       

    Builders使用构造器模式的类:如Color
       
       
        
        
         1.0
        
        
        
         0.0
        
        
        
         0.0
        
    
       

    fx:include-包含另一个fxml文件或者国际化资源文件resource bundle
       
       
    
       
    
       
        
         
          
        
    
       
    my_button.fxml
       
    
    包含国际化资源文件
       

    fx:constant

    fx:reference-通过fx:id进行引用
       
        
        
            
         //用于替代ImageView的image属性
        
    
       
       
        
        
        
        
        
        
    
       

    fx:copy-暂时别用,以后也许会改变
    fx:root-指向root元素

    Property元素

    property元素支持强制类型转换。 分为:
      A property setterA read-only list propertyA read-only map property Property Setters
          
      
          
      

      ReadOnly List Property
          
          
            
             ... 
           
      
          

      ReadOnly Map Property
          
      

      Default Property
          
      
          
      
          
          
           
          ...
      
          

      Static Property
          
          
            
             
           
      
          

      定义Blocks----fx:define fx:define定义的内容不会被添加到Scene Graph,最典型的应用就是单选按钮组中ToggleGroup的fx:define 引用它之前需要添加符号$
          
          
            
             
           
          
            
             
             
             
           
      
          



      Attributes:

      分类:
        A property of a class instanceA "static" propertyAn event handler Property Attribute与Property Element是有区别: 1.property attribute只有当元素关闭时才会生效 2.property attribute还支持解析操作(resolution operators):
          Location resolution位置解析Resource resolution国际化资源解析Variable resolution变量解析

          Location resolution

          @代表与当前fxml文件在同一目录
                
              
                 
                  
                 
              
          
                
          注意@路径解析后面接的必须是已经被URL编码的字符。如My Image.jpg应该写成这样

          Resource resolution

          %代表该字符变量应该用国际化资源解析

          Variable resolution

          $代表需要进行变量解析,一般与fx:define配合使用
                
              
                 
          
                
          ...
          
                
          
                

          转义处理:



          表达式绑定:

          ${expr}
                
          

          支持的其他操作:
          "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

          Static Properties与Instance Properties类似
          static properties attribute与element有点区别:
            

          EventHandlers

          适用于setOnEvent类的方法(如setOnAction)

          脚本化处理方式:

          使用java script声明与脚本
                
          ...
          
          
                
              
                  
                   
                 
          
                

          控制器类Controller方法处理方式:

          注意#号,与@FXML注解
                
              
                  
                   
                 
          
                
          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的特殊的处理

          ObservableList, ObservableMap orObservableSet uses a special onChange attribute that points to a handler method with aListChangeListner.Change, MapChangeListener.Change or SetChangeListener.Changeparameter respectively.
                
              
                 
          
                
          public class MyController {
              public void handleChildrenChange(ListChangeListener.Change c) {
                  System.out.println("Children changed!");
              }
          }

          对于parent property的处理:
          public class MyController {
              public void handleParentChange(Observableva lue value, Parent oldValue, Parent newValue) {
                  System.out.println("Parent changed!");
              }
          }
                


          Scripting脚本化

                 
          
          
                 
          
                 
          
          
                 
              
                  
                    function handleButtonAction(event) { java.lang.System.out.println('You clicked me!'); } 
                  
          
              
                   
                    
                  
          
                 

          从外部文件读取脚本。
                 
          
                 
          
          
                 
              
                  
          
              
                   
                    
                  
          
                 
          example.js:
          function handleButtonAction(event) {
             java.lang.System.out.println('