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); }
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.
Hidde source share