Title: How do I set the size of a JPanel?
Tags: java;swing;jpanel
Question: Most posts I've seen about this seem to be related to the user resizing it, however I want the program itself to resize it.
I'm trying to make a titled border in a program, and I only want it to be a certain size. However it seems to be the size of the whole window, which is not what I want. I tried setting the size and the preferred size, however it doesn't seem to be affecting anything.
Here's the code, by the way:
private JFrame Frame;
private JPanel Main;
private TitledBorder Border;
Frame = new JFrame("Window Title");
Frame.setSize(600,400);
Frame.setResizable(false);
Main = new JPanel();
Main.setSize(new Dimension(30,30)); // I also tried using ints to set the size
Border = BorderFactory.createTitledBorder("Border Title");
Main.setBorder(Border);
Frame.add(Main);
Frame.setVisible(true);
Comment: `"I tried setting the size and the preferred size, however it doesn't seem to be affecting anything."` -- meaning that you're doing something wrong in code not shown. Why make it hard to guess what and instead show your code, preferably as a [mcve] (please read the link)

Comment: You're forgetting that you're using layout managers and most respect preferred size (and you're not showing that attempt). Best to override `getPreferredSize()`, and then let the layout managers do their thing. [For example](http://stackoverflow.com/questions/25229505/how-to-set-the-size-of-jpanel-in-java). The example also shows a proper [mcve], something you've not yet posted, as it compiles and runs.

Comment: Also see [this link](http://stackoverflow.com/questions/10866762/use-of-overriding-getpreferredsize-instead-of-using-setpreferredsize-for-fix). Also, you're forgetting to `pack()` your JFrame before displaying it.

Comment: Variable names should NOT start with an upper case character. Note the Border is not considered when the preferred size of a panel is calculated. So you need to add components to the panel before a reasonable size can be calculated. If you want the TitledBorder to be included in the preferred size calculation then you can check out: http://stackoverflow.com/questions/36405080/why-is-my-titled-border-panel-so-small/36405695#36405695



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:09/23/16', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<java><swing><jpanel>', 'Title': 'How do I set the size of a JPanel?', 'ViewCount': 26, 'id': 39666571, 'user_AboutMe': None, 'user_AccountId': 7822304.0, 'user_DisplayName': 'fixylol', 'user_Reputation': 13.0, 'user_Views': 4.0, 'user_id': 5915084.0}}