Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to use a combination of <code>getPreferred/Minimum/MaximumSize</code> and a layout that will actually respect it.</p> <p>Here, I've used a <code>GridBagLayout</code> set up so that the board will always stay the same size, but the cell will use the maximum amount of same.</p> <p>If you want to keep the text at the bottom of the board (instead of the frame), remove the <code>weighty</code> constraint (and possibly the <code>weightx</code>) as well</p> <p><img src="https://i.stack.imgur.com/N2roi.png" alt="enter image description here"><img src="https://i.stack.imgur.com/4JmiM.png" alt="enter image description here"></p> <pre><code>public class TestBoardGame { public static void main(String[] args) { new TestBoardGame(); } public TestBoardGame() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; gbc.weighty = 1; frame.add(new BoardGamePane(), gbc); gbc.gridx = 0; gbc.gridy = 1; gbc.weightx = 1; gbc.fill= GridBagConstraints.HORIZONTAL; JLabel text = new JLabel("Area for text"); text.setHorizontalAlignment(JLabel.CENTER); frame.add(text, gbc); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class BoardGamePane extends JPanel { public BoardGamePane() { setBorder(new LineBorder(Color.RED)); setBackground(Color.ORANGE); } @Override public Dimension getPreferredSize() { return new Dimension(400, 300); } @Override public Dimension getMinimumSize() { return getPreferredSize(); } @Override public Dimension getMaximumSize() { return getPreferredSize(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload