我正在处理有一个通常的登录窗口(JavaFX‘8对话框)的应用程序,然后继续接受它打开新窗口(JFrame)。如果它在多监视器环境中运行,它应该按照这样的方式工作,即JFrame将在关闭JavaFX‘8对话框的同一监视器上打开。
现在,我可以在监视器上打开JFrame,可以使用GraphicsEnvironment手动选择,但此时我无法确定在哪个监视器JavaFX‘8对话框上关闭了。有什么想法吗?谢谢。
发布于 2015-03-20 09:10:08
好吧,这就是对我有用的东西:
/**
* As an argument you pass a Scene(of Java FX 8 Dialog in my example),
* by closing it you want JFrame to open on same monitor.
**/
public static int getActiveStageLocation(Scene scene){
List interScreens = Screen.getScreensForRectangle(scene.getWindow().getX(),
scene.getWindow().getY(),
scene.getWindow().getWidth(),
scene.getWindow().getHeight());
Screen activeScreen = (Screen) interScreens.get(0);
Rectangle2D r = activeScreen.getBounds();
double position = r.getMinX();
return (int) position;
}然后简单地设置JFrame的设置位置:
/**
* Some code for instantiating frame and so on....
* Parameter scene you could get for example from your FXML
**/
myFrame.setLocation(getActiveStageLocation(scene), myFrame.getY());
myFrame.setVisible(true);发布于 2015-03-19 08:27:08
也许您可以在关闭JavaFX对话框时保存您的位置:
private void saveWindowPosition() {
Preferences userPrefs = Preferences.userNodeForPackage(getClass());
userPrefs.putDouble("stage.x", primaryStage.getX());
userPrefs.putDouble("stage.y", primaryStage.getY());
}像这样读出来:
private void restoreWindowPosition() {
Preferences userPrefs = Preferences.userNodeForPackage(getClass());
// get window location from user preferences: use x=100, y=100 as
// default
double x = userPrefs.getDouble("stage.x", 100);
double y = userPrefs.getDouble("stage.y", 100);
primaryStage.setX(x);
primaryStage.setY(y);
}https://stackoverflow.com/questions/29139165
复制相似问题