Studyon Minte9.com
Java SE




Border Layout



A BorderLayout places components in up to five areas: top, bottom, left, right, and center. 

package testexample; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.EmptyBorder; public class Test extends JFrame { public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 300, 300); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.setResizable(false); frame.setVisible(true); } public Test() { initMenu(); } public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu menuFile = new JMenu("File"); JMenuItem menuItem1= new JMenuItem("Page1"); JMenuItem menuItem2 = new JMenuItem("Page2"); menuBar.add(menuFile); menuFile.add(menuItem1); menuFile.add(menuItem2); menuBar.setPreferredSize(new Dimension(300, 25)); setJMenuBar(menuBar); menuItem1.addActionListener(new MenuAction(new Action1())); } private class MenuAction implements ActionListener { private JPanel panel; private MenuAction(JPanel panel) { this.panel = panel; } @Override public void actionPerformed(ActionEvent e) { changePanel(panel); } } public void changePanel(JPanel panel) { getContentPane().removeAll(); getContentPane().add(panel); getContentPane().doLayout(); validate(); repaint(); } } // --- Look here --- // class Action1 extends JPanel { public Action1() { JPanel top = new JPanel(); top.setPreferredSize(new Dimension(300, 50)); top.setBackground(Color.decode("#CCCCCC")); JPanel left = new JPanel(); left.setPreferredSize(new Dimension(50, 125)); left.setBackground(Color.decode("#DDDDDD")); JPanel center = new JPanel(); center.setPreferredSize(new Dimension(175, 125)); center.setBackground(Color.decode("#AAAAAA")); JPanel right = new JPanel(); right.setPreferredSize(new Dimension(50, 125)); right.setBackground(Color.decode("#DDDDDD")); JPanel bottom = new JPanel(); bottom.setPreferredSize(new Dimension(300, 50)); bottom.setBackground(Color.decode("#CCCCCC")); add(top, BorderLayout.PAGE_START); add(left, BorderLayout.LINE_START); add(center, BorderLayout.CENTER); add(right, BorderLayout.LINE_END); add(bottom, BorderLayout.PAGE_END); } }
 


http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html