簡體   English   中英

如何使用Container添加填充?

[英]How do I add padding 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);

產量

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

難道你不能簡單地添加一個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及其ContentPane不實現Borders ,這是prehistoric Componenet

  • 直接放在JPanelEmptyBorders

  • 對於Java5及更高版本,不需要調用ContentPane (僅用於setBackground:-) ,您可以直接將JComponents添加到JFrame#add(myJComponent) ,注意Swing Top-Level Containers默認已實現BorderLayout

您可以覆蓋Container#getInsets ,但您應該使用Swing組件。

你不喜歡使用秋千嗎? 組件有一種添加邊距的方法。 Swing比過時的awt庫要好得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM