Reputation: 23
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().
Upvotes: 1
Views: 816
Reputation: 1495
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
Upvotes: 4