how I can add space between JFrame and a JPanel inserts into this JFrame? I would insert space in the way that the elements inside the JPanel didn't appear too near at the JFrame
There are a number of ways in a Swing GUI to provide a separation between components, and white space around components: JToolBar has the methods addSeparator() & addSeparator(Dimension) . JMenu uses a spacing component better suited to menus, available through addSeparator() .
JToolBar(). add(component) − To add a component to the toolbar. JToolBar(). addSeparator(dimension) − To add required spacing between button on the toolbar.
Use vertical glue in a vertical BoxLayout and horizontal glue in a horizontal layout. For example, this will allow extra vertical space between two buttons. JPanel p = new JPanel(); p. setLayout(new BoxLayout(p, BoxLayout.
Set the JPanel's border with an EmptyBorder with appropriate parameters.
i.e.,
// caveat: code not tested
myPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
If the JPanel already has a border, then you can either use a compound border or wrap the JPanel in another JPanel, say using BorderLayout and in the BorderLayout.CENTER position, and give the wrapper JPanel an empty border.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With