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

时间:2014-09-07 20:39:43

标签: java javafx

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

我正在使用两个显示器系统,如果有办法在每次活动屏幕而不是主屏幕上打开工具,那将会很棒。

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

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

更新

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

3 个答案:

答案 0 :(得分:2)

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

这只是一个尝试,而不是最终的答案,但我想分享相当有用的代码片段来提出一个想法。我在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;
    }
}

现在,设置Stage这些X和Y位置,如果不是零或只是设置centerOnScreen这里检查这一点很重要,因为如果鼠标移动得太快,点p可能会返回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();
        }
    }

随时建议改进或任何错误。

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

像@galovics一样,您可以在显示舞台之前确定鼠标位置,请查看: http://docs.oracle.com/javafx/2/api/javafx/stage/Screen.html https://community.oracle.com/thread/2390272?tstart=0 希望它有所帮助