我想知道是否有一种方法可以在教程中的示例JavaFX应用程序上工作时,在当前活动的屏幕上打开与JavaFX桌面平台无关的应用程序窗口.
我正在使用两个监视器系统,如果有一种方法可以使该工具每次都在活动屏幕上而不是主屏幕上打开,那将是很好的.
到目前为止,我已经能够学习在屏幕上设置自定义XY位置以打开应用程序窗口,但这使用了主要的桌面监视器.
应用程序启动时,或多或少希望将窗口居中显示在光标所在的屏幕上.
更新:
这是可以通过设置Form.StartPosition属性在Windows Forms C#中实现的.基本上是告诉应用程序从用户当前正在工作或正在看的桌面屏幕上启动或打开.
解决方法:
感谢@galovics和@Rafael Guillen提供的提示1和2,我得以解决此问题.
这只是一个尝试,而不是最终的答案,但想分享相当有效的代码段以给出一个想法.我已经在Windows和Ubuntu上对其进行了测试,效果很好.仅供参考,我仍在学习JavaFX.
以下代码在启动时将我无法调整大小的JavaFX应用程序窗口居中到活动的显示或监视器屏幕.
注意:仅在您具有固定的预定义窗口大小(如我的情况)时,此方法才有效.否则,JavaFX将在Stage.show()之后计算窗口大小,然后在show()方法之前无法获得Width和Height.它返回NaN.在课堂上阅读有关在这种情况下如何使用它的评论.
/*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Window_On_ActiveScreen;
import java.awt.HeadlessException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.util.List;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
/**
* X-Y position of a Window on active screen at startup if more than one screen.
* Note: This works smooth only if the outer most AnchorPane size is fixed at
* design time. This is because, if the size is not fixed JavaFX calculates
* Window size after Stage.show() method. If the pref size is fixed, then use
* this class in WindowEvent.WINDOW_SHOWING event, or if the pref size is set to
* USE_COMPUTED_SIZE then use it in WindowEvent.WINDOW_SHOWN event (this will
* give a quick splash Window though). Feel free to improve and share this code.
* I am new to JavaFX so tired what I know so far. Tested on Windows but need
* more attention to Linux and Mac
*
* @author
*/
public class StartUpLocation {
private double xPos = 0;
private double yPos = 0;
/**
* Get Top Left X and Y Positions for a Window to centre it on the
* currently active screen at application startup
* @param windowWidth - Window Width
* @param windowHeight - Window Height
*/
public StartUpLocation(double windowWidth, double windowHeight) {
// Get X Y of start-up location on Active Screen
// simple_JavaFX_App
try {
// Get current mouse location, could return null if mouse is moving Super-Man fast
Point p = MouseInfo.getPointerInfo().getLocation();
// Get list of available screens
List<Screen> screens = Screen.getScreens();
if (p != null && screens != null && screens.size() > 1) {
// Screen bounds as rectangle
Rectangle2D screenBounds;
// Go through each screen to see if the mouse is currently on that screen
for (Screen screen : screens) {
screenBounds = screen.getVisualBounds();
// Trying to compute Left Top X-Y position for the Application Window
// If the Point p is in the Bounds
if (screenBounds.contains(p.x, p.y)) {
// Fixed Size Window Width and Height
xPos = screenBounds.getMinX() + ((screenBounds.getMaxX() - screenBounds.getMinX() - windowWidth) / 2);
yPos = screenBounds.getMinY() + ((screenBounds.getMaxY() - screenBounds.getMinY() - windowHeight) / 2);
return;
}
}
}
} catch (HeadlessException headlessException) {
// Catch and report exceptions
headlessException.printStackTrace();
}
}
/**
* @return the top left X Position of Window on Active Screen
*/
public double getXPos() {
return xPos;
}
/**
* @return the top left Y Position of Window on Active Screen
*/
public double getYPos() {
return yPos;
}
}
现在,将这些X和Y位置(如果不为零)设置为Stage或仅将centerOnScreen设置为该值.在此处进行检查很重要,因为如果鼠标移动得太快,则点p可能会返回null,其余的计算也将返回null任何例外.
// If Outer most AnchorPane pref size is fixed then use it in this event
// Else otherwise use the handleWindowShownEvent(WindowEvent event)
public void handleWindowShowingEvent(WindowEvent event) {
Stage stage = (Stage) event.getSource();
// Get X Y of start-up location on Active Screen
StartUpLocation startUpLoc = new StartUpLocation(mainWindowAnchorPane.getPrefWidth(), mainWindowAnchorPane.getPrefHeight());
double xPos = startUpLoc.getXPos();
double yPos = startUpLoc.getYPos();
// Set Only if X and Y are not zero and were computed correctly
if (xPos != 0 && yPos != 0) {
stage.setX(xPos);
stage.setY(yPos);
} else {
stage.centerOnScreen();
}
}
欢迎在这里提出改进建议或任何错误.
更新:作为类中的通用方法添加.