开发者

Reserving the edge of the screen in Java

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 librar开发者_如何学运维ies if possible.


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.


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜