First Attempt
Home ] Up ] [ First Attempt ] Second Attempt ] A Calculator ]

 

 

You might think that you could use a BorderLayout, and simply add the three buttons all to the South.   So your code might look like:

package swingExamples;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class NestedLayouts1Panel extends JPanel
{
  public NestedLayouts1Panel()
  {
    setLayout(new BorderLayout());
    add(m_yellow, BorderLayout.SOUTH);
    add(m_blue, BorderLayout.SOUTH);
    add(m_red, BorderLayout.SOUTH);
  }

  ////////////// Data //////////////////
  private JButton m_yellow = new JButton("Yellow");
  private JButton m_blue = new JButton("Blue");
  private JButton m_red = new JButton("Red");
}

class NestedLayouts1Frame extends JFrame
{
  public NestedLayouts1Frame()
  {
    setTitle("NestedLayouts");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.add( new NestedLayouts1Panel() );
  }
}

public class NestedLayouts1
{
  public static void main(String[] args)
  {
    NestedLayouts1Frame frame = new NestedLayouts1Frame();
    frame.setVisible(true);
  }
}

but the results look like this:

Notice that only the last component to be added to the south is the one that actually resides there.

That clearly doesn't work!

 

This page was last modified on 02 October, 2007