Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在活动屏幕或多屏幕设置中的监视器上打开JavaFX应用程序_Java_Javafx - Fatal编程技术网

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

在活动屏幕或多屏幕设置中的监视器上打开JavaFX应用程序,java,javafx,Java,Javafx,我想知道在使用教程中的示例JavaFX应用程序时,是否有一种方法可以使JavaFX桌面独立于平台的应用程序窗口在当前活动屏幕上打开 我使用的是双监视器系统,如果有办法每次都在活动屏幕而不是主屏幕上打开工具,那就太好了 到目前为止,我能够学习在屏幕上设置自定义XY位置以打开应用程序窗口,但这使用了主桌面监视器 在应用程序启动时,或多或少地在屏幕上寻找鼠标光标所在窗口的中心位置 更新: 这是可以在Windows窗体C#中通过设置Form.StartPosition属性实现的。基本上是告诉应用程序从用

我想知道在使用教程中的示例JavaFX应用程序时,是否有一种方法可以使JavaFX桌面独立于平台的应用程序窗口在当前活动屏幕上打开

我使用的是双监视器系统,如果有办法每次都在活动屏幕而不是主屏幕上打开工具,那就太好了

到目前为止,我能够学习在屏幕上设置自定义XY位置以打开应用程序窗口,但这使用了主桌面监视器

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

更新:


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

我认为没有这样的工具可以做到这一点。但是,您可以创建一个应用程序来确定鼠标的位置,然后该应用程序应该启动JavaFX应用程序并传递鼠标参数


在JavaFX应用程序中,您只需处理参数并将窗口设置到正确的位置。

Like@galovics说,您可以在显示舞台之前确定鼠标位置,请查看以下内容:
希望能有所帮助

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

这只是一个尝试,并不是最终的答案,但我想分享一段相当有效的代码,给出一个想法。我在Windows和Ubuntu上测试过,效果很好。仅供参考,我仍在学习JavaFX

下面的代码在启动时将我的不可重新调整大小的JavaFX应用程序窗口集中到活动的显示屏或监视器屏幕上

注意:仅当您像我的情况一样固定了预定义的窗口大小时,此选项才有效。否则,JavaFX会在
Stage.show()
之后计算窗口大小,然后在
show()
方法之前无法获得宽度和高度。它返回
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;
    }
}
请随意提出改进建议或任何错误


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

哦,我明白了,在Windows窗体中,我总是可以通过设置Form.StartPosition属性来实现这一点。很遗憾JavaFX在默认情况下不能做到这一点。
    // 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();
        }
    }