Java (swing): Disarm, resizing a JFrame by dragging the bottom left corner

http://imageshack.us/a/img845/796/15881822.jpg

Black is the content of the JFrame and red is the JPanel. This happens when the JFrame is resized. A little smaller when you resize the frame in the upper left corner, but still happens. The panel cannot keep up with the change in frame.

Why is he “correcting”?

thank

+3
source share
1 answer

The following lines provide the desired behavior on a JFrame:

public class MyFrame extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MyFrame().setVisible(true);
            }
        });
    }

    public MyFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setBackground(Color.black);

        JPanel panel = new JPanel();
        panel.setBackground(Color.red);

        getContentPane().add(panel);

        pack();
    }
}
+1
source

All Articles