Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I would like to place the background image and cards such that they're always centered vertically and horizontally, even upon resizing the JFrame.</p> </blockquote> <p>Then you need to use layout managers on your panels. The layout manager is responsible for redoing the layout.</p> <blockquote> <p>How to add two JPanels to a JFrame in the center?</p> </blockquote> <p>You could try using the OverlayLayout for this. I think the basic code would be:</p> <pre><code>JPanel contentPane = new JPanel( new GrigBagLayo9ut() ); frame.add(contentPane, BorderLayout.CENTER); JPanel overlay = new JPanel() overlay.setLayout( new OverlayLayout(overlay) ); contentPane.add(overlay, new GridBagConstraints()); // this should center the overlay panel overlay.add(yourCardPanel); // you care panel must use a suitable layout overlay.add(new JLabel() ); // use a JLabel for the background not a custom panel </code></pre> <blockquote> <p>I also need to add another JPanel to the JFrame in the South border,</p> </blockquote> <p>The default layout manager for a JFrame's content pane is a BorderLayout. We already added the game panel to the center, so know you just add your other panel to the SOUTH.</p> <p>If the OverlayLayout doesn't work the way you want then you will need to nest panels. Something like:</p> <pre><code>JPanel center = new JPanel( new GridBagLayout() ); frame.add(center, BorderLayout.CENTER); JLabel background = new JLabel(...); background.setLayoutManager( new GridBagLayout() ); center.add(background, new GridBagConstraints()); background.add(yourCardPanel, new GridBagConstraints()); </code></pre> <p>Edit:</p> <p>Using nested panels:</p> <pre><code>import java.awt.*; import javax.swing.*; public class GridBagLayoutCenter extends JPanel { public GridBagLayoutCenter() { setLayout( new BorderLayout() ); JLabel background = new JLabel( new ImageIcon("mong.jpg") ); background.setLayout( new GridBagLayout() ); add(background, BorderLayout.CENTER); JPanel tiles = new JPanel(); tiles.setPreferredSize( new Dimension(200, 200) ); tiles.setBackground( Color.RED ); background.add(tiles, new GridBagConstraints()); add(new JLabel("SOUTH"), BorderLayout.SOUTH); } private static void createAndShowUI() { JFrame frame = new JFrame("GridBagLayoutCenter"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( new GridBagLayoutCenter() ); frame.setSize(400, 400); frame.setLocationByPlatform( true ); frame.setVisible( true ); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } </code></pre> <p>The preferred size of the "tiles" panel should not be hardcoded. The size should be determined by your custom layout manager based on the tiles that you add to the panel. The size should not change as tiles are removed.</p>
    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. 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