Results 1 to 2 of 2
Thread: JButton Size Help?
- 05-14-2011, 06:35 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
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(); } }
- 05-14-2011, 07:02 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
jbutton
By Patea2000 in forum Advanced JavaReplies: 1Last Post: 03-16-2011, 08:59 AM -
JButton Size
By jboy in forum New To JavaReplies: 7Last Post: 10-14-2009, 04:20 PM -
Setting frame size to the size of an image
By Yoruichi in forum AWT / SwingReplies: 5Last Post: 04-22-2009, 04:37 PM -
Jbutton size
By mrvigneshmca in forum AWT / SwingReplies: 2Last Post: 03-18-2009, 03:51 PM -
Help with JButton
By geoffreybarwise in forum New To JavaReplies: 4Last Post: 05-21-2008, 10:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks