Studyon Minte9.com
Java SE




Group Layout



Groups are created via createSequentialGroup and createParallelGroup methods.

LEADING means left (top) 
TRAILING means right (bottom)

package testexample; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Test extends JFrame { private static JPanel c1; // --- Look Here --- // private static JPanel c2; private static JPanel c3; private static JPanel c4; private static GroupLayout layout; public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 300, 200); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); } public Test() { initMenu(); } public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu menuOpen = new JMenu("Open"); JMenuItem menuOpenItem1 = new JMenuItem("Horizontal"); JMenuItem menuOpenItem2 = new JMenuItem("Vertical"); JMenuItem menuOpenItem3 = new JMenuItem("Alignment"); JMenuItem menuOpenItem4 = new JMenuItem("Trailing"); menuBar.add(menuOpen); menuOpen.add(menuOpenItem1); menuOpen.add(menuOpenItem2); menuOpen.add(menuOpenItem3); menuOpen.add(menuOpenItem4); setJMenuBar(menuBar); menuOpenItem1.addActionListener(new MenuAction(new Action1())); menuOpenItem2.addActionListener(new MenuAction(new Action2())); menuOpenItem3.addActionListener(new MenuAction(new Action3())); menuOpenItem4.addActionListener(new MenuAction(new Action4())); } 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, BorderLayout.CENTER); getContentPane().doLayout(); validate(); repaint(); } public void initLayoutAndElements(JPanel panel) { // --- Look Here --- // layout = new GroupLayout(panel); panel.setLayout(layout); c1 = new JPanel(); c1.setMaximumSize(new Dimension(10, 10)); c1.setBackground(Color.red); c2 = new JPanel(); c2.setMaximumSize(new Dimension(20, 20)); c2.setBackground(Color.yellow); c3 = new JPanel(); c3.setMaximumSize(new Dimension(30, 30)); c3.setBackground(Color.black); c4 = new JPanel(); c4.setMaximumSize(new Dimension(40, 40)); c4.setBackground(Color.blue); } class Action1 extends JPanel { public Action1() { initLayoutAndElements(this); layout.setHorizontalGroup( layout.createSequentialGroup() // --- Look Here --- // .addGap(150) .addComponent(c1) .addComponent(c2) .addComponent(c3) .addComponent(c4) ); layout.setVerticalGroup( layout.createParallelGroup() .addComponent(c1) .addComponent(c2) .addComponent(c3) .addComponent(c4) ); } } class Action2 extends JPanel { public Action2() { initLayoutAndElements(this); layout.setHorizontalGroup( layout.createParallelGroup() // --- Look Here --- // .addComponent(c1) .addComponent(c2) .addComponent(c3) .addComponent(c4) ); layout.setVerticalGroup( layout.createSequentialGroup() .addGap(50) .addComponent(c4) .addComponent(c3) .addComponent(c2) .addComponent(c1) ); } } class Action3 extends JPanel { public Action3() { initLayoutAndElements(this); layout.setHorizontalGroup( layout.createSequentialGroup() .addGap(150) .addComponent(c1) // --- Look Here --- // .addComponent(c2) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER) .addComponent(c3) .addComponent(c4) ) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.CENTER) .addComponent(c1) .addComponent(c2) .addGroup(layout.createSequentialGroup() .addComponent(c3) .addComponent(c4) ) ); } } class Action4 extends JPanel { public Action4() { initLayoutAndElements(this); layout.setHorizontalGroup( layout.createSequentialGroup() .addGap(150) .addComponent(c1) .addComponent(c2) .addComponent(c3) .addComponent(c4) ); // --- Look Here --- // layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(c1) .addComponent(c2) .addComponent(c3) .addComponent(c4) ); } } }
 


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