Swing JButton - SetBounds - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Swing JButton - SetBounds

pvfpvf Posts: 6Member
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 ....

Comments

  • IllcoIllco Posts: 382Member
    [code]
    button.setLocation( x, y );
    button.setSize( w, h );
    [/code]

    OR

    [code]
    button.setNormalBounds( x, y, w, h );
    [/code]
  • chamsterchamster Posts: 662Member
    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?
    [code]
    this.setLayout (null);
    [/code]

    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:

    [code]
    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 ();
    }
    }
    [/code]

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

    Happy coding!


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    [email protected]
    http://konrads.webbsida.com

Sign In or Register to comment.