Swing JButton - SetBounds

I am new to swing. I want to design a form with a few buttons in custom positions. I tried to use SetBounds to do the trick but it doesn't seem to work. If you know how to do this please help ....


  • [code]
    button.setLocation( x, y );
    button.setSize( w, h );


    button.setNormalBounds( x, y, w, h );
  • The method setBounds (...) works just fine. If it doesn't, then it might be caused by the fact that you have a LayoutManager active which makes the JFrame ignore what you said about the JButtons bounds.

    Have you used this?
    this.setLayout (null);

    An instance of JFrame has BorderLayout by default wheter you like it or not. My guess is that you got a button in the middle and that it covered the whole window, right?

    Try the following and switch on/off the marked line:

    import java.util.*;
    import java.awt.*;
    import javax.swing.*;

    public class GraphicsTest extends JFrame
    public GraphicsTest ()
    //this.getContentPane ().setLayout (null);

    JButton b = new JButton ("I'm a JButton");
    b.setBounds (new Rectangle (30, 50, 80, 100));

    this.getContentPane ().add (b);

    this.setSize (new Dimension (400, 250));
    this.setVisible (true);

    public static void main (String[] arg)
    new GraphicsTest ();

    By the way - why did you decide not to use a LayoutManager at all?

    Happy coding!

    Kind Regards
    (+46/0) 708-70 73 92
    [email protected]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!