Windows上的JDialog延伸到Windows任务栏之外.任何解决方法? [英] JDialog on windows extends past the windows taskbar. Any workarounds?

查看:51
本文介绍了Windows上的JDialog延伸到Windows任务栏之外.任何解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题讨论了一个已知的错误,其中JFrame扩展到Windows任务栏中. 答案链接到错误报告(该报告有多个重复项)并提供了一种解决方法.我发现该问题也适用于JDialogs. JFrame解决方法不适用.有没有类似的解决方法可以使JDialogs在Windows上表现自己?

This question discusses a known bug where JFrames extend into the windows taskbar. An answer links to the bug report (which has various duplicates) and provides a workaround. I've discovered that the problem also holds for JDialogs. The JFrame workaround does not apply. Is there a similar workaround to make JDialogs behave themselves on windows?

示例代码:

import javax.swing.*;

public class Demo extends JDialog {
    public Demo() {
        setSize(250,12500);
        setVisible(true);
    }
    public static void main(String[] args) {
        new Demo();
    }
}

修改:
看来这不会在JDK中修复. 此错误报告以如果开发人员希望保持其窗口在屏幕上完全可见,他们应该考虑自己检查屏幕插图 [类似于下面的解决方案] ,并以其他方式重新布局其组件,或者在之后手动调整窗口大小调用pack(),或使用屏幕插入感知的布局管理器 [不同于BorderLayout和GridBagLayout等更常见的布局管理器] ."


It looks like this will not be fixed in the JDK. This bug report closes with the comment that "If a developer wants to keep their windows entirely visible on the screen, they should consider checking the screen insets themselves [like the solution below], and re-layout their component in a different way, or re-size the window manually after calling pack(), or use a screen-insets-aware layout manager [unlike more common ones like BorderLayout and GridBagLayout]."

推荐答案

这基本上可以通过将对话框移到指定设备的范围内并将其缩小以确保该对话框适合"指定的屏幕左边缘和下边缘在指定设备的范围内.

This will basically make sure that the dialog "fits" into the specified screen by moving it to be within the bounds of the specified device and shrinking it so that it's left and bottom edges are within the bounds of the specified device.

这将查看设备的边界和插入点,以计算对话框可以驻留的安全"区域.

This looks at the devices bounds and insets to calculate the "safe" area that the dialog can reside.

public class TestScreenSize {

    public static void main(String[] args) {
        new TestScreenSize();
    }

    public TestScreenSize() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                Test test = new Test();
                test.setLayout(new BorderLayout());
                test.setVisible(true);
                System.exit(0);
            }
        });
    }

    public class Test extends JDialog {

        public Test() {
            setModal(true);
            setLocation(0, 0);
            setSize(2000, 2000);
        }

        @Override
        public void setBounds(int x, int y, int width, int height) {
            Rectangle bounds = getSafeScreenBounds(new Point(x, y));
            if (x < bounds.x) {
                x = bounds.x;
            }
            if (y < bounds.y) {
                y = bounds.y;
            }
            if (width > bounds.width) {
                width = (bounds.x + bounds.width) - x;
            }
            if (height > bounds.height) {
                height = (bounds.y + bounds.height) - y;
            }
            super.setBounds(x, y, width, height);
        }

    }

    public static Rectangle getSafeScreenBounds(Point pos) {

        Rectangle bounds = getScreenBoundsAt(pos);
        Insets insets = getScreenInsetsAt(pos);

        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= (insets.left + insets.right);
        bounds.height -= (insets.top + insets.bottom);

        return bounds;

    }

    public static Insets getScreenInsetsAt(Point pos) {
        GraphicsDevice gd = getGraphicsDeviceAt(pos);
        Insets insets = null;
        if (gd != null) {
            insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());
        }
        return insets;
    }

    public static Rectangle getScreenBoundsAt(Point pos) {
        GraphicsDevice gd = getGraphicsDeviceAt(pos);
        Rectangle bounds = null;
        if (gd != null) {
            bounds = gd.getDefaultConfiguration().getBounds();
        }
        return bounds;
    }

    public static GraphicsDevice getGraphicsDeviceAt(Point pos) {

        GraphicsDevice device = null;

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice lstGDs[] = ge.getScreenDevices();

        ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);

        for (GraphicsDevice gd : lstGDs) {

            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            Rectangle screenBounds = gc.getBounds();

            if (screenBounds.contains(pos)) {

                lstDevices.add(gd);

            }

        }

        if (lstDevices.size() > 0) {
            device = lstDevices.get(0);
        } else {
            device = ge.getDefaultScreenDevice();
        }

        return device;

    }
}

这篇关于Windows上的JDialog延伸到Windows任务栏之外.任何解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆