JavaFX学习笔记

CSS参考:

http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html

API参考

http://docs.oracle.com/javafx/2/api/index.html

 

2013-7-26

SplitPane中的节点,如果不是一个布局对象,则setDividerPositions函数的值不会起作用,只会以节点的最小值作为区域的大小 

 

 

Button上的文字与边界的默认设置了padding,如果不想留空,可以使用btn.setStyle("-fx-padding:0;");清除

 

TextField内建支持在文本框内设置提示文本setPromptText("XXX")

 

创建无边框窗口

stage.initStyle(StageStyle.UNDECORATED);

 

  • 创建播放器

MediaPlayer mplayer = new MediaPlayer(new Media("file://"+Constant.PATH_DATA+"DoubleFace.mp3"));
mplayer.setCycleCount(1);
mplayer.play();
mplayer.stop();

播放播放的媒体文件只能在构造函数中设置,而且必须为合法的http\file\jar等前缀的合法URI
URI的规范中,资源描述文字,只允许使用字母,数字,安全字符,特殊字符,和转义字符
转义字符在URL中规定是使用%和两个hex进行表示,路径中的空格要转换为%20
整个路径可以使用URLENCODE

 

在windows下运行以下的代码

MediaPlayer mpler = new MediaPlayer(new Media("file://E:/java/jbProj/TomatoClock/data/DoubleFace.mp3"));
mpler.setCycleCount(1);
mpler.play();

输出错误信息

Exception in thread "main" MediaException: MEDIA_INACCESSIBLE : E
    at javafx.scene.media.Media.<init>(Media.java:380)
    at net.yondy.tomatoclock.Test.main(Test.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

原因RFC 1738 – Uniform Resource Locators (URL):

 

A file URL takes the form:

file://<host>/<path>
[…]

As a special case, <host> can be the string "localhost" or the empty string; this is interpreted as 'the machine from which the URL is being interpreted'.

 

也就是路径应该是以file:///路径

new Media("file://E:/java/jbProj/TomatoClock/data/DoubleFace.mp3")

 

  • 窗口的拖拽处理

OnMousePressed中根据事件源的屏幕座标和Stage的屏幕座标,计算出鼠标在窗口中的座标,

OnMouseDragged中根据鼠标的窗口座标及事件的屏幕座标,计算出Stage在拖拽以后的位置

                //when mouse button is pressed, save the initial position of screen

                rootGroup.setOnMousePressed(new EventHandler<MouseEvent>() {

                    public void handle(MouseEvent me) {

                        initX = me.getScreenX() - stage.getX();

                        initY = me.getScreenY() - stage.getY();

                    }

                });

 

                //when screen is dragged, translate it accordingly

                rootGroup.setOnMouseDragged(new EventHandler<MouseEvent>() {

                    public void handle(MouseEvent me) {

                        stage.setX(me.getScreenX() - initX);

                        stage.setY(me.getScreenY() - initY);

                    }

                });

 

  • 分页

分页器Pagination,包含一个Node用于显示主体的内容,同时可以设置显示的页码数目。

 

GridPane

addColumn(int columnIndex, Node... children)  可以一次过将所有同列上的节点全部添加到布局上,与此相同的还有addRow

修改节点的对齐方式,需要使用GridPane.setHalignment(),可以使用类似下面的代码进行进行批量修改

        //设置对齐方式
       for(Node node : mainPane.getChildren()){
                if(0==GridPane.getColumnIndex(node)){
                    GridPane.setHalignment(node, HPos.RIGHT);
                }
       }

 

 Label

修改文字颜色setStyle("-fx-text-fill:#fff;");

 

ComboBox

选中元素

ComboBox cbCycles = new ComboBox(lstCycleType);
cbCycles.getSelectionModel().select(0);

posted @ 2012-12-31 02:07  yondy  阅读(800)  评论(0编辑  收藏  举报