简体   繁体   English

如何使用Container添加填充?

[英]How do I add padding with Container?

I am trying to make on top some padding but how to do this with Container? 我试图在顶部做一些填充,但如何使用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);

Output 产量

 [javac] symbol  : method setBorder(javax.swing.border.EmptyBorder)
 [javac] location: class java.awt.Container 
 [javac] contentPane.setBorder(new EmptyBorder(10, 10, 10, 10) );
 [javac]

Can't you simply add a JPanel to contain everything and set empty border on that ? 难道你不能简单地添加一个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);
  • JFrame / Frame and its ContentPane doesn't implements Borders , this is prehistoric Componenet JFrame / Frame及其ContentPane不实现Borders ,这是prehistoric Componenet

  • put directly there JPanel with EmptyBorders 直接放在JPanelEmptyBorders

  • for Java5 and higher isn't required to call for ContentPane (only for setBackground:-) , you can directly add JComponents to the JFrame#add(myJComponent) , notice Swing Top-Level Containers have got implemented BorderLayout by default 对于Java5及更高版本,不需要调用ContentPane (仅用于setBackground:-) ,您可以直接将JComponents添加到JFrame#add(myJComponent) ,注意Swing Top-Level Containers默认已实现BorderLayout

您可以覆盖Container#getInsets ,但您应该使用Swing组件。

don't you prefer to use swing ? 你不喜欢使用秋千吗? Components have a method to add margins. 组件有一种添加边距的方法。 Swing is far better than the obsolete awt library. Swing比过时的awt库要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM