Windows taskbar height/width
I can't figure out how to get the windows taskbar height dynamicaly to set my application fullscreen.
As you know, taskbar can be in four positions: bottom, top, left or right, so I'm wondering if it's possible to also know the current position to set the window bounds.EDIT: Using Lukas link I tryied this:
GraphicsDevice myDevice;
Window myWindow;
try {
myDevice.setFullScreenWindow(myWindow);
...
} finally {
myDevice.setFullScreenWindow(null);
}
But I'm getti开发者_StackOverflow中文版n a NullPointerException
It is possible to obtain the Windows taskbar height if necessary:
Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle winSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
int taskBarHeight = scrnSize.height - winSize.height;
When you create your JFrame
. Make a call to the setExtendedState()
method in the JFrame API
jFrame = new JFrame("TESTER");
jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
The MAXIMIZED_BOTH setting will set your window to fullscreen and automatically take into account the position of the Taskbar.
You can use:
int taskbarheight = Toolkit.getDefaultToolkit().getScreenSize().height
- GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height();
or you can use, as well:
JFrame frame = new JFrame();
frame.setSize(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getSize();
If you want your Application to run in full-screen mode, you can enter it by getting a suitable GraphicsDevice
and using the setFullScreenWindow(Window)
-method:
GraphicsDevice myDevice = GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice();
Window myWindow;
try {
myDevice.setFullScreenWindow(myWindow);
...
} finally {
myDevice.setFullScreenWindow(null);
}
For further (and more complete) information. see the Docs)
This is part of a program I wrote:
public enum Location {
TOP, RIGHT, BOTTOM, LEFT;
}
private static final class Taskbar {
public final Location location;
public final int width, height;
private Taskbar(Location location, int width, int height) {
this.location = location;
this.width = width;
this.height = height;
}
public static Taskbar getTaskbar() {
Rectangle other = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getMaximumWindowBounds();
return new Taskbar(other.x != 0 ? Location.TOP
: (other.y != 0 ? Location.LEFT
: (other.width == IFrame.width ? Location.BOTTOM
: Location.RIGHT)), IFrame.width
- other.width, IFrame.height - other.height);
}
}
Essentially, calling Taskbar.getTaskbar()
will give a taskbar containing information on its location (TOP
, RIGHT
, BOTTOM
, LEFT
), its width, and its height.
精彩评论