我试图在顶部填充一些填充但是如何使用Container?
JFrame frame = super.screen.getFullScreenWindow();
//Container contentPane = frame.getContentPane();
//JPanel contentPane = new JPanel();
// Make sure the content pane is transparent
if (contentPane instanceof JComponent) {
((JComponent)contentPane).setOpaque(false);
}
else {
// ??
}
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10) );
//frame.getContentPane().add(contentPane, BorderLayout.CENTER);
[javac] symbol : method setBorder(javax.swing.border.EmptyBorder)
[javac] location: class java.awt.Container
[javac] contentPane.setBorder(new EmptyBorder(10, 10, 10, 10) );
[javac]
答案 0 :(得分:15)
难道你不能简单地添加一个JPanel来包含所有内容并在其上设置空白边框吗?
JPanel containerPanel = new JPanel();
containerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
containerPanel.setLayout(new BorderLayout());
//panel to test
JPanel testPanel = new JPanel();
testPanel.setBackground(Color.blue);
containerPanel.add(testPanel,BorderLayout.CENTER);
//assuming you are extending JFrame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(containerPanel, BorderLayout.CENTER);
答案 1 :(得分:3)
JFrame / Frame
及其ContentPane
未实现Borders
,这是史前Componenet
直接将JPanel
与EmptyBorders
Java5
及更高版本不需要致电ContentPane
(仅适用于setBackground:-)
,您可以直接将JComponents
添加到JFrame#add(myJComponent)
},通知Swing Top-Level Containers
已默认实施BorderLayout
答案 2 :(得分:2)
您可以覆盖Container#getInsets,但您应该使用Swing组件。
答案 3 :(得分:0)