0

2 つの異なるコンポーネントを同時に表示するのに問題があります。

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);
   }
}

今、このフォーラムで、両方を同時に表示するために何かできることを読みましたが、それがどのように行われたかを理解できませんでした。誰でも助けてもらえますか?私は、一方がもう一方の前にあることを望んでいます。レイヤリングを制御する方法はありますか? 前もって感謝します。

4

2 に答える 2

1

あなたが抱えている問題はJFrame、デフォルトでBorderLayout. BorderLayout単一のコンポーネントのみを、5 つの使用可能な位置のいずれかに表示できます。

単一のコンテナーに複数のコンポーネントを追加するには、レイアウトを構成するか、ニーズにより適したものを使用する必要があります...

その他の例については、A Visual Guide to Layout Managers をご覧ください

于 2013-11-01T20:50:11.977 に答える
1

JFrameLayout Manager内では、通常、さまざまなコンポーネントを配置するために使用されます。チュートリアルはこちら.

何かのようなもの:

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

の基本的なレイアウト マネージャーをセットアップするには、JFrame. z-order - docs pageJLayeredPaneを指定できるもあるので、次のようになります。

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 クラスから継承されている場合は、適切なサイズなどを設定して、サイズがゼロにならないようにする必要があるかもしれません!

于 2013-11-01T18:36:29.077 に答える