我有一个很好的primaryStage。我有一个以primaryStage为中心打开的对话框(我通过将对话框的所有者设为primaryStage来实现这个功能
dialog.setInitOwner(primaryStage).
但是,如果 primaryStage 已经靠近屏幕边缘,则对话框将离开屏幕。如果所有者靠近屏幕边缘,如何使对话框(所有者为 primaryStage)不离开屏幕?
我试过这个:
double x = dialog.getX();
double y = dialog.getY();
double w = dialog.getWidth();
double h = dialog.getHeight();
if (x < Main.bounds.getMinX())
{
dialog.setX(Main.bounds.getMinX());
}
if (x + w > Main.bounds.getMaxX())
{
dialog.setX(Main.bounds.getMaxX() - w);
}
if (y < Main.bounds.getMinY())
{
dialog.setY(Main.bounds.getMinY());
}
if (y + h > Main.bounds.getMaxY())
{
dialog.setY(Main.bounds.getMaxY() - h);
}
dialog.showAndWait();
Main.bounds 由以下人员创建:
private static Bounds computeAllScreenBounds()
{
double minX = Double.POSITIVE_INFINITY;
double minY = Double.POSITIVE_INFINITY;
double maxX = Double.NEGATIVE_INFINITY;
double maxY = Double.NEGATIVE_INFINITY;
for (Screen screen : Screen.getScreens())
{
Rectangle2D screenBounds = screen.getVisualBounds();
if (screenBounds.getMinX() < minX)
{
minX = screenBounds.getMinX();
}
if (screenBounds.getMinY() < minY)
{
minY = screenBounds.getMinY();
}
if (screenBounds.getMaxX() > maxX)
{
maxX = screenBounds.getMaxX();
}
if (screenBounds.getMaxY() > maxY)
{
maxY = screenBounds.getMaxY();
}
}
return new BoundingBox(minX, minY, maxX - minX, maxY - minY);
尝试这个,不会改变任何行为。我觉得这是因为 dialog.getX() 函数返回一个 NaN ... }
收到一只叮咚
撒科打诨
相关分类