Using your code, just add one line to change LayoutManagerinto JFrame. Then, when you add the component, it will save the preferred size.
Also, instead of calling, jp.setSize(40,40)call jp.setPreferredSize (new dimension (40, 40)).
pack() JFrame, .
JFrame frame = new JFrame("Testing");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1000, 2000));
frame.setBackground(Color.WHITE);
JPanel jp = new JPanel();
jp.setBackground(Color.BLUE);
jp.setPreferredSize(new Dimension(40, 40));
frame.add(jp);
frame.pack();
frame.setVisible(true);
, LayoutManagers, . - .