Why I should use a JPanel?

Viewed 1737

What's the difference between:

public class Test {

    public static void main(String[] args) {
        JButton button= new JButton("1");
        button.setVisible(true);
        JPanel panel= new JPanel();
        panel.add(button);
        panel.setVisible(true);
        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setVisible(true);
        frame.pack();

    }
}

and

public class Test {

    public static void main(String[] args) {
        JButton button= new JButton("1");
        button.setVisible(true);
        JFrame frame = new JFrame();
        frame.add(button);
        frame.setVisible(true);
        frame.pack();

    }
}

I know that a JPanel is a container for GUI components but I really don't see the utility of use it. Certainly I'm very wrong but I'm starting with Swing, so... Why I should use a JPanel? What's really the purpose?

6 Answers
Related