JavaFX直到目前最新的1.2.1版本里没有提供模态窗口或着对话框,我在项目里通过扩展Control自己实现了一个简单的模态窗口,控件由2个类Window.fx和WindowSkin.fx组成,在WindowSkin里实现了Skin的
抽象方法contains,在模态下始终返回true。
下面是控件的源码和一个演示demo。
Window.fx
package org.jqueen.fx.scene.control;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.control.Control;
/**
* @author Leon Chen
*/
public class Window extends Control {
public var modal: Boolean;
public var background: Image;
public var content: Node[];
override protected function create(): Node {
skin = WindowSkin {};
super.create();
}
}
WindowSkin.fx
package org.jqueen.fx.scene.control;
import javafx.scene.control.Skin;
import javafx.scene.Group;
import javafx.scene.image.ImageView;
/**
* @author Leon Chen
*/
package class WindowSkin extends Skin {
def windowControl = bind control as Window;
def bounds : Bounds = bind windowControl.boundsInLocal;
def background = ImageView {
image: bind windowControl.background
};
def content = bind windowControl.content;
init {
node = Group {
content: bind[background, content]
}
}
override public function intersects(localX: Number, localY: Number, localWidth: Number, localHeight: Number): Boolean {
return bounds.intersects(localX, localY, localWidth, localHeight);
}
override public function contains(localX: Number, localY: Number): Boolean {
if (windowControl.modal) {
return true;
} else {
return bounds.contains(localX, localY);
}
}
}
点击图片可运行程序,非模态时背景可以监听到鼠标事件。