1

我想要一个JFrame,在左边和右边有一个边框,颜色为黑色,宽度为withfOfJFrame/10。

现在,我的尝试看起来像这样:

JFrame f = new JFrame();
f.setSize(800, 600);
f.setLayout(new BorderLayout());

JPanel leftBorder = new JPanel();
JPanel rightBorder = new JPanel();
leftBorder.setBackground(Color.black);
rightBorder.setBackground(Color.black);
leftBorder.setSize(f.getWidth()/10, f.getHeight());
rightBorder.setSize(f.getWidth()/10, f.getHeight());
JPanel center = new JPanel();
center.setBackground(Color.red);

f.add(leftBorder, BorderLayout.WEST);
f.add(center, BorderLayout.CENTER);
f.add(rightBorder, BorderLayout.EAST);    
f.setVisible(true);

这会在左侧和右侧添加一个黑色边框,但该边框具有固定大小,并且在调整窗口大小时不会重新计算。大小甚至不是 800 的 1/10(JFrame 的开始宽度)。

我究竟做错了什么?还是有更好的方法来做到这一点?

4

2 回答 2

2

GridBagLayout您可以使用 a和适当的权重来获得所需的结果:

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);
            }
        });
    }
}
于 2013-01-29T17:22:10.627 回答
2

AComponentListener适用于此。

附录:Flank面板需要revalidate()自己明确地响应任何尺寸变化。

图片

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();
            }
        });
    }
}
于 2013-01-29T17:19:31.093 回答