GridBagLayout cant get layout right

后端 未结 1 1142
深忆病人
深忆病人 2020-12-11 23:47

I\'m new to GridBagLayout but I tried to use the standard convention that I could find which was to draw out my idea on a piece of grid paper and then try and translate the

相关标签:
1条回答
  • 2020-12-12 00:19

    Well you can easily accomplish this by using a GridBagLayout.

    Considering contentPane with GridBagLayout as its layout, you can divide the JPanel into three parts now. A single left side JPanel (with GridBagLayout, and which will contain those RED, BLACK and BLUE JPanels), and the right side consisting of two JPanels, i.e. GREEN and MAGENTA.

     ------------------------
    |  left   | green JPanel |
    | JPanel  |______________|
    |  with   |magenta Jpanel|
    |3 JPanel |              |
    |________________________|
    

    Now the left JPanel will place RED, BLACK and BLUE JPanel on itself, with GridBagLayout as its Layout Manager with RED and BLUE having weighty = 0.1, and BLACK having weighty = 0.8

    See this example code :

    import java.awt.*;
    import javax.swing.*;
    
    public class GridBagExample {
        private JPanel leftPanel;
        private JPanel redPanel;
        private JPanel blackPanel;
        private JPanel bluePanel;
        private JPanel greenPanel;
        private JPanel magentaPanel;
    
        private GridBagConstraints gbc;
    
        public GridBagExample() {
            gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        }
    
        private void displayGUI() {
            JFrame frame = new JFrame("GridBagLayout Example");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            JPanel contentPane = new JPanel(new GridBagLayout());
    
            leftPanel = getPanel(Color.WHITE);
            leftPanel.setLayout(new GridBagLayout());
            redPanel = getPanel(Color.RED.darker());
            blackPanel = getPanel(Color.BLACK);
            bluePanel = getPanel(Color.CYAN.darker().darker());
            greenPanel = getPanel(Color.GREEN.darker().darker());
            magentaPanel = getPanel(Color.MAGENTA);
            /**
              * @param : 
              * leftPanel : JPanel (with GridBagLayout), on which
              *               all other components will be placed.
              * redPanel : JPanel, which will be added to the leftPanel
              * 0 : specifies the grid X, which in this case is 0
              * 0 : specifies the grid Y, which in this case is 0
              * 1 : specifies the width for this grid (cell), we keeping 
              *     this default as 1
              * 1 : specifies the height for this grid (cell), we keeping
              *     this default as 1
              * GridBagConstraints.BOTH : allows JPanel to expand in both
              *     directions as the containing container expands (in 
              *     this case redPanel will expand both HORIZONTALLY and
              *     VERTICALLY, as leftPanel will expand)
              * weightx : This is the actual width the redPanel will occupy
              *           relative to all other components on the leftPanel
              * weighty : This is the actual height the redPanel will occupy
              *           relative to all other components on the leftPanel
              */
            addComp(leftPanel, redPanel, 0, 0, 1, 1,
                            GridBagConstraints.BOTH, 1.0, 0.1);
            addComp(leftPanel, blackPanel, 0, 1, 1, 1,
                            GridBagConstraints.BOTH, 1.0, 0.8);
            addComp(leftPanel, bluePanel, 0, 2, 1, 1,
                            GridBagConstraints.BOTH, 1.0, 0.1);
            addComp(contentPane, leftPanel, 0, 0, 1, 2,
                            GridBagConstraints.BOTH, 0.5, 1.0);
            addComp(contentPane, greenPanel, 1, 0, 1, 1,
                            GridBagConstraints.BOTH, 0.5, 0.3);
            addComp(contentPane, magentaPanel, 1, 1, 1, 1,
                            GridBagConstraints.BOTH, 0.5, 0.7);
    
            frame.setContentPane(contentPane);
            /*
             * Once you will add components to these
             * JPanels, then use pack(), instead of
             * setSize(). The use of the latter is 
             * just for illustration purpose only
             */
            //frame.pack();
            frame.setSize(300, 300);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private void addComp(JPanel panel, JComponent comp,
                                int x, int y, int width, int height,
                                int fill, double weightx, double weighty) {
            gbc.gridx = x;
            gbc.gridy = y;
            gbc.gridwidth = width;
            gbc.gridheight = height;
            gbc.fill = fill;
            gbc.weightx = weightx;
            gbc.weighty = weighty;
    
            panel.add(comp, gbc);
        }
    
        private JPanel getPanel(Color backColor) {
            JPanel panel = new JPanel();
            panel.setOpaque(true);
            panel.setBackground(backColor);
    
            return panel;
        }
    
        public static void main(String[] args) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    new GridBagExample().displayGUI();
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }
    

    Here is the output of the same :

    GRIDBAGLAYOUTIMAGE

    0 讨论(0)
提交回复
热议问题