JavaFX学习之道:JavaFX API具体解释之Window,Stage,PopupWindow

stage包中包括
Window, Stage, PopupWindow, Popup, FileChooser, DirectoryChooser, Screen等类。



当中Window类可理解成一个窗口,用于存放Scene。并与用户操作。一般window作为窗口。都用其子类Stage和PopupWindow。
看一下Window作为窗口的顶级类包括的一些共同属性
eventDispatcher setEventDispatcher(EventDispatcher value)
focused: 焦点集中 setFocused(boolean)设置
x y width height: 位置及大小 setX(double)。setY(double),setWidth(double),setHeight(double) 设置
onCloseRequest 关闭Window事件 setOnCloseRequest(EventHandler)
onHidden 隐藏事件 setOnHidden(EventHandler)
onHiding 隐藏事件。优先于OnHidden setOnHiding(EventHandler)
onShowing 显示事件 setOnShowing(EventHandler)
onShown 显示事件,在OnShowing之后 setOnShown(EventHandler)
opacity 透明 setOpacity(double) 值为0到1
scene 设置场景 setScene(Scene)
showing 是否是显示状态 isShowing()

Window的一些方法:
addEventFilter(EventType eventType, EventHandler eventFilter) 加入事件过滤
removeEventFilter(EventType eventType, EventHandler eventFilter)
addEventHandler(EventType eventType, EventHandler eventHandler) 加入事件
removeEventHandler(EventType eventType, EventHandler eventHandler)
buildEventDispatchChain(EventDispatchChain tail)
centerOnScreen() 使Window显示中间
hide() 隐藏 实际就是关闭
show() 保护类型 显示window
requestFocus() 要求焦点集中
sizeToScene() 使Window的大小匹配它Scene的大小。假设你没有设置Window大小的话,那么就是刚好匹配的(匹配不等于相等了,window始终要大Scene一些)。若果你设置了window大小。在调用该方法,则会改变window的大小以匹配Scene大小。

最好的样例是,你设置window大小。而不设置Scene大小,再调用该方法。


fireEvent(Event event) 调用指定的事件

Stage作为顶级容器,其主Stage由系统创建。

额外的可由应用程序创建。
fullScreen 是否全屏。该属性类型为ReadOnly,不可绑定。 setFullScreen(boolean) 尽管是全屏 可是width,height等属性值仍不变。若在全屏的时候改变这些值。返回原来状态的时候,使用改变后的值。


iconified 是否最小化 setIconified(boolean)
resizable 设置大小是否变化 setResizable(boolean) 为true则不能拉动stage的大小
title 设置标题
maxHeight maxWidth minHeight minWidth 设置最大最小高宽

stage的一些方法:
initModality(Modality modality) 设置stage的形态。依据功能来讲就是窗体之间的堵塞模式,该方法不能被primaryStage调用,仅仅能被自己创建的stage调用。
Modality.NONE – a stage that does not block any other window.
不限制,父窗体与子窗体之间没堵塞
Modality.WINDOW_MODAL – a stage that blocks input events from being delivered to all windows from its owner (parent) to its root. Its root is the closest ancestor window without an owner.
堵塞父窗体。也就是子窗体出现的时候,父窗体点击不了。
Modality.APPLICATION_MODAL – a stage that blocks input events from being delivered to all windows from the same application, except for those from its child hierarchy.
这样的为堵塞其他窗体,除了它的子窗体。

initStyle(StageStyle style) 设置样式 设置样式也能够通过stage的构造函数设置
StageStyle.DECORATED – a stage with a solid white background and platform decorations.
一般的stage
StageStyle.UNDECORATED – a stage with a solid white background and no decorations.
白色背景,没有修饰
StageStyle.TRANSPARENT – a stage with a transparent background and no decorations.
透明,没有修饰
StageStyle.UTILITY – a stage with a solid white background and minimal platform decorations.
白色背景,仅一个关闭button

close() 关闭窗体 也就是调用父类的Hide()方法
initOwner(Window owner)指定所属的window。就相当指定父窗体。
showAndWait()调用之后会堵塞在这里。直到被hidden或closed(被调用隐藏或关闭它),才会唤醒。继续运行以下的代码。 举个样例,假如我在代码里面创建了2个stage。但是我想第一个stage关闭的时候才创建第二个。此时你就能够在第一个stage创建完后调用showAndWait()方法。当第一个被隐藏或关闭时,才会继续运行第二个stage的创建方法。
toBack() 实如今后端
toFront() 显示在前端

PopupWindow作为顶级容器。但本身是抽象类。

其主要子类由Popup, 和 PopupControl。 而PopupControl并非直接使用的空间,而是其子类ContextMenu。Tooltip。

PopupWindow可理解成弹出层,子类Popup用于自己定义弹出层,ContextMenu。Tooltip为系统实现的弹出层。
PopupWindw和stage不同,它没有窗口的修饰。没有标题栏,而且不会作为顶级容器直接在操作系统上显示(一般都在stage上显示)。一般用于提醒,可移动的Box等。


看一下PopupWindow作为弹出框的顶级类所包括的一些共同属性:
autoFix
autoHide 自己主动隐藏,setAutoHide(boolean) 默认值为true。在焦点失去的时候会隐藏(但是什么显示却没有)。
consumeAutoHidingEvents 是否在隐藏的时候消费掉事件。 setConsumeAutoHidingEvents(boolean) 也就是说调用完隐藏事件后,就消耗掉事件。不再往下传递,避免Popup的UI控件触发事件。举个样例假如你失去焦点时,PopupWindow的autoHide事件触发。同一时候PopupWindow的一个UI控件也触发了这个事件,此时设置consumeAutoHidingEvents为true,由于会首先调用autoHide事件。而且把这个响应事件消耗了,所以那个UI控件的事件就不会被处理了。
hideOnEscape 这个就是你按esc的时候PopupWindow是否隐藏,setHideOnEscape(boolean)
onAutoHide 自己主动隐藏处理事件, setOnAutoHide(EventHandler) 。

在它本身的隐藏事件后调用。


ownerNode,ownerWindow 所属的节点和窗体,仅仅读对象。

PopupWindow显示必须指定其拥有者,所以就有该属性。
能够看它的show方法,owner能够是window,也能够是Node
show(Window owner)
show(Window ownerWindow, double screenX, double screenY)
show(Node ownerNode, double screenX, double screenY)
显示在该节点右側:
double screenX = button.getScene().getWindow().getX() + button.getScene().getX() + button.localToScene(0, 0).getX(); 这里计算出button位于Screen中的位置,然后仅仅须要加上Button的宽度。
double width = button.getBoundsInParent().getWidth(); 由于BoundsInParent包括button的effect效果占的位置,所以用这个比button.getWidth()好。
仅仅要确定了button在Screen中的位置,然后PopWindow想位于button的哪里就好计算了。
button.localToScene(0, 0) 当中有二个參数,这是指Node自身以那个点作为坐标点。 举例:button以左上方点0,0作为位置计算的点,所以參数是0,0。假若是circle,则參数是circle.centerX,circle.centerY。由于circle是以中心点作为坐标点。
获取owner
getOwnerWindow()
getOwnerNode()

再看一下PopupWindow中一些方法:
hide() 隐藏。
setScene(Scene scene) 设置scene,保护类型。PopupWindow已经创建了Scene,并指定了根节点。
getContent() 保护类型,获取根节点的ChildrenList。


看一下getContent的源代码
protected ObservableList getContent() {
final Parent rootNode = getScene().getRoot();
if (!(rootNode instanceof Group)) {
throw new IllegalStateException(
“The content of the Popup can’t be accessed”);
}

return ((Group) rootNode).getChildren();
}
不难发现根节点就是Group。

一般我们要开发PopupWindow,直接使用它的子类Popup。
Popup中就一个getContent() 方法,看它的源代码。就是调用了PopupWindow的getContent方法。


public final ObservableList getContent() {
return super.getContent();
}

当中有些方法,没使用过,理解有点模糊,就没写。知道的大家评论下。互相学习。

原文地址:https://www.cnblogs.com/wgwyanfs/p/6868373.html