java 界面大小不可变,Java setResizable(false)改变窗口大小(swing)

博主在使用Java的JFrame时遇到一个问题,当设置窗口为不可调整大小(setResizable(false))时,窗口的尺寸反而增大了约10像素。尽管窗口边界的设置值没有改变,但窗口的实际大小发生了变化。经过测试,发现在Windows 7和JDK 6环境下,窗口的边距(insets)在设置不可调整大小后不正确,可能导致了视觉上的尺寸增大。可能的原因是计算 insets 时存在一个bug。
摘要由CSDN通过智能技术生成

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.

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:

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

}

});

}

}

EDIT/UPDATE:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值