I want to create a JFrame that's initially maximized, and has a defined size it shrinks to when the user presses the maximize button.
Minimal example:
package minimalfullscreenexample;
import java.awt.Dimension;
import static java.awt.Frame.MAXIMIZED_BOTH;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MinimalFullscreenExample {
public void displayFrame() {
JFrame frame = new JFrame("Minimal example");
frame.setExtendedState(MAXIMIZED_BOTH);
frame.setPreferredSize(new Dimension(500, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> { new MinimalFullscreenExample().displayFrame(); });
}
}
Expected behaviour: The window openes maximized. When the user clicks the maximize button, the window becomes un-maximized, and has a size of 500x300 pixels.
This works as it should with
This does NOT work with
where the window opens un-maximized.
When the setPreferredSize
call is removed, the window opens maximized, but shrinks to "nothing" on un-maximizing.
Is there any know bug or workaround for that? (Moving the setPreferredSize
above setExtendedState
doesn't help).
I've had the same problem, but i managed to solve it.
The problem is frame.pack().
In fact, it sets the size of the frame to its preferred size. I've also noticed the difference between Java 8 (or 9) and Java 12, I think it's a bug on the openjdk.
However, to fix this, you should call setExtendedState after calling pack(), like this:
package minimalfullscreenexample;
import static java.awt.Frame.MAXIMIZED_BOTH;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MinimalFullscreenExample {
public void displayFrame() {
JFrame frame = new JFrame("Minimal example");
frame.setPreferredSize(new Dimension(500, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setExtendedState(MAXIMIZED_BOTH); // after frame.pack()
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new MinimalFullscreenExample().displayFrame();
});
}
}
I think you are simply misunderstanding the effect of pack :
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents . The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.
Your use case is probably the explanation:
JFrame
may delegate to content pane, but the LayoutManager
may simply ignore the preferredSize
(for example, BorderLayout
ignore it for the CENTER
constraint), same apply for minimum (which is 0, 0 here) and maximum size (which is (Integer.MAX_VALUE, Integer.MAX_VALUE)
or (Short.MAX_VALUE, Short.MAX_VALUE)
). What may have changed between Java 11 and 12 is the native call to resize the window: it may simply disallow you from changing the size to (0,0) or negative values which may be considered an error for the Window Manager . Java may introduce another layer (eg: validate size) or not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.