Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The general is: if you have a layout problem, <strong>always</strong> solve it with an appropriate LayoutManager. <strong>Never</strong> tweak a component's sizing hint to reach your goal.</p> <p>In this case, it's particularly easy to adjust the CardLayout. By default, it calculates its prefSize to the max of prefSizes of all cards. Simply subclass and implement to return the prefSize (plus insets) of the currently visible card:</p> <pre><code>public static class MyCardLayout extends CardLayout { @Override public Dimension preferredLayoutSize(Container parent) { Component current = findCurrentComponent(parent); if (current != null) { Insets insets = parent.getInsets(); Dimension pref = current.getPreferredSize(); pref.width += insets.left + insets.right; pref.height += insets.top + insets.bottom; return pref; } return super.preferredLayoutSize(parent); } public Component findCurrentComponent(Container parent) { for (Component comp : parent.getComponents()) { if (comp.isVisible()) { return comp; } } return null; } } </code></pre> <p>Using that (borrowing @mKorbel's example), the main method cleanly shrinks down:</p> <pre><code>private static void createAndShowUI() { final CardLayout cardLayout = new MyCardLayout(); final JPanel cardHolder = new JPanel(cardLayout); final JFrame frame = new JFrame("MultiSizedPanels"); JLabel[] labels = { new JLabel("Small Label", SwingConstants.CENTER), new JLabel("Medium Label", SwingConstants.CENTER), new JLabel("Large Label", SwingConstants.CENTER)}; for (int i = 0; i &lt; labels.length; i++) { int padding = 50 * (i + 1); Border lineBorder = BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(Color.blue), BorderFactory.createEmptyBorder(padding, padding, padding, padding)); labels[i].setBorder(lineBorder); JPanel containerPanel = new JPanel(); containerPanel.add(labels[i]); cardHolder.add(containerPanel, String.valueOf(i)); } JButton nextButton = new JButton("Next"); nextButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cardLayout.next(cardHolder); frame.pack(); } }); JPanel btnHolder = new JPanel(); btnHolder.add(nextButton); frame.add(cardHolder, BorderLayout.CENTER); frame.add(btnHolder, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocation(150, 150); frame.setVisible(true); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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