1

I've created a jFrame form in a package in netbeans. The project is without a main class. I've placed a button from the palette. The following is the code for the button:

 int x = jButton1.getHorizontalAlignment();
 int y = jButton1.getVerticalAlignment();
     JButton button2=new JButton();              
     button2.setBounds(200, 200, 100, 100);    
     button2.setVisible(true);

The second button will not show. Why? the x and y are to be used later for relative positioning. I would also like to know how to do that besides x+something and y+something in the coordinate parameters of the .setBounds().

7
  • 3
    1) Do you need to add the button to the JFrame's content pane or a JPanel? 2) Don't use setBounds() unless you are just messing around. Instead, position your widgets with layout managers, and let them set the exact size and position. Commented Jun 30, 2013 at 11:14
  • @7stud I'm trying to add the button to the object pane. How do I use layout managers to create objects at relative positions? Commented Jun 30, 2013 at 11:25
  • 1
    I agree with @7stud, that you should learn and use layout managers, but you may find it easier starting with some of the easier to use layouts such as BorderLayout, BoxLayout, GridLayout, rather than starting with GridBagLayout since the latter can be a bit complex. Remember that you can nest JPanels, each using its own layout and thereby use simple layout managers to create complex layouts of components. Commented Jun 30, 2013 at 11:41
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space. Commented Jun 30, 2013 at 12:00
  • 1
    For best help on laying out components, show ASCII art of how the GUI should look at smallest size and (if resizable) how it looks after extra width and height is added. Commented Jun 30, 2013 at 12:12

1 Answer 1

4

The second button will not show. Why?

Because, You have not added the button to JPanel .

I would also like to know how to do that besides x+something and y+something in the coordinate parameters of the .setBounds().

For setBounds to work you need to set the layout of the container to be null which is very very bad practice. Because , it diminishes the portability of application across the platform and also it is very had to maintain the code with setBounds. You should let the swing inbuilt layouts to do its work . Have a look at here : A Visual Guide to Layout Managers

Sign up to request clarification or add additional context in comments.

4 Comments

I haven't added a JPanel. I still tried using the JPanel.add() funcion. It asks me to Assign a new value to the return variabl. Clicking that gives me Component add = JPanel.add(button2); Which still gives and error.
@DeeparthGupta: your questions suggest that you're trying to program Swing without first going through a tutorial or two. Please stop guessing and read the tutorials, else you'll be in for a very frustrating experience.
@Hovercraft Full Of Eels: I think I should because the stuff they teach at school doesn't cover this. Any good tutorials to suggest?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.