BorderLayout doesn't show correctly

风格不统一 提交于 2019-12-02 04:14:21

You may achieve the desired result with a GridBagLayout and appropriate weights:

public class Snippet {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                JPanel leftBorder = new JPanel();
                JPanel rightBorder = new JPanel();
                leftBorder.setBackground(Color.black);
                rightBorder.setBackground(Color.black);

                JPanel center = new JPanel();
                center.setBackground(Color.red);

                f.setLayout(new GridBagLayout());

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.fill = GridBagConstraints.BOTH;
                gbc.weighty = 1.0;
                gbc.gridy = 0;
                gbc.gridwidth = 1;
                gbc.gridheight = 1;

                gbc.gridx = 0;
                gbc.weightx = 0.1;
                f.add(leftBorder, gbc);

                gbc.gridx = 1;
                gbc.weightx = 0.8;
                f.add(center, gbc);

                gbc.gridx = 2;
                gbc.weightx = 0.1;
                f.add(rightBorder, gbc);

                f.pack();
                f.setVisible(true);
            }
        });
    }
}

A ComponentListener works well for this.

Addendum: The Flank panel needs to revalidate() itself explicitly in response to any size change.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/a/14588506/230513 */
public class Test {

    private static final int W = 320;
    private static final Center center = new Center();

    private static class Center extends JPanel {

        public Center() {
            setBackground(Color.red);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(W, 3 * W / 4);
        }
    };

    private static class Flank extends JPanel {

        private Dimension d = new Dimension(W / 10, 0);

        public Flank() {
            setBackground(Color.black);
            Test.center.addComponentListener(new ComponentAdapter() {

                @Override
                public void componentResized(ComponentEvent e) {
                    d = new Dimension(e.getComponent().getWidth() / 10, 0);
                    revalidate();
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return d;
        }
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.add(new Flank(), BorderLayout.WEST);
        f.add(center, BorderLayout.CENTER);
        f.add(new Flank(), BorderLayout.EAST);

        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!