Swing: set JFrame content area size(Swing:设置JFrame内容区域大小)
问题描述
我正在尝试制作一个可用内容区域正好为 500x500 的 JFrame.如果我这样做......
I'm trying to make a JFrame with a usable content area of exactly 500x500. If I do this...
public MyFrame() {
super("Hello, world!");
setSize(500,500);
}
... 我得到一个全尺寸为 500x500 的窗口,包括标题栏等,我真的需要一个大小为 504x520 的窗口来说明窗口边框和标题栏.我怎样才能做到这一点?
... I get a window whose full size is 500x500, including the title bar, etc., where I really need a window whose size is something like 504x520 to account for the window border and titlebar. How can I achieve this?
推荐答案
你可以尝试几件事:1 - 一个黑客:
you may try couple of things: 1 - a hack:
public MyFrame(){
JFrame temp = new JFrame;
temp.pack();
Insets insets = temp.getInsets();
temp = null;
this.setSize(new Dimension(insets.left + insets.right + 500,
insets.top + insets.bottom + 500));
this.setVisible(true);
this.setResizable(false);
}
2- 或将 JPanel 添加到框架的内容窗格中,然后只需设置首选/最小尺寸将 JPanel 的大小改为 500X500,调用 pack()
2- or Add a JPanel to the frame's content pane and Just set the preferred/minimum size of the JPanel to 500X500, call pack()
- 2- 更便携
这篇关于Swing:设置JFrame内容区域大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swing:设置JFrame内容区域大小


基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01