3

How to set the JButton position on the JFrame?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
G Jay
  • 41
  • 1
  • 1
  • 2

3 Answers3

4

can any one tell me the property to set the button position on the Jframe.

answer is simple --->

use proper Layout Manager, in the case that you'll real question edit with description but SSCCE will be better

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Not a real answer to the question the OP is asking, although it is not asked nicely. – Hidde May 19 '12 at 11:00
  • @Hidde I don't think an absolute layout is a real answer to any need. Layouts, using appropriate layout padding, and occasional empty borders, is a much better way to logically layout a GUI that includes white-space. Try recreating something like the [Nested Layout Example](http://stackoverflow.com/a/5630271/418556) (including PLAF change) using an absolute layout (and a `ComponentListener` presumably). – Andrew Thompson May 20 '12 at 06:10
  • I agree that you almost never use an Absolute Layout, but I have found use for it once (a small math application with drawings in which buttons were placed. I had to make it unresizable...). Here however, the OP asks for a way to put his button in a NULL Layout, so that's the answer I give him, with further advice. – Hidde May 20 '12 at 08:23
2

If you have an Absolute Layout (which you must not, horrible resizing capabilities, and a bad habit), you can call on either .setBounds(int x, int y, int w, int h), or .setLocation(int x, int y).

Hidde
  • 11,493
  • 8
  • 43
  • 68
  • Whoops, it's `.setLocation (int x, int y)`. Again, you should use a LayoutManager, and not an Absolute Layout unless you really must. – Hidde May 19 '12 at 12:39
0

Try using a proper Layout Manager, my favorite is GridBagLayout because it's easy to use. You just need to make a JPanel and then create GridBagConstraints for each component in the JFrame. Here's the code (function inside the main class, which is an extension of JFrame):

public void GridBagLayoutExample() {
    JPanel pane = new JPanel(new GridBagLayout()) //create a Panel with a layout
    JButton b = new JButton('Button1');
    GridBagConstraints bconstraints = new GridBagConstraints();
    //more code here
}

For more information, look in the javadocs.

Gavriel Feria
  • 415
  • 6
  • 13