You can follow this Example, also take a look on setLocation()
method for Components
like JLabel
, also try to use Layouts
to manage better the position for each Component
.
This guy also work with location with JLabel
, you can see how he does the work and apply it in your project.
Example
JLabel label = new JLabel("Hi");
panel.add(label);
//This is to get the width and height
Dimension size = label.getPreferredSize();
//You can change 100(x) and 100(y) for your likes, so you can put that JLabel wherever you want
label.setBounds(100, 100, size.width, size.height);
You'll have to know what x
and what y
you want to put the JLabel
right there.
If you want to do that, just do what tong1
did, but showing the x
and y
to know where exactly you want it.
Create a Container
Container container= getContentPane();
Add the JLabel
on it
container.add(label);
Add an MouseListener()
to get the x
and y
container.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
//x and y are ints.
x = e.getX();
y = e.getY();
label.setBounds(x,y,150,50);
}
});