Java, BorderLayout.CENTER, getting the width and height of a JPanel

I use Swing and AWT (for listeners) to create a small program. I have a problem getting the size of my JPanel (a class called Chess). My layout:

public class Main extends JFrame implements MouseListener, ActionListener{ Chess chessPanel = new Chess (); JButton newGameButton = new JButton ("New Game"); JButton loadGameButton = new JButton ("Load Game"); JButton saveGameButton = new JButton ("Save Game"); JButton exitButton = new JButton ("Exit"); public static void main (String [] args) { new Main(); } Main () { super ("Chess"); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); setSize(dim); setLocation(0,0); setUndecorated(true); chessPanel.addMouseListener(this); add(chessPanel, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); newGameButton.addActionListener(this); loadGameButton.addActionListener(this); saveGameButton.addActionListener(this); exitButton.addActionListener(this); buttonPanel.add(newGameButton); buttonPanel.add(loadGameButton); buttonPanel.add(saveGameButton); buttonPanel.add(exitButton); add(buttonPanel, BorderLayout.SOUTH); setVisible(true); } // ... Code ... } 

As you can see by the code, I have one JPanel in CENTER, which occupies almost the entire screen. At the bottom, I have another JPanel (SOUTH) that has a series of buttons.

I need the JPanel size in CENTER. When I call the getWidth (), getHeight (), or getBounds () methods inherited from JPanel, they all return 0 due to BorderLayout. Any idea how to get real values?

PS: the screen always occupies the entire screen and will never be resized if this helps.

+4
source share
1 answer

You probably call getWidth before the JPanel is displayed, and therefore it will be 0. The solution is to get the size after rendering, for example, after the pack () or setVisible (true) package has been called at the root The container that contains this JPanel.

In addition, I recommend that you do not call setSize () on anything, since most standard layout managers respect the preferred size of the component rather than size, and when you call package (), which tells layout managers to do something different, the set sizes are usually are ignored. You might want to make your JPanel, which is in the center, set your own size by overriding its setPreferredSize method if it should be a certain size. Then let the JFrame and its held containers set the size of the bid depending on their layout managers when you call the package.

eg.

 import java.awt.*; import javax.swing.*; public class Main extends JFrame { Chess chessPanel = new Chess(); JButton newGameButton = new JButton("New Game"); JButton loadGameButton = new JButton("Load Game"); JButton saveGameButton = new JButton("Save Game"); JButton exitButton = new JButton("Exit"); public static void main(String[] args) { new Main(); } Main() { super("Chess"); add(chessPanel, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(newGameButton); buttonPanel.add(loadGameButton); buttonPanel.add(saveGameButton); buttonPanel.add(exitButton); System.out.printf("chessPanel Size before rendering: %s%n", chessPanel.getSize()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(buttonPanel, BorderLayout.SOUTH); pack(); System.out.printf("chessPanel Size after rendering: %s%n", chessPanel.getSize()); setLocationRelativeTo(null); setVisible(true); } // ... Code ... } @SuppressWarnings("serial") class Chess extends JPanel { private static final int CHESS_WIDTH = 600; private static final int CHESS_HEIGHT = CHESS_WIDTH; private static final int MAX_ROW = 8; private static final int MAX_COL = 8; private static final Color LIGHT_COLOR = new Color(240, 190, 40); private static final Color DARK_COLOR = new Color(180, 50, 0); @Override public Dimension getPreferredSize() { return new Dimension(CHESS_WIDTH, CHESS_HEIGHT); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int panelWidth = getWidth(); int panelHeight = getHeight(); int sqrWidth = panelWidth / MAX_ROW; int sqrHeight = panelHeight / MAX_COL; for (int row = 0; row < MAX_ROW; row++) { for (int col = 0; col < MAX_COL; col++) { Color c = (row % 2 == col % 2) ? LIGHT_COLOR : DARK_COLOR; g.setColor(c); int x = (row * panelWidth) / MAX_ROW; int y = (col * panelHeight) / MAX_COL; g.fillRect(x, y, sqrWidth, sqrHeight); } } } } 
+7
source

All Articles