NetBeans (Java Swing):设置窗口大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1933016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:34:45  来源:igfitidea点击:

NetBeans (Java Swing): Set window size

javaswingnetbeans

提问by Johan

This is driving me crazy. When I set an appropriate size for my window in design-mode it just ignores this size and uses something else when I compile and run. Even tough I set the minimumSize and preferredSize it just ignores this... Why? How can I set my own size?

这真让我抓狂。当我在设计模式下为我的窗口设置合适的大小时,它只会忽略这个大小并在我编译和运行时使用其他大小。即使很难,我设置了 minimumSize 和 preferredSize 它只是忽略了这一点......为什么?如何设置自己的尺寸?

采纳答案by Spoike

Even if you've set the size with minimumSizeand preferredSize, you might have forgotten to call Window.pack()in which Swing will resize the components (and all of it's subcomponents) according to the sizes set.

即使您使用minimumSizeand设置了大小preferredSize,您也可能忘记调用Window.pack()which Swing 将根据设置的大小调整组件(及其所有子组件)的大小。

You call it in your window (or whatever is building your window) after all the preferred sizes are set.

在设置了所有首选大小后,您可以在窗口(或构建窗口的任何东西)中调用它。

Alternatively you can use the Component.setSize()method but it comes with some caveats.

或者,您可以使用该Component.setSize()方法,但它带有一些警告

回答by Peter Lang

Have you checked if you really set the size of the JFrame or of a contained JPanel?

您是否检查过是否真的设置了 JFrame 或包含的 JPanel 的大小?

Have you tried setSize?

你试过setSize吗?

回答by Gurce

I've found myself in a similar situation while using netbeans ide. I had a read of the following thread, that helped:

在使用 netbeans ide 时,我发现自己处于类似的情况。我阅读了以下线程,这有帮助:

http://forums.netbeans.org/ptopic28011.html

http://forums.netbeans.org/ptopic28011.html

Seems as though the swing application framework is saving the app's window size in a subfolder within your home dir (for Windows, the "Application Data" folder, for Linux, in your "~/" home folder).

似乎 Swing 应用程序框架将应用程序的窗口大小保存在您的主目录中的子文件夹中(对于 Windows,“应用程序数据”文件夹,对于 Linux,在您的“~/”主文件夹中)。

For example, for my application 'CrapApp', swing had saved some last-window-size info into the sub-folder "~/.CrapApp/", into a file called "mainFrame.session.xml".

例如,对于我的应用程序“CrapApp”,swing 已将一些最后一个窗口大小的信息保存到子文件夹“~/.CrapApp/”中,并保存到名为“mainFrame.session.xml”的文件中。

So no matter how I re-sized the window within the designer, upon running, it seemed to have ignored it and instead loaded the window size from the preferences within this sub-folder.

因此,无论我如何在设计器中重新调整窗口大小,在运行时,它似乎都忽略了它,而是从该子文件夹中的首选项加载了窗口大小。

So my solution was to delete this preserved-settings sub-folder, eg, in my case, "rm -rf ~/.CrapApp/"

所以我的解决方案是删除这个保留设置的子文件夹,例如,在我的例子中,“rm -rf ~/.CrapApp/”

Then the problem went away and I could re-size within the designer and run the app with this re-sized window now visible.

然后问题就消失了,我可以在设计器中重新调整大小并在这个重新调整大小的窗口现在可见的情况下运行应用程序。

This made me want to learn what triggered this issue. I noticed that simply running the app within the netbeans ide and closing it didn't generate the sub-folder.

这让我想了解是什么触发了这个问题。我注意到只是在 netbeans ide 中运行应用程序并关闭它并不会生成子文件夹。

After a bit of tinkering with my app, I noticed the following action triggered the generation of this "~/.CrapApp/" sub-folder.

在对我的应用程序进行一些修改后,我注意到以下操作触发了这个“~/.CrapApp/”子文件夹的生成。

  1. Going to my app's "Help >> About"
  2. Click the "Close" button in the about dialog that appears
  3. Exit the app
  1. 转到我的应用程序的“帮助 >> 关于”
  2. 在出现的关于对话框中单击“关闭”按钮
  3. 退出应用程序

And now the "~/.CrapApp/" sub-folder re-appears. This help/about dialog was auto-generated by netbeans ide, so I didn't really tinker with it, but this seems to have been the culprit in my case.

现在“~/.CrapApp/”子文件夹重新出现。这个帮助/关于对话框是由 netbeans ide 自动生成的,所以我并没有真正修改它,但这似乎是我的案例的罪魁祸首。

Perhaps it may be a bug in the netbeans ide, I'm using a somewhat old version (v6.8), which seems roughly from the era of your original post too.

也许这可能是 netbeans ide 中的一个错误,我使用的是一个有点旧的版本 (v6.8),它似乎也与您原始帖子的时代大致相同。