Java setResizable(false)改变窗口大小(swing) [英] Java setResizable(false) changes the window size (swing)

查看:1024
本文介绍了Java setResizable(false)改变窗口大小(swing)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。我正在使用窗口的空布局(= JFrame和窗口),如果我使用setResizable(false),窗口大小会变大(到右边和底部,我会说大约10个像素)。我不知道为什么。

I have a strange problem. I am using the null layout for a window (= JFrame and on windows) and if I use setResizable (false) the window size gets bigger (to right and bottom, around 10 pixels I would say). I do not know why.

这两个println返回相同的尺寸,奇怪的是,还... ...

The two println's return the same sizes, what is strange, also...

mainWnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWnd.setTitle(wndTitle);
mainWnd.setBounds(wndPosX, wndPosY, wndWidth, wndHeight);
System.out.println(mainWnd.getHeight() + mainWnd.getWidth());
mainWnd.setResizable(false);
System.out.println(mainWnd.getHeight() + mainWnd.getWidth());

有人有想法吗?为什么窗口会被调整大小?

Does somebody has an idea? Why does the window gets resized?

UPDATE:
这里也是一样(使用和不使用setResizable编译它,如果你重叠,你可以看到它windows):

UPDATE: Same thing here (compile it with and without the setResizable and than you can see it, if you overlap the windows):

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Main
{
    private static JFrame mainWnd = null;

    public static void main(String[] args)
    {
        mainWnd = new JFrame();

        mainWnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWnd.setTitle("asda");
        mainWnd.setBounds(50, 50, 300, 300);

        mainWnd.setResizable(false);

        mainWnd.setVisible(true);
    }
}


推荐答案

它在我的例子中没有改变,所以你必须有其他东西导致你的问题:

It does not change on my example, so you must have something else that causes your issue:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test4 {

    protected static void initUI() {
        JFrame frame = new JFrame("test");
        frame.setBounds(0, 0, 300, 200);
        frame.setVisible(true);
        System.err.println(frame.getSize());
        frame.setResizable(false);
        System.err.println(frame.getSize());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                initUI();
            }
        });
    }
}

编辑/更新:

将resizable设置为false(至少在Windows 7和JDK 6上)时,不知何故插入不正确。不知何故,它们从30,8,8,8变为25,3,3,3,尽管边界(由操作系统绘制)实际上保持不变。由于insets是Frame的边界的一部分,因此当它不可调整大小时,框架实际上太大(视觉上)。对我来说,当帧不可调整大小时,计算出的插图中有一个错误。

Somehow insets are incorrect when you set the resizable to false (at least on Windows 7 and JDK 6). Somehow they change from 30,8,8,8 to 25,3,3,3 although the border (which is painted by the OS) stays actually the same. Since insets are part of the bounds of the Frame, the frame is actually too big (visually) when it is not resizable. For me it looks like there is a bug in the computed insets when the frame is not resizable.

这篇关于Java setResizable(false)改变窗口大小(swing)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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