Java GUI 隐藏窗口任务栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6790600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java GUI hides windows taskbar
提问by Asghar
I am using JFrame
to create my GUI for a desktop application. The size of the GUI I am setting according to the resolution of the platform screen using this code.
我正在JFrame
为桌面应用程序创建 GUI。我根据使用此代码的平台屏幕的分辨率设置的 GUI 大小。
this.setSize(this.getToolkit().getScreenSize());
The problem is that when I run the application the GUI covers all of the screen. The Windows task-bar is also hidden behind the GUI.
问题是当我运行应用程序时,GUI 覆盖了整个屏幕。Windows 任务栏也隐藏在 GUI 后面。
I want that whatever the size of the task-bar is, the task-bar should be visible in all conditions. How do I achieve that?
我希望无论任务栏的大小如何,任务栏都应该在所有条件下都可见。我如何做到这一点?
采纳答案by StanislavL
What about ?
关于什么 ?
jFrame.setState(JFrame.MAXIMIZED_BOTH);
回答by Hunter McMillen
Since you are using a JFrame you should just call:
由于您使用的是 JFrame,您应该只调用:
jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
This takes into account the position of the taskbar.
这考虑了任务栏的位置。
回答by Andrew Thompson
Is your task-bar set to auto-hide?
您的任务栏是否设置为自动隐藏?
I just ran this test code on my Windows 7 machine.
我刚刚在我的 Windows 7 机器上运行了这个测试代码。
import java.awt.Frame;
import javax.swing.*;
class TestFrameSize {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("Test Screen Size");
f.setAlwaysOnTop(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(f.getToolkit().getScreenSize());
f.setExtendedState(Frame.MAXIMIZED_BOTH);
f.setVisible(true);
System.out.println(f.getSize());
}
});
}
}
In fact, I ran it twice. Here is the output:
事实上,我运行了两次。这是输出:
Task-bar configured to 'auto-hide'
任务栏配置为“自动隐藏”
java.awt.Dimension[width=1920,height=1080]
java.awt.Dimension[width=1928,height=1088]
(In which the frame seems to be 8 pixels taller & wider than the available screen space - odd.)
(其中框架似乎比可用屏幕空间高和宽 8 个像素 - 奇怪。)
Task-bar notconfigured to 'auto-hide'
任务栏未配置为“自动隐藏”
java.awt.Dimension[width=1920,height=1080]
java.awt.Dimension[width=1928,height=1048]
40 pixels shorter, and no longer covering the task-bar.
缩短 40 像素,不再覆盖任务栏。
回答by W A K A L E Y
I know this is old question but when i use jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
or the setState
it displays it properly while displaying it for the first time. When I minimize it and again maximize it, it again covers the Task Bar. I am using Windows 7 and Java 1.7.
我知道这是个老问题,但是当我使用jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
或setState
第一次显示它时它会正确显示它。当我最小化它并再次最大化它时,它再次覆盖任务栏。我使用的是 Windows 7 和 Java 1.7。
I found the solution here
我在这里找到了解决方案
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
f.setMaximizedBounds(env.getMaximumWindowBounds());
f.setVisible(true);
this should do the trick.
这应该可以解决问题。
回答by sealz
you should be able to find the TaskbarHeight with a method
您应该能够使用方法找到 TaskbarHeight
say getTaskbarHeight();
说 getTaskbarHeight();
the minus that from
减去从
setFullScreen();
I found this example online
我在网上找到了这个例子
回答by Anthony Holland
It is quite a lot easier than all this! The taskbar exists in the insets of the screen. So to get the space you want to occupy just use the results of the following two calls:
这比这一切要容易得多!任务栏存在于屏幕的插图中。因此,要获得您想要占用的空间,只需使用以下两个调用的结果:
Toolkit.getDefaultToolkit().getScreenInsets(f.getGraphicsConfiguration())
and
和
f.getGraphicsConfiguration().getBounds();
where f
is a window on the same screen as the taskbar.
f
与任务栏在同一屏幕上的窗口在哪里。
Do not use Toolkit.getDefaultToolkit().getScreenSize(); because it will give you the size of the first screen but not necessarily the screen you are on.
不要使用 Toolkit.getDefaultToolkit().getScreenSize(); 因为它会给你第一个屏幕的大小,但不一定是你所在的屏幕。
Try playing around with the below frame while you
尝试玩下面的框架,而你
- move the window from one screen to another
- drag the task-bar from one edge to another of each screen
- resize the task-bar
- make the task-bar autohide
- adjust your screen resolution.
- 将窗口从一个屏幕移动到另一个屏幕
- 将任务栏从每个屏幕的一个边缘拖到另一个边缘
- 调整任务栏大小
- 使任务栏自动隐藏
- 调整你的屏幕分辨率。
Each time you press the button it will faithfully print out everything you need to known about the task-bar's position.
每次您按下按钮时,它都会忠实地打印出您需要了解的有关任务栏位置的所有信息。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TaskBarExplorer {
public static void main(String[] args) {
final Frame f = new JFrame("F");
JButton b = new JButton("do");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
print("current device");
print(f.getGraphicsConfiguration());
GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
print("");
print("all devices");
for (GraphicsDevice gd : gds) {
print(gd.getDefaultConfiguration());
}
print("");
}
});
f.add(b);
f.setSize(100, 100);
f.setLocation(600, 400);
f.setVisible(true);
}
private static void print(GraphicsConfiguration gc) {
print("insets: " + Toolkit.getDefaultToolkit().getScreenInsets(gc));
print("bounds: " + gc.getBounds());
}
private static void print(String s) {
System.out.println(s);
}
}
Some example output for the lazy (this is from one execution, button pressed after different adjustments):
懒惰的一些示例输出(这是来自一次执行,不同调整后按下的按钮):
current device
insets: java.awt.Insets[top=0,left=0,bottom=78,right=0]
bounds: java.awt.Rectangle[x=0,y=0,width=1280,height=1024]
all devices
insets: java.awt.Insets[top=0,left=0,bottom=78,right=0]
bounds: java.awt.Rectangle[x=0,y=0,width=1280,height=1024]
insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]
current device
insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]
all devices
insets: java.awt.Insets[top=0,left=0,bottom=78,right=0]
bounds: java.awt.Rectangle[x=0,y=0,width=1280,height=1024]
insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]
current device
insets: java.awt.Insets[top=0,left=0,bottom=0,right=58]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]
all devices
insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
bounds: java.awt.Rectangle[x=0,y=0,width=1280,height=1024]
insets: java.awt.Insets[top=0,left=0,bottom=0,right=58]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]
回答by Cyril D.
Perhaps you shoud use GraphicsDevice.setFullScreenWindow() instead of setFullScreen(). It will maximize a window as opposed to using the screen fully in a "sort-of" non windowed mode.
也许您应该使用 GraphicsDevice.setFullScreenWindow() 而不是 setFullScreen()。它将最大化一个窗口,而不是在“某种”非窗口模式下完全使用屏幕。
回答by Jalal Sordo
This is what worked for me in Windows 10/Java 8:
这就是在 Windows 10/Java 8 中对我有用的方法:
setExtendedState(Frame.MAXIMIZED_BOTH);
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
setSize(env.getMaximumWindowBounds().getSize());