How to center JPanel with dynamic size

回眸只為那壹抹淺笑 提交于 2019-12-24 11:35:16

问题


I need to display chessboard. I have a BoardPanel class which extends JPanel and a GamePanel (also extending JPanel) class containing BoardPanel. GamePanel fills all the application frame.

I want BoardPanel to always be a square with size equal to the minimum of GamePanel's width and height (if GamePanel's width is greater than height there should be empty space on the left and right, if it's smaller there should be empty space on top and bottom). It's also important that BoardPanel should be displayed in the center of parent panel.

I wrote sth like this:

public GamePanel() {
    setLayout(new BorderLayout(0, 0));
    boardPanel = new BoardPanel(...);
    this.add(boardPanel, BorderLayout.CENTER);
    ...
}

and in BoardPanel:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int size = Math.min(this.getParent().getHeight(), this.getParent().getWidth());
    this.setSize(size, size);
    ...
}

It resizes well, but chessboard is always displayed in top left corner of GamePanel (all the empty space is displayed on bot or right) and I don't know how to fix it.

Any help? Thanks in advance!


回答1:


Center it using a GridBagLayout.

import java.awt.*;
import javax.swing.*;

public class CenteredPanel {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JPanel gui = new JPanel(new GridBagLayout());
                JPanel square = new SquarePanel();
                square.setBackground(Color.RED);
                gui.add(square);

                JFrame f = new JFrame("SquareBoard");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);
                f.add(gui);
                f.setMinimumSize(new Dimension(400,100));
                f.pack();
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class SquarePanel extends JPanel {
    @Override
    public Dimension getPreferredSize() {
        Container c = this.getParent();
        int size = Math.min(c.getHeight(), c.getWidth());
        Dimension d = new Dimension(size,size);
        return d;
    }
}



回答2:


  • No need for new BorderLayout(0,0) simply use default constructor for BorderLayout

  • Dont call setSize() rather override getPreferredSize() of JPanel like so:

    @Override
    public void getPreferredSize() {
    
            int size = Math.min(this.getParent().getHeight(), this.getParent().getWidth());
    
           return new Dimension(size,size);
    }
    
  • also its never good to do work in your paintComponent as this should be used exclusively for painting only.

If the above does not work I'd suggest a SSCCE to illustrate specific problems you might have



来源:https://stackoverflow.com/questions/13715921/how-to-center-jpanel-with-dynamic-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!