问题描述
我打算制作一个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也可以观察到此异常。没有使其不可调整大小,没有可观察的panel / contentPane,但是当使用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.
推荐答案
你是对的,设置一个不可靠的框架似乎增加了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#pack
重置它c $ c> JFrame #setResizable
You can reset it by call JFrame#pack
after the calling 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是可缩放(错误)大小调整问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!