Can I set the size of individual panels in CardLayout?

When I create my GUI, I use cardlayout to store different panels, as I am sure many people know. This sets my screen to the width and height of the largest panel. This causes problems with the aesthetics of my first screens, which are much smaller than SudokuPanel and CalkuroPanel .

I tried to set my preferred size when I switch to large screens, but to no avail.

Any help with links to good information or anything that would just be generally useful would be greatly appreciated :).

Please find my main class (where I draw the GUI) below:

 import java.awt.*; import javax.swing.*; import java.util.*; public class puzzleGUI{ private static JPanel screens; public static String username; static public void main (String[] args){ JFrame puzzle = new JFrame ("Sudoku/Calkuro Pro"); ... PuzzleStartPanel startPanel = new PuzzleStartPanel(); PuzzleChoosePanel choosePanel = new PuzzleChoosePanel(); PuzzleSudokuPanel sudokuPanel = new PuzzleSudokuPanel(); PuzzleCalkuroPanel calkuroPanel = new PuzzleCalkuroPanel(); screens = new JPanel(new CardLayout()); screens.add(startPanel, "start"); screens.add(choosePanel, "choosePuzzle"); screens.add(sudokuPanel, "sudoku"); screens.add(calkuroPanel, "calkuro"); screens.setPreferredSize (new Dimension(250, 80)); puzzle.setJMenuBar(menuBar); puzzle.getContentPane().add(screens); puzzle.pack(); puzzle.setVisible(true); puzzle.setResizable(false); puzzle.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); } static public void getUsername(String str){ username = str; } static public void openWindow(String newFrame){ CardLayout cl = (CardLayout)(screens.getLayout()); cl.show(screens, newFrame); } } 

Edit

brainblast tried to pack the frame after resetting the preferred size when calling openWindow and wolah, new frame size: D

 static public void openWindow(String newFrame, int a, int b){ CardLayout cl = (CardLayout)(screens.getLayout()); cl.show(screens, newFrame); screens.setPreferredSize (new Dimension(a, b)); puzzle.pack(); } 
+4
source share
2 answers

Is it possible to set the size of individual panels in the cardlayout file

Sure. But the layout will ignore them and make each card the same size. The best you can hope for is to add small panels to another panel (with layout), which allows you to reduce the content.


This answer shows this technique using a single label. Change the shortcut to "small panels" and using the layouts on the right, it will be centered.

QsSEU.png

+4
source

I had a similar problem and decided to trick it a bit ("dirty code"). I had two panels: SmallOne and HugeOne. I set the visibility of HugeOne to false. In this case, SmallOne sets the size of the entire CardPanel. You can make a method that is called when the user selects the HugeOne panel, and in this method you set the visibility of the HugeOne to true. And CardPanel will resize.

It works like a charm :)

0
source

Source: https://habr.com/ru/post/1440934/


All Articles