JavaFX实现一个简单的模态窗口 博客分类: JavaFX JavaFX
程序员文章站
2024-02-23 14:07:40
...
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();
}
}
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);
}
}
}
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);
}
}
}
上一篇: Python中字典的setdefault()方法教程
下一篇: Java基础