簡體   English   中英

JFrame中。 無法顯示多個組件

[英]Jframe. Cant get multiple components to display

我無法同時顯示兩個不同的組件。

public class BandViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
      frame.setSize(1000, 800);
      frame.setTitle("band");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      BandComponent component = new BandComponent();
      ConcertBackground component1 = new ConcertBackground();
      frame.add(component);
      frame.add(component1);

      frame.setVisible(true);
   }
}

現在我在這個論壇上看到你可以做一些事情同時展示,但卻無法弄清楚它是如何完成的。 有人可以幫忙嗎? 我希望有一個在另一個面前。 他們是否有某種方法來控制分層? 提前致謝。

JFrame ,布局管理器通常用於定位不同的組件, 這里是教程。

就像是:

Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());

JFrame設置基本布局管理器。 還有一個JLayeredPane允許您指定一個z-order - docs頁面 ,所以類似於:

JLayeredPane layeredPane = new JLayeredPane();

BandComponent component = new BandComponent();
ConcertBackground component1 = new ConcertBackground();
layeredPane.add(component, 0); // 0 to display underneath component1
layeredPane.add(component1, 1);

contentPane.add(layeredPane);

以這種方式設置顯示層次結構,其中對象在對象內。 我不確定BandComponentConcertBackground類是什么,但是如果它們從Swing類繼承,則可能必須設置首選大小或類似值以確保它們沒有零大小!

您遇到的問題是因為默認情況下JFrame使用BorderLayout BorderLayout只允許單個組件出現在其五個可用位置中的任何一個位置。

要將多個組件添加到單個容器,您需要配置布局或使用更符合您需求的布局...

有關更多示例,請參閱布局管理器的可視指南

暫無
暫無

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

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