简体   繁体   中英

How do I move a JPanel from the East side of an application to the West?

There is an open source client for a game I play but some of the UI elements are not my favorite so I've looking through the code trying to make changes. I've been able to resolve most issues I've run into but I'm stumped with this one. My goal is to change the location of the sidebar from the right side of the game window to the left.

Here is an image for reference:

The red panel on the right is the current configuration and the green panel on the left is where I would like it moved to.

After some digging I found code that controls this panel's layout:

        container = new JPanel();
        container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
        container.add(new ClientPanel(client));

I've tried changing the end of the second line "BoxLayout.X_AXIS" to Y_AXIS, LINE_AXIS, and PAGE_AXIS but none of these had the desired outcome.

My main question is this. Is there a way to change the middle line to keep the X Axis alignment but flip it to the West side of the screen?

I'm sure there is something obvious I'm missing but I haven't found a solution after multiple hours of research. I can provide more code if needed and I'll try to answer any questions to clarify what I'm asking if needed.

Using Container.add(Component) adds the component at the end. If you wish to add it at any other position than the end, use Container.add(Component, int) , where the integer is the (zero-indexed) index you wish to add the component at.

Adding all components at the beginning instead of the end effectively reverses their order, which should result in your requested behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM