JFrame isResizable(false) sizing issue(JFrame isResizable(false) 大小问题)
问题描述
我打算制作一个 ContentPanel 为 600x600 的 JFrame,并且我希望 JFrame 不能重新调整大小.在这个盒子里,我画了一个 600x600 的红色轮廓矩形,以确保在我运行程序时一切都匹配.在限制 JFrame 的大小调整之前,我通过以下方式设置 JFrame 的大小:
I intended to make a JFrame with a ContentPanel of 600x600 and I wanted the JFrame to be not re-sizable. Inside this box, I Drew a 600x600 red-outlined rectangle to make sure that everything matched when i ran the program. Before restricting resizing for the JFrame, I set the size of my JFrame by doing:
getContentPane().setPreferredSize( new Dimension(600,600));
pack();
当我启动程序时,我的矩形边界与 JFrame 的尺寸完美契合.但是,当我将 isResizable(false) 添加到等式中时,矩形的右边缘和矩形的底边缘之间似乎存在像素缓冲区.通过一些试验和错误,似乎 isResizable(false) 在高度和宽度上增加了 10 个像素.(因此 contentPane 的尺寸为 590x590 + 额外的 10 是我的解决方法)
And when I launched the program and the boundaries of my rectangle fit perfectly with the dimensions of the JFrame. However, when i added isResizable(false) into the equation, there seemed to be buffer of pixels between the right edge of my rectangle as well as the bottom edge of my rectangle. With a little trial and error, it seems as though isResizable(false) adds 10 pixels to height and width. (So having dimension of 590x590 for contentPane + the extra 10 is my workaround)
我的问题 为什么会这样?我似乎在任何地方都找不到关于额外 10 像素的任何文档?
My Questions why is this? I can't seem to find any documentation about the extra 10 pixels anywhere?
注意:在不修改大小的情况下制作 JFrame 也可以观察到这种异常情况.如果不使其不可调整大小,则没有可观察的面板/内容窗格,但是当使用 isResizable(false) 时,面板/窗格的可见部分.
Note: This anomaly was also also observed by making a JFrame without modifying the size. Without making it un-resizable, there is no observable panel/contentPane, but when isResizable(false) is used, there is visible part of the panel/pane.
推荐答案
你是对的,将一个框架设置为 un-resiable 似乎确实增加了 10 个像素的高度和宽度,至于为什么,我不能说,这似乎是更新本机对等体的副作用,但是......
You are right, setting a frame to un-resiable does seem to add 10 pixels to it's height and width, as to why, I can't say, this seems to be side effect of updating the native peer, however...
你可以在调用JFrame#setResizable
public class TestResizableFrame {
public static void main(String[] args) {
new TestResizableFrame();
}
public TestResizableFrame() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new FixedPane());
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class FixedPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = getSize();
String text = size.width + "x" + size.height;
FontMetrics fm = g.getFontMetrics();
int x = (getWidth()- fm.stringWidth(text)) / 2;
int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g.drawString(text, x, y);
g.setColor(Color.RED);
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
}
}
这篇关于JFrame isResizable(false) 大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JFrame isResizable(false) 大小问题
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01