Reputation: 33
Right now I have the following code which adds the JLabel to the top center of the Panel, which I assume is the default
imageLabel = new JLabel();
ImageIcon customer1 = new ImageIcon("src/view/images/crab.png");
imageLabel.setIcon(customer1);
storePanel.add(imageLabel);
imageLabel.setBounds(20, 20, 50, 50);
setBounds obviously isn't putting it at 20,20....so how do you position something to a point within a Panel?
Upvotes: 1
Views: 1483
Reputation: 22481
Although not recommended, you can do absolute positioning if you set your layout manager to null
.
storePanel.setLayout(null);
// imageLabel initialization code
storePanel.add(imageLabel);
imageLabel.setBounds(20, 20, 50, 50);
My advice is to use a Good IDE + UI Builder combo such as:
Thease are WYSIWYG tools that can generate Swing code using flexible Layout Managers such as Group Layout or JGoodies Form Layout.
A layout manager is a must if you want to design good UIs. They not only handle the size and positioning of components, but things such as redistributing / repositioning / resizing components on window resize (which is really hard to get right by hand). Also, those UI designers can hint you so that you stick to the guidelines and best practices in order to design high quality / cross-platform UI.
Upvotes: 1
Reputation: 3806
If you don't mind a bit of manual work you can add constraints to your label using a SpringLayout. This allows you to position edges an exact distance from other edges, which by default also sorts the components size (By basically setting the edges a set distance apart when you lay it out) I have demonstrated below with a textArea, but is could easily apply to your label as well.
public class SO {
public static void main(String[] args) {
//Components
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(frame.getSize());
JTextArea text = new JTextArea();
//Add components
panel.add(text);
frame.add(panel);
//Layout add & setup
SpringLayout layout = new SpringLayout();
panel.setLayout(layout);
layout.putConstraint(SpringLayout.WEST, text, 10, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, text, 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.EAST, text, -10, SpringLayout.EAST, panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 1
Reputation: 10994
Seems your storePanel
is JPanel
and has default FlowLayout
manager, because of your setBounds(20, 20, 50, 50);
doesn't work. It will be work with null layout (storePanel.setLayout(null);
).
But I recommend you to use LayoutManager
.
Upvotes: 2
Reputation: 23639
Use the appropriate LayoutManager to place components in the panel.
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
In your case you should be able to use a FlowLayout
and set the horizontal and vertical gaps when you create it.
http://docs.oracle.com/javase/7/docs/api/java/awt/FlowLayout.html#FlowLayout(int,%20int,%20int)
Upvotes: 2