Search code examples
javaswingjframejbuttonborder-layout

How do I set the size of a JButton?


The button takes up the whole screen, and I try using setSize() but that doesn't appear to be doing anything. Here's my code so far:

JButton start = new JButton("PLAY");
start.setSize(new Dimension(100, 100));
myFrame.add(start);

Solution

  • By default, JFrame has BorderLayout with CENTER alignment. Thats why single component will takes full screen. So add a suitable Layout Manager to JFrame.

    For details, go through, How to Use Various Layout Managers.