我有一个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...... }
根据要求,举个例子:
public class Test extends Application
{
@Override
public void start(Stage primaryStage)
{
Bounds bounds = computeAllScreenBounds();
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText("This is an alert!");
alert.setContentText("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
alert.initOwner(primaryStage);
double x = alert.getX();
double y = alert.getY();
double w = alert.getWidth();
double h = alert.getHeight();
if (x < bounds.getMinX())
{
alert.setX(bounds.getMinX());
}
if (x + w > bounds.getMaxX())
{
alert.setX(bounds.getMaxX() - w);
}
if (y < bounds.getMinY())
{
alert.setY(bounds.getMinY());
}
if (y + h > bounds.getMaxY())
{
alert.setY(bounds.getMaxY() - h);
}
alert.showAndWait();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
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);
}
}
另一个前任:
public class Test extends Application
{
@Override
public void start(Stage primaryStage)
{
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText("This is an alert!");
alert.setContentText("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
alert.initOwner(primaryStage);
alert.setOnShown(new EventHandler<DialogEvent>()
{
@Override
public void handle(DialogEvent event)
{
double x = alert.getX();
double y = alert.getY();
double w = alert.getWidth();
double h = alert.getHeight();
//SHOWS ALL NaN NaN NaN NaN
System.out.println(x + " " + y + " " + w + " " + h);
}
});
alert.showAndWait();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
最佳答案
使用 showAndWait 时警报上的 onShown 处理程序的(对我来说出乎意料的)问题是,在调用处理程序时,警报尚未显示且尚未调整大小/位置 - 这有点疯狂,可能可能是被认为是一个错误。
解决方法是监听警报的 ShowingProperty 并在该监听器中进行任何大小/位置调整。类似的东西(显然只是一个 poc)
alert.showingProperty().addListener((src, ov, nv) -> {
double x = alert.getX();
double y = alert.getY();
double w = alert.getWidth();
double h = alert.getHeight();
// as example just adjust if location top/left is off
// production must cope with bottom/right off as well, obviously
if (x < 0) {
alert.setWidth(w + x);
alert.setY(0);
}
if (y <0) {
alert.setHeight(h + y);
alert.setY(0);
}
});
alert.showAndWait();
仅供引用:我确实认为这是一个错误,因此提交了 issue让我们看看会发生什么
关于java - 防止对话框离开屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52062291/