DannyD
DannyD

Reputation: 2881

Having trouble re-sizing JButton in Java

Whenever I add my jbutton to my container it's really huge. I thought using the label.setBounds() function would work but it didn't

public Liability_Calculator(String s)
{
    super(s);
    setSize(325,200);
    Color customColor = Color.WHITE;

    c = getContentPane();
    c.setLayout(new BorderLayout());


    //the button
    ok = new JButton("OK");

    //ok.setSize(50, 50);

    //HERE IS WHERE I TRY AND RESIZE!
    ok.setBounds(30,30,50,50);

    c.add(ok, BorderLayout.SOUTH);

    setVisible(true);
} 

Upvotes: 2

Views: 101

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Suggestions:

  • You will want to read up on the layout managers to
  • understand why your GUI is behaving this way
  • and to see how to use the layout managers to your advantage to create better looking GUI's in an easy way.
  • You'll also want to avoid setting bounds on any gui components.

For instance, a JPanel uses FlowLayout(FlowLayout.CENTER)) by default, and you can use that to your advantage by placing your ok JButton into a JPanel and then the JPanel into the contentPane:

  ok = new JButton("OK");
  // ok.setBounds(30, 30, 50, 50);

  JPanel southPanel = new JPanel();
  southPanel.add(ok);

  c.add(southPanel, BorderLayout.SOUTH);

This will change the first image to the second:

enter image description here enter image description here

Upvotes: 2

Related Questions