如何设置JFrame的插图?

时间:2013-01-02 05:54:32

标签: java swing jframe insets

有没有办法设置JFrame的插图? 我试过了

frame.getContentPane().getInsets().set(10, 10, 10, 10);

frame.getInsets().set(10, 10, 10, 10);

但它们似乎都不起作用。

5 个答案:

答案 0 :(得分:21)

JPanel contentPanel = new JPanel();

Border padding = BorderFactory.createEmptyBorder(10, 10, 10, 10);

contentPanel.setBorder(padding);

yourFrame.setContentPane(contentPanel);

所以基本上,contentPanel是你框架的主要容器。

答案 1 :(得分:3)

覆盖Insets的{​​{1}}不会成为您实际问题的灵魂。 要回答您的问题,您无法设置JFrame的Insets。您应该扩展JFrame并覆盖JFrame方法以提供所需的插入。

答案 2 :(得分:0)

您必须创建一个LayOutConstraint对象并设置其Insets。 就像下面的例子一样,我使用了GridBagLayout()并使用了GridBagConstraint()对象。

    GridBagConstraints c = new GridBagConstraints();
    JPanel panel = new JPanel(new GridBagLayout());
    c.insets = new Insets(5, 5, 5, 5); // top, left, bottom, right
    c.anchor = GridBagConstraints.LINE_END;

    // Row 1
    c.gridx = 0;
    c.gridy = 0;
    c.anchor = GridBagConstraints.LINE_START;
    panel.add(isAlgoEnabledLabel, c);

答案 3 :(得分:0)

由于这个问题还没有明确的答案,你可以像basiljames所说here那样做。正确的方法是扩展JFrame,然后覆盖getInsets()方法。

例如

import javax.swing.JFrame;
import java.awt.Insets;

public class JFrameInsets extends JFrame {
    @Override
    public Insets getInsets() {
        return new Insets(10, 10, 10, 10);
    }

    private JFrameInsets()  {
        super("Insets of 10");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setMinimumSize(getSize());
        setVisible(true);
    }

    public static void main(String[] args) {
        new JFrameInsets();
    }
}

答案 4 :(得分:0)

您可以创建一个主JPanel并将其他所有内容插入其中。

然后,您可以使用BorderFactory创建EmptyBorderLineBorder

查看此答案: https://stackoverflow.com/a/17925693/8953378