我正在尝试制作一个弹出式叠加窗口。
public class TestPopup extends PopupControl {
private final Window owner;
private final Rectangle rect = new Rectangle(100, 100, Color.RED);
private boolean isPopupTemporarilyHidden = false;
private final ChangeListener<Boolean> parentFocusChangeListener;
private final NumberBinding aX;
private final NumberBinding aY;
protected TestPopup(Window owner)
{
this.owner = owner;
parentFocusChangeListener = (focusedProperty, wasFocused, isFocused) -> {
if (owner.isShowing())
return;
// Hide the popup when the owner window loses focus
if (!isFocused) {
hide();
isPopupTemporarilyHidden = true;
}
// Show again when the owner window re-gains focus
else if (isPopupTemporarilyHidden) {
show();
isPopupTemporarilyHidden = false;
}
};
final WeakChangeListener<Boolean> weakListener = new WeakChangeListener<>(parentFocusChangeListener);
owner.focusedProperty().addListener(weakListener);
setSkin(new Skin<Skinnable>() {
@Override
public Skinnable getSkinnable() {
return null;
}
@Override
public Node getNode() {
return rect;
}
@Override
public void dispose() {}
});
// Trying to adjust popup location whenever the owner window moves
aX = owner.xProperty().add(owner.getScene().xProperty()).add(10);
aY = owner.yProperty().add(owner.getScene().yProperty()).add(10);
// anchorX and anchorY are read-only - cannot bind
aX.addListener(ax -> setAnchorX(aX.getValue().doubleValue()));
aY.addListener(ay -> setAnchorY(aY.getValue().doubleValue()));
}
public void showPopup() {
show(owner, owner.getX() + owner.getScene().getX() + 10, owner.getY() + owner.getScene().getY() + 10);
}
}
如果我最小化窗口,然后通过任务栏将其最小化,则弹出窗口(带有红色矩形)将在所有者窗口缓慢创建动画之前显示在屏幕上。我使用的是Windows 10,因此本机窗口确实具有当它们最小化时的动画。
我也尝试读取Stage.iconifiedProperty()
的值,但是似乎该值在动画结束之前更改为false
。动画结束后,有什么方法可以让弹出窗口显示 吗?
另外一个问题:当我在屏幕上拖动所有者窗口时,弹出窗口中会有明显的滞后。无论如何,我可以做些什么来减少延迟?