The essence of the problem lies in the fact that when the window is resized by dragging its borders, the internal components change their size without any errors, adjusting to the new window size. However, expanding to full-screen mode, or when “sticking” to the edge of the screen (Modern OS function), the scale of the components remains the same as during the last resizing in the usual way. And returning the window to its original size expands the components for full-screen mode.
Main class:
public class Window extends JFrame implements ComponentListener { private static final long serialVersionUID = 1L; WorldCamera screenArea; SidePanel sideBarArea; Window(int width, int height) { addComponentListener(this); setSize(width, height); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); setResizable(true); setVisible(true); this.loadScreen(width, height); } void loadScreen(int width, int height) { screenArea = new GameWorldCamera(); sideBarArea = new SidePanel(); screenArea.setPreferredSize(new Dimension((width / 6 * 5) - 20, height - 50)); sideBarArea.setPreferredSize(new Dimension((width / 6) - 10, height - 50)); screenArea.setBorder(BorderFactory.createLineBorder(Color.BLACK)); sideBarArea.setBorder(BorderFactory.createLineBorder(Color.BLACK)); this.add(screenArea); this.add(sideBarArea); System.out.println("Now width: " + width + ", height: " + height); } void unloadScreen() { try { this.remove(ScreenArea); this.remove(sideBarArea); } catch(NullPointerException Ex) { System.out.println(Ex); } } public void componentResized(ComponentEvent arg0) { System.out.println("Now x:" + this.getWidth() + " y:" + this.getHeight()); try { screenArea.setPreferredSize(new Dimension((this.getWidth() / 6 * 5) - 20, this.getHeight() - 50)); screenArea.printSize(); sideBarArea.setPreferredSize(new Dimension((this.getWidth() / 6) - 10, this.getHeight() - 50)); sideBarArea.printSize(); } catch(NullPointerException Ex) { } } } Components:
class WorldCamera extends JPanel { WorldCamera() { System.out.println("camera created"); } public void printSize() { System.out.println("Camera x:" + this.getWidth() + " y:" + this.getHeight()); } } class SidePanel extends JPanel { SidePanel() { System.out.println("side panel created"); } public void printSize() { System.out.println("Side panel x:" + this.getWidth() + " y:" + this.getHeight()); } }