简体   繁体   English

BorderLayout.CENTER不能使我的JPanel居中

[英]BorderLayout.CENTER doesn't center my JPanel

I have the following code to create my GUI. 我有以下代码来创建我的GUI。

private static void createGUI() {
  JFrame frame = new JFrame ("Tiles Game");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

  frame.setJMenuBar (new JMenuBar());
  frame.setContentPane (MainPanel.getInstance());

  frame.pack();
  frame.setResizable (false);
  frame.setLocationRelativeTo (null);
  frame.setVisible (true);
}

This is the MainPanel (extends JPanel) constructor: 这是MainPanel(扩展JPanel)构造函数:

private MainPanel() {
  super (new BorderLayout());
  setPreferredSize (new Dimension (IMG_SIZE + 10, IMG_SIZE + 10));
  ...
  panel = new ImagePanel();
  add (panel, BorderLayout.CENTER);
}

And this is the ImagePanel (extends JPanel) constructor: 这是ImagePanel(扩展了JPanel)构造函数:

private ImagePanel() {
  super();
  setPreferredSize (new Dimension (IMG_SIZE, IMG_SIZE));
  ...
}

However the ImagePanel is aligned to the top left corner of the MainPanel rather than centered, so I get a bunch of extra padding at the bottom and right sides, and none at the top and left sides. 但是,ImagePanel与MainPanel的左上角对齐,而不是居中对齐,因此我在底部和右侧都得到了一堆额外的填充,在顶部和左侧都没有填充。 How do I place it at the center of the MainPanel? 如何将其放置在MainPanel的中央?

Don't use a JPanel to display an image. 不要使用JPanel显示图像。

Instead use a JLabel with an ImageIcon . 而是将JLabelImageIcon一起使用。

If you want extra space around the image then you use an EmptyBorder on the label. 如果要在图像周围留出更多空间,请在标签上使用EmptyBorder

Probably what's happening is that you are drawing your image from (0, 0) which are the top-left corner. 可能正在发生的事情是您从左上角的(0, 0)绘制图像。 Then you set the preferred size to 10 pixels larger which makes the panel larger, but the image is still at (0, 0) 然后,将首选大小设置为大10像素,这会使面板更大,但是图像仍为(0, 0)

Instead use the same size witoout adding 10, and just use an EmptyBorder for the panel. 而是使用相同大小的witoout加10,然后对面板使用EmptyBorder Also as a recommendation, override getPreferredSize() instead of using setPreferredSize() 另外建议,重写getPreferredSize()而不是使用setPreferredSize()

public class ImagePanel extends JPanel {
    public ImagePanel() {
        setBorder(new EmptyBorder(10, 10, 10, 10));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(IMG_SIZE, IMG_SIZE);
    }
}

Also you may want to consider using a GridBagLayout for the container panel, for a sure center, if the container is to be larger than the the child panel. 另外,如果容器要大于子面板,则可能要考虑对容器面板使用GridBagLayout以确保居中。 Few things you could do. 您可以做的几件事。 Even consider using a ImageIcon and JLabel instead of painting (if the image doesn't need to be resized (as Camickr(+1) pointed out). A JLabel could easily be made a background by just setting the layout of the label and set it as the content pane of the frame. 甚至考虑使用ImageIconJLabel代替绘画(如果不需要调整图像大小(如Camickr(+1)所指出的那样)。只需设置标签的布局和设置即可轻松将JLabel设置为背景)作为框架的内容窗格。

ImageIcon icon = new ImageIcon(...)
JLabel frameBackground = new JLabel(icon);
frameBackground.setLayout(new BorderLayout());
frame.setContentPane(frameBackground);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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