Set the JPanel size to fill parent with little margin

后端 未结 4 837
遇见更好的自我
遇见更好的自我 2021-01-07 10:15

I have two Jpanel (JpanelLeft and JpanelLeftContent) how can i make the JpanelLeftContent fill parent size with a little margin on the lef

4条回答
  •  无人及你
    2021-01-07 10:34

    Do try this code example :

    import java.awt.*;
    import javax.swing.*;
    
    public class InsetTesting extends JFrame
    {
        private void createAndDisplayGUI()
        {
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setLocationByPlatform(true);
    
            JPanel contentPane = new JPanel();
            contentPane.setOpaque(true);
            contentPane.setBorder(BorderFactory.createLineBorder(
                                        Color.DARK_GRAY.darker(), 5, true));
            contentPane.setBackground(Color.WHITE);
    
            add(contentPane, BorderLayout.CENTER);
            pack();
            setVisible(true);
        }
    
        public Insets getInsets()
        {
            return (new Insets(30, 20, 10, 20));
        }
    
        public Dimension getPreferredSize()
        {
            return (new Dimension(200, 400));
        }
    
        public static void main(String\u005B\u005D args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new InsetTesting().createAndDisplayGUI();
                }
            });
        }
    }
    

    Here is the output of the same :

    INSETS

提交回复
热议问题