I have this little code
public class Test extends JFrame {
public static void main(String[] args) {
    new Test();
}
Test() {
    getContentPane().add(new MyPanel());
    pack();
    setVisible(true);
}
private class MyPanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        setSize(640, 480);
        setPreferredSize(new Dimension(640, 480));
        setMinimumSize(new Dimension(640, 480));
        g.fillRect(20, 20, 50, 50);
    }
}
Unfortunately the frame is not resized to the size of the nested panel after calling pack(). I already read the related answers for this topic, but none of them helped. Could you provide me a solution, please?
 
     
     
     
    