Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the <a href="http://sscce.org/" rel="nofollow">SSCCE</a>.</p> <p>1) For continuous increase/decrease of size, you should look for some animation API.</p> <p>or</p> <p>2) If isn't there any background task with output to the GUI, you can to pause while sizing the <code>JFrame</code>. In that case, you would delay the increase/decrease by using <code>Thread#sleep(int)</code> wrapped into <code>Runnable</code> thread.</p> <p>3) Notice (not for OP) using <code>Thread#sleep(int)</code> directly during EDT would be freeze GUI until the delay ended.</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.Border; //based on HFOE http://stackoverflow.com/questions/5414177/change-size-of-jpanel-using-cardlayout/5414770#5414770 public class MultiSizedPanels { private static void createAndShowUI() { final CardLayout cardLayout = new CardLayout(); 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; Dimension size = labels[i].getPreferredSize(); size = new Dimension(size.width + 2 * (i + 1) * padding, size.height + 2 * (i + 1) * padding); labels[i].setPreferredSize(size); Border lineBorder = BorderFactory.createLineBorder(Color.blue); labels[i].setBorder(lineBorder); JPanel containerPanel = new JPanel(); if (i == 1) { containerPanel.setPreferredSize(new Dimension(300, 200)); containerPanel.add(labels[i], BorderLayout.CENTER); cardHolder.add(containerPanel, String.valueOf(i)); } else if (i == 2) { containerPanel.setPreferredSize(new Dimension(600, 400)); containerPanel.add(labels[i], BorderLayout.CENTER); cardHolder.add(containerPanel, String.valueOf(i)); } else { containerPanel.setPreferredSize(new Dimension(800, 600)); containerPanel.add(labels[i], BorderLayout.CENTER); cardHolder.add(containerPanel, String.valueOf(i)); } } JButton nextButton = new JButton("Next"); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cardLayout.next(cardHolder); Dimension dim = new Dimension(); for (Component comp : cardHolder.getComponents()) { if (comp.isVisible() == true) { dim = comp.getPreferredSize(); } } frame.setPreferredSize(dim); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { 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); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } </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. 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