Java GUI隐藏窗口任务栏



我使用JFrame为桌面应用程序创建GUI。我使用这段代码根据平台屏幕的分辨率设置GUI的大小。

this.setSize(this.getToolkit().getScreenSize());

问题是,当我运行应用程序时,GUI覆盖了整个屏幕。Windows任务栏也隐藏在GUI后面。

我希望无论任务栏的大小如何,任务栏应该在所有情况下都是可见的。我该怎么做呢?

由于您使用的是JFrame,您应该只调用:

jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

这考虑了任务栏的位置

任务栏设置为自动隐藏吗?

我刚刚在我的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());
            }
        });
    }
}

事实上,我运行了两次。下面是输出:

任务栏设置为'auto-hide'

java.awt.Dimension[width=1920,height=1080]
java.awt.Dimension[width=1928,height=1088]

(其中帧看起来高了8像素&比可用的屏幕空间宽-奇数。)

任务栏配置为'auto-hide'

java.awt.Dimension[width=1920,height=1080]
java.awt.Dimension[width=1928,height=1048]

缩短40像素,不再覆盖任务栏

我知道这是一个老问题,但是当我使用jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);setState时,它在第一次显示它时正确显示它。当我最小化并再次最大化它时,它再次覆盖任务栏。我用的是Windows 7和Java 1.7。

我在这里找到了解决方案

f.setExtendedState(JFrame.MAXIMIZED_BOTH);  
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();  
f.setMaximizedBounds(env.getMaximumWindowBounds());  
f.setVisible(true);  

你应该能够找到任务栏的高度与方法

say getTaskbarHeight();

减去

setFullScreen();

我在网上找到这个例子

设置屏幕大小-任务栏

也许你应该使用GraphicsDevice.setFullScreenWindow()而不是setFullScreen()。它将最大化一个窗口,而不是在"有点"非窗口模式下使用整个屏幕。

这可比这简单多了!任务栏存在于屏幕的插页中。因此,要获得想要占用的空间,只需使用以下两个调用的结果:

Toolkit.getDefaultToolkit().getScreenInsets(f.getGraphicsConfiguration())

f.getGraphicsConfiguration().getBounds();

其中f是与任务栏在同一屏幕上的窗口。

不要使用Toolkit.getDefaultToolkit().getScreenSize();因为它会给你第一个屏幕的大小,但不一定是你所在的屏幕。

当你

  • 将窗口从一个屏幕移动到另一个屏幕
  • 将任务栏从屏幕的一边拖到另一边
  • 调整任务栏大小使任务栏自动隐藏
  • 调整屏幕分辨率。

每次你按下这个按钮,它都会忠实地打印出你需要知道的关于任务栏位置的所有信息。

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

惰性执行的一些输出示例(这是一次执行,在不同的调整后按下按钮):

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]

为什么?

jFrame.setState(JFrame.MAXIMIZED_BOTH);

这是我在Windows 10/Java 8中工作的方法:

setExtendedState(Frame.MAXIMIZED_BOTH);
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();  
setSize(env.getMaximumWindowBounds().getSize());

最新更新