0

Why isn't my minimap panel at the right of the JFrame?

My code seems to be right but I must be making a stupid mistake. Please help me out :)

    private static void loadResources() {
    minimap.setMaximumSize(new Dimension(minimapWidth, minimapHeight));
    minimap.setMinimumSize(new Dimension(minimapWidth, minimapHeight));
    minimap.setPreferredSize(new Dimension(minimapWidth, minimapHeight));

    panel.setMaximumSize(new Dimension(width, height));
    panel.setMinimumSize(new Dimension(width, height));
    panel.setPreferredSize(new Dimension(width, height));

    minimap.setBackground(Color.red);
    panel.setBackground(Color.GRAY);

    panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    panel.add(minimap);
    minimap.setLayout(new FlowLayout(FlowLayout.RIGHT));

    Character.createCharacter();
    frame.add(panel);
}

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nic
  • 98
  • 2
  • 11
  • 1
    learn all about LayoutManagers, best starting with the online tutorial referenced in the swing tag wiki here. BTW: [don't use setXXSize, ever](http://stackoverflow.com/a/7229519/203657) http://stackoverflow.com/a/7229519/203657 – kleopatra Oct 18 '13 at 15:38
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Provide ASCII art of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Oct 18 '13 at 15:54

1 Answers1

0

You set the panel layout to be CENTER. Then you add the minimap, so the minimap is centered too.

By calling

minimap.setLayout(new FlowLayout(FlowLayout.RIGHT));

You just say that all elements inside the minimap (nothing in this case) should be positioned on the right relative to the minimap rectangle. So if you add an element, let's say a grey rectangle, to the minimap you should see the red rectangle in the center of the JFrame and the gray rectangle on the right inside the red rectangle.

You may try

panel.setLayout(new FlowLayout(FlowLayout.RIGHT));

But this may change other element position.

chiarfe
  • 522
  • 3
  • 15