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();
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)
My Questions why is this? I can't seem to find any documentation about the extra 10 pixels anywhere?
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.
You can change the size of the JFrame by simply placing the cursor in the corners and dragging it. Or if you press the resize option next to close(X) in the upper right corner, it will be enlarged to full-screen size. This happens because the resize is set to “true” by default.
Making a Frame Non-Resizable: use setResizable(false) to freeze a frame's size. : JFrame Window « Swing « Java Tutorial. 14.80.
getScreenSize(); // get 2/3 of the height, and 2/3 of the width int height = screenSize. height * 2 / 3; int width = screenSize. width * 2 / 3; // set the jframe height and width jframe. setPreferredSize(new Dimension(width, height));
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...
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With