GridBagLayout panels alignment

后端 未结 7 1554
北荒
北荒 2020-12-07 01:38

I have a little issue with the GridBag Layout Manager. I am trying to display 9 panels like this:

\"Ideal

相关标签:
7条回答
  • 2020-12-07 02:17

    For the last two JPanel, I would suggest you to simply add them to a new JPanel with GridLayout and set this JPanel with the GridLayout on to the JPanel having the GridBagLayout. Please have a look at the code example, that resulted in actually the same output, as you were expecting :

    import javax.swing.*;
    import java.awt.*;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Gagandeep Bali
     * Date: 2/7/13
     * Time: 10:34 PM
     * To change this template use File | Settings | File Templates.
     */
    public class GridBagExample
    {
        private JPanel contentPane, footerPanel;
        private JPanel s1, s2, s3, s4, s5, s6, s7, s8, s9;
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("GridBagLayout Example");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            contentPane = new JPanel();
            contentPane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            /*
             * Constraints for S1 JPanel.
             */
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridwidth = 1;
            gbc.gridheight = 2;
            gbc.weightx = 0.2;
            gbc.weighty = 0.8;
            s1 = new JPanel(new GridBagLayout());
            s1.setOpaque(true);
            s1.setBackground(Color.BLUE);
            s1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
            JLabel s1Label = new JLabel("S1", JLabel.CENTER);
            s1.add(s1Label);
            contentPane.add(s1, gbc);
    
            /*
             * Constraints for S2 JPanel.
             */
            gbc.weighty = 0.4;
            gbc.gridx = 1;
            gbc.gridheight = 1;
            s2 = new JPanel(new GridBagLayout());
            s2.setOpaque(true);
            s2.setBackground(Color.GREEN);
            s2.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
            JLabel s2Label = new JLabel("S2", JLabel.CENTER);
            s2.add(s2Label);
            contentPane.add(s2, gbc);
    
            /*
             * Constraints for S3 JPanel.
             */
            gbc.gridy = 1;
            s3 = new JPanel(new GridBagLayout());
            s3.setOpaque(true);
            s3.setBackground(Color.CYAN);
            s3.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
            JLabel s3Label = new JLabel("S3", JLabel.CENTER);
            s3.add(s3Label);
            contentPane.add(s3, gbc);
    
            /*
             * Constraints for S4 JPanel.
             */
            gbc.weighty = 0.8;
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.gridheight = 2;
            s4 = new JPanel(new GridBagLayout());
            s4.setOpaque(true);
            s4.setBackground(Color.WHITE);
            s4.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
            JLabel s4Label = new JLabel("S4", JLabel.CENTER);
            s4.add(s4Label);
            contentPane.add(s4, gbc);
    
            /*
             * Constraints for S5 JPanel.
             */
            gbc.weighty = 0.4;
            gbc.gridx = 3;
            gbc.gridheight = 1;
            s5 = new JPanel(new GridBagLayout());
            s5.setOpaque(true);
            s5.setBackground(Color.RED);
            s5.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
            JLabel s5Label = new JLabel("S5", JLabel.CENTER);
            s5.add(s5Label);
            contentPane.add(s5, gbc);
    
            /*
             * Constraints for S6 JPanel.
             */
            gbc.gridy = 1;
            s6 = new JPanel(new GridBagLayout());
            s6.setOpaque(true);
            s6.setBackground(Color.MAGENTA);
            s6.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
            JLabel s6Label = new JLabel("S6", JLabel.CENTER);
            s6.add(s6Label);
            contentPane.add(s6, gbc);
    
            /*
             * Constraints for S7 JPanel.
             */
            gbc.gridheight = 2;
            gbc.weighty = 0.8;
            gbc.gridx = 4;
            gbc.gridy = 0;
            s7 = new JPanel(new GridBagLayout());
            s7.setOpaque(true);
            s7.setBackground(Color.ORANGE);
            s7.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
            JLabel s7Label = new JLabel("S7", JLabel.CENTER);
            s7.add(s7Label);
            contentPane.add(s7, gbc);
    
            footerPanel = new JPanel();
            footerPanel.setLayout(new GridLayout(1, 2, 5, 5));
    
            s8 = new JPanel(new GridBagLayout());
            s8.setOpaque(true);
            s8.setBackground(Color.PINK);
            s8.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
            JLabel s8Label = new JLabel("S8", JLabel.CENTER);
            s8.add(s8Label);
            footerPanel.add(s8);
    
            s9 = new JPanel(new GridBagLayout());
            s9.setOpaque(true);
            s9.setBackground(Color.YELLOW);
            s9.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
            JLabel s9Label = new JLabel("S9", JLabel.CENTER);
            s9.add(s9Label);
            footerPanel.add(s9, gbc);
    
            /*
             * Constraints for Footer JPanel.
             */
            gbc.gridheight = 1;
            gbc.gridwidth = 5;
            gbc.weightx = 1.0;
            gbc.weighty = 0.2;
            gbc.gridx = 0;
            gbc.gridy = 2;
            contentPane.add(footerPanel, gbc);
    
            frame.setContentPane(contentPane);
            frame.pack();
            //frame.setSize(500, 500);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new GridBagExample().displayGUI();
                }
            });
        }
    }
    

    Here is the Output of the above code :

    GridBagLayoutExample

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