如何在java swing中设置特定的窗口(框架)大小? [英] How to set specific window (frame) size in java swing?

查看:341
本文介绍了如何在java swing中设置特定的窗口(框架)大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码不起作用:

JFrame frame = new JFrame("mull");

mull panel = new mull();

// add panel to the center of window
frame.getContentPane().add("Center", panel);
frame.setSize(500, 300); // << not working!!!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); // give a suitable size to window automatically
frame.setVisible(true); // make window visible

我的窗口非常小。如何解决?

I am getting very small window. How to fix it?

推荐答案

嗯,你正在使用 frame.setSize() frame.pack()

您应该一次使用其中一个。

You should use one of them at one time.

使用 setSize()你可以给出你想要的帧大小但是如果你使用 pack(),它会根据其中组件的大小自动更改帧的大小。它不会考虑您之前提到的大小。

Using setSize() you can give the size of frame you want but if you use pack(), it will automatically change the size of the frames according to the size of components in it. It will not consider the size you have mentioned earlier.

尝试从您的代码中删除 frame.pack()或在设置大小之前将其放置然后运行它。

Try removing frame.pack() from your code or putting it before setting size and then run it.

这篇关于如何在java swing中设置特定的窗口(框架)大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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