(JButton) size of button in new window in JAVA GUI

73 Views Asked by At

I am using setBounds() method on my button to set the size and location of the button . After running, the size of the button is taken up all the frame. Here's my source code

public class Main {
    JFrame frame = new JFrame();
    JButton cal = new JButton();
    
    public Main() {
        
        cal.setText("Calculate");
        cal.setBounds(150, 50, 100, 100);
        cal.addActionListener(e -> System.out.println("Calculate"));
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.setTitle("Calculation Program");
        frame.setSize(1000,1000);
        frame.setVisible(true);
        frame.add(cal);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Main());
    }
}

And this is the result

I would like to know how can I possibly set the size to it actual?

3

There are 3 best solutions below

1
Raboro On

You need to add this line to your code:

frame.setLayout(null);

Then it works. You need to set the layout to null, because the default value when you init the JFrame is BorderLayout and this breaks you setBounds input.

0
FLUFFY_BIRD On

To make sure your button is in the correct position you need to set Layout Manager sutable for your situation. By default Swing uses FlowLayout wich will put your objects one after another. You can check MigLayout. This Layout is easy to use and it is very versatile.

0
Grinding For Reputation On

Let me point out a few mistakes in your code and question in general.

  1. Using setBounds(). It's just plain wrong. There is no point in having to be so specific every time you create a component, its unecessary, a waste of time and effort. There are so many Layout Managers that you are provided with just take advantage of that.
  2. However if you still want to use setBounds() because you are a menace to Swing you should remember you can't use setBounds() without setting layout to null with frame.setLayout(null). Trying to use the setBounds method without setting the null layout just won't work.
  3. Using images instead of copying the code here on StackOverflow. I had to manually copy this code line by line from an image wasting my time just so I could run it, please avoid using pictures of code as pointed out by Diego Borba

Instead of hard coding the position and sizing of your button (which is pointless as it will retain the same position from the top left corner of the JFrame) just use GridBagLayout because

  • It can keep the orientation no matter the size of the parent component
  • It is really customizable
  • It is also really easy to understand

Taking this into consideration let's see how we can implement this in your code!

public class Main {
    JFrame frame = new JFrame();
    JButton cal = new JButton();
    
    public Main() {
        
        cal.setText("Calculate");
        cal.addActionListener(e -> System.out.println("Calculate"));
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.setLayout(new GridBagLayout());
        frame.setTitle("Calculation Program");
        frame.setSize(1000,1000);
        frame.setVisible(true);
        frame.add(cal);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Main());
    }
}

Incase you want to change the size of your button use setPreferredSize() like so

...
cal.setText("Calculate");
cal.addActionListener(e -> System.out.println("Calculate"));
cal.setPreferredSize(new Dimension(200,500));
...