gpt4 book ai didi

java - 在多屏幕设置中的 Activity 屏幕或监视器上打开 JavaFX 应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:29 25 4
gpt4 key购买 nike

我想知道是否有办法在当前 Activity 屏幕上打开 JavaFX 桌面平台独立应用程序窗口,同时处理教程中的示例 JavaFX 应用程序。

我正在使用两个监视器系统,如果有一种方法可以让该工具每次都在 Activity 屏幕而不是主屏幕上打开,那就太好了。

到目前为止,我已经学会了在屏幕上设置自定义 XY 位置以打开应用程序窗口,但这使用的是主桌面显示器。

或多或少地寻找应用程序启动时鼠标光标所在屏幕上的窗口居中。

更新:

这可以通过设置 Form.StartPosition 属性在 Windows 窗体 C# 中实现。基本上告诉应用程序从用户当前正在工作或查看的桌面屏幕开始或打开。

最佳答案

感谢@galovics 和@Rafael Guillen 的提示 12 ,我能够解决这个问题。

这只是一个尝试而不是最终答案,但我想分享一段相当有效的代码来给出一个想法。我已经在 Windows 和 Ubuntu 上测试过它,效果很好。仅供引用,我还在学习 JavaFX。

以下代码在启动时将我不可调整大小的 JavaFX 应用程序窗口置于 Activity 显示或监视器屏幕的中心。

注意:这仅在您像我的情况一样具有固定的预定义窗口大小时才有效。否则 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();
}
}

请随时在这里提出改进建议或任何错误。

更新:添加为类中的通用方法。

关于java - 在多屏幕设置中的 Activity 屏幕或监视器上打开 JavaFX 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25714573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com