Reserving the edge of the screen in Java

寵の児 提交于 2019-12-01 03:18:55

问题


How can I make a window (such as a java.awt.Frame, javax.swing.JFrame, java.awt.Dialog, javax.swing.JDialog, etc.) reserve the edge of the screen, like a dock, taskbar, Trillian, or Microsoft OneNote can? I aim to only use standard JDK libraries if possible.


回答1:


This behaviour is very specific to the Windows operating system, and as such there is no way to do this in the standard JDK or with AWT/Swing, since these need to work consistently across a number of different operating systems.

The closest equivalent to what you want is to get the current screen's boundaries and move/resize your window to fit flush against one side, although this will now have any effect on the desktop or other windows.

The closest thing you will probably get to what you want is by using JNIWrapper and WinPack to alter the native window behaviour. Their documentation doesn't show anything about desktop toolbars, though. You would probably have to use their JNI to access the specific COM functions directly and then find out how to do it in C++, but if you don't know C++, that's probably out of the scope of your project.

Addendum:

If you want to make use of the native features for specific operating systems and desktop managers, you will need to connect with a native C++ library to hook into Gnome, KDE, Mac OS etc. Search for 'Java JNI' and the windowing system you want to integrate with to find packages you can use. For instance, for Gnome, see here.

If you want to support multiple operating systems, make sure you create a common interface so you can make implementations that connect with the respective JNIs depending on which one is available at runtime, and allow fallback if none of them are available.

PS. If you want popup notifications in your app, Mac OS and various Linux builds support Growl so that can save you some time.




回答2:


You can use Toolkit.getDefaultToolkit().getScreenInsets() From JMenu sources

Toolkit toolkit = Toolkit.getDefaultToolkit();
    GraphicsConfiguration gc = getGraphicsConfiguration();
    Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
    GraphicsEnvironment ge =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();
    for(int i = 0; i < gd.length; i++) {
        if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
            GraphicsConfiguration dgc =
                gd[i].getDefaultConfiguration();
            if(dgc.getBounds().contains(position)) {
                gc = dgc;
                break;
            }
        }
    }


    if (gc != null) {
        screenBounds = gc.getBounds();
        // take screen insets (e.g. taskbar) into account
        Insets screenInsets = toolkit.getScreenInsets(gc);

        screenBounds.width -= 
                    Math.abs(screenInsets.left + screenInsets.right);
        screenBounds.height -= 
                    Math.abs(screenInsets.top + screenInsets.bottom);
        position.x -= Math.abs(screenInsets.left);
        position.y -= Math.abs(screenInsets.top);
    }


来源:https://stackoverflow.com/questions/4660102/reserving-the-edge-of-the-screen-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!