How to move a component to a layout?

How to move a component in a frame when I use a layout? I tried this:

System.out.println(test1.getLocation()); int oy = test1.getY(); int ox = test1.getX(); oy++; ox++; test1.setLocation(ox, oy); validate(); test1.validate(); System.out.println(test1.getLocation()); 

The first place coincides with the last. I know that you cannot change the location in the layout, but how to do it? I asked a similar question before and did not receive an answer. For this, I searched all over the Internet, but I did not find the answer.

TL DR - How do you move a component?

+4
source share
3 answers

The problem is that the LayoutManager panel sets the label location for you.

What you need to do is set the layout to null by:

 setLayout(null); 

This will ensure that the frame / panel does not attempt to compose the components themselves.

Then call setBounds(Rectangle rect) on the shortcut. For instance:

lbl.setBounds(new Rectangle(new Point(200, 300), lbl.getPreferedSize())); This should place the component you want in.

However, unless you have a really big reason to LayoutManager components yourself, it is usually best to use the LayoutManager to work in your favor.

Here 's a great LayoutManager using LayoutManager s, if you should use an absolute though, then look at this tutorial

+3
source

can help you take a look at DragLayout and combine with ComponentMover made by @camickr

+3
source

The setLocation() method works when setting the layout to null . If you use the layout manager, one option would be to remove the component from its container and add it using the new layout restrictions.

+2
source

Source: https://habr.com/ru/post/1433063/


All Articles