开发者

Swing Layout: Prevent my components from moving around

开发者 https://www.devze.com 2023-01-30 10:40 出处:网络
I\'m coming from a Opengl project and I need to be able t开发者_开发百科o place elements on my screen and that they stay there.

I'm coming from a Opengl project and I need to be able t开发者_开发百科o place elements on my screen and that they stay there.

I'm creating an applet that is the same size as the original opengl project (800*480) and I want to be able to place a button 150*50 at the coordinates 100,200.

everything works fine util an events triggers a setSize, then suddenly layout() is triggered and that make my button move to the center of the screen...

how can I work around that ?

I'm currently overriding the layout() of my JPanel but it doesn't feel right ^^


Read the Swing tutorial on Using Layout Managers. It is strongly recommended that you learn to use layout managers effectively and avoid trying to manually position components. Beginners don't understand how to use layout managers effectively and attempt to use manually placement.

However, the tutorial has a section on "Absolute Positioning" that will help you out. However in general this should only be used when you have a drag and drop feature.


By default a JPanel will use a FlowLayout. You'd be better off with something else, for example a BorderLayout, but there are a number to choose from. Change it with e.g.:

panel.setLayout(new BorderLayout());

You can set a border around your button with setBorder.

0

精彩评论

暂无评论...
验证码 换一张
取 消