Results 1 to 2 of 2
  1. #1
    bpx95 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default JButton Size Help?

    I'm trying to set the size of a JButton on a JFrame. It ends up being the same size as the frame no matter what. How can I do this? Or do I have to make a JPanel in order to do this?

    Java Code:
    import java.awt.Dimension;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    
    public class Test{
    
    	public Test(){
    		JButton reset = new JButton();
    		reset.setPreferredSize(new Dimension(25, 25));
    		reset.setText("test");
    		
    		JFrame wind = new JFrame();
    		wind.setTitle("title");
    		wind.setSize(500,500);
    		wind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		wind.setLocationRelativeTo(null);
    		wind.setResizable(false);
    		
    		wind.add(reset);
    		
    		wind.setVisible(true);
    	}
    	
    	public static void main(String[] args){
    		new Test();
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm trying to set the size of a JButton on a JFrame

    You should note that this is not what usually happens in Swing. Containers have a layout manager and this layout manager figures out the size and position of the components in the container. The layout manager uses the components preferred (and min and max) sizes, and also it has some general idea about how to lay things out (a GridLayout lays things out in a grid etc).

    Laying out a container (detemining the size and location of the included components) is covered in Oracle's Tutorial in the section Laying Out Components Within a Container.

    The approach usually followed allows for very flexible and fluid layout. By fluid I mean that layout changes to match the size that the user chooses for the window.

    ----------------

    In your code you add the button to the frame. What actually happens is that the button is added to the frame's content pane: which, by default will have a BorderLayout. The button gets added to what the border layout considers the "centre" of the content pane. As you have found, in the absence of any other components, the centre will be sized to take up all available room.

    This behaviour of frames is described in Using Top-Level Containers. The BorderLayout behaviour is described at the previous link.

    -------------

    I would highly reccommend you have a read of Oracle's Swing Tutorial (or at least those bits relating to starting a Swing application and laying out components within a container) as it is best to do things right from the start.

    If you must set the button size and position "by hand", then you can forgo using a layout manager. This is discussed in the first link.

Similar Threads

  1. jbutton
    By Patea2000 in forum Advanced Java
    Replies: 1
    Last Post: 03-16-2011, 08:59 AM
  2. JButton Size
    By jboy in forum New To Java
    Replies: 7
    Last Post: 10-14-2009, 04:20 PM
  3. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 04:37 PM
  4. Jbutton size
    By mrvigneshmca in forum AWT / Swing
    Replies: 2
    Last Post: 03-18-2009, 03:51 PM
  5. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •