`
ihuashao
  • 浏览: 4553308 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

JavaFX实现一个简单的模态窗口

阅读更多

JavaFX直到目前最新的1.2.1版本里没有提供模态窗口或着对话框,我在项目里通过扩展Control自己实现了一个简单的模态窗口,控件由2个类Window.fx和WindowSkin.fx组成,在WindowSkin里实现了Skin的抽象方法contains,在模态下始终返回true。

下面是控件的源码和一个演示demo。

Window.fx

<!-- {cps..1}-->package org.jqueen.fx.scene.control;

import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.control.Control;

/**
*
@author LeonChen
*/
public class Window extends Control{

public varmodal:Boolean;
public varbackground:Image;
public varcontent:Node[];

override
protected functioncreate():Node{
skin
= WindowSkin{};
super .create();
}
}


WindowSkin.fx

<!-- {cps..2}-->package org.jqueen.fx.scene.control;

import javafx.scene.control.Skin;
import javafx.scene.Group;
import javafx.scene.image.ImageView;

/**
*
@author LeonChen
*/
package class WindowSkin extends Skin{

defwindowControl
= bindcontrolasWindow;
def bounds : Bounds = bind windowControl.boundsInLocal;
def background = ImageView {
image: bind windowControl.background
};

defcontent
= bindwindowControl.content;

init{
node
= Group{
content:bind[background,content]
}
}

override
public functionintersects(localX:Number,localY:Number,localWidth:Number,localHeight:Number):Boolean{
return bounds .intersects(localX,localY,localWidth,localHeight);
}

override
public functioncontains(localX:Number,localY:Number):Boolean{
if (windowControl.modal){
return true ;
}
else {
return bounds .contains(localX,localY);
}
}
}


点击图片可运行程序,非模态时背景可以监听到鼠标事件。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics