有没有办法设置JFrame
的插图?
我试过了
frame.getContentPane().getInsets().set(10, 10, 10, 10);
和
frame.getInsets().set(10, 10, 10, 10);
但它们似乎都不起作用。
答案 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
创建EmptyBorder
或LineBorder
。