package examples; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.*; import javax.swing.border.LineBorder; import static java.awt.BorderLayout.*; public class GUILayoutExample extends JPanel { static final GridBagConstraints gbc = new GridBagConstraints(); static final SpringLayout.Constraints sc = new SpringLayout.Constraints(); static enum LayoutChoice {border, flow, grid, gridbag, cascade}; ButtonGroup selection = new ButtonGroup(); ChoiceButton[] choices = new ChoiceButton[LayoutChoice.values().length]; JPanel choicesPanel = new JPanel(); public GUILayoutExample() { setLayout(new BorderLayout()); AbstractButton no, yes; add(yes = new JButton(), WEST); yes.setIcon(new ImageIcon("smile.jpg")); yes.setToolTipText("yes"); add(no = new JButton("no"), EAST); no.setIcon(new ImageIcon("frown.jpg")); JPanel nestedPanel = new JPanel(new BorderLayout()); add(nestedPanel, BorderLayout.SOUTH); nestedPanel.add(new JSlider(), EAST); nestedPanel.add(new JTextField("Enter text here"), WEST); add(new JLabel("What's your favorite LayoutManager?"), NORTH); add(choicesPanel, CENTER); choicesPanel.setBorder(new LineBorder(Color.BLUE, 2)); for (LayoutChoice choice : LayoutChoice.values()) choices[choice.ordinal()] = new ChoiceButton(choice); choices[0].doClick(); } void changeLayout(LayoutChoice lc) { choicesPanel.removeAll(); switch (lc) { case flow: addComponentsFlow(); break; case border: addComponentsBorder(); break; case grid: addComponentsGrid(); break; case gridbag: addComponentsGridBag(); break; case cascade: addComponentsCascade(); break; } choicesPanel.revalidate(); } void addComponentsFlow() { choicesPanel.setLayout(new FlowLayout()); for (JComponent c : choices) choicesPanel.add(c); } void addComponentsGrid() { choicesPanel.setLayout(new GridLayout(2,0)); for (JComponent c : choices) choicesPanel.add(c); } void addComponentsBorder() { choicesPanel.setLayout(new BorderLayout()); choicesPanel.add(choices[0], NORTH); choicesPanel.add(choices[1], WEST); choicesPanel.add(choices[2], CENTER); choicesPanel.add(choices[3], EAST); choicesPanel.add(choices[4], SOUTH); } void addComponentsGridBag() { choicesPanel.setLayout(new GridBagLayout()); addToGridBag(choices[0],0,0,1,2); addToGridBag(choices[1],2,0,2,1); addToGridBag(choices[2],0,2,10,1); addToGridBag(choices[3],0,5,2,2); addToGridBag(choices[4],2,7,2,2); } void addToGridBag(JComponent c, int x, int y, int width, int height) { gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = width; gbc.gridheight = height; gbc.fill = GridBagConstraints.BOTH; gbc.anchor = GridBagConstraints.EAST; choicesPanel.add(c, gbc); } void addComponentsCascade() { choicesPanel.setLayout(new CascadingLayout()); for (JComponent c : choices) { choicesPanel.add(c); } } class ChoiceButton extends JToggleButton implements ActionListener { LayoutChoice choice; ChoiceButton(LayoutChoice choice) { super(choice + " layout"); this.choice = choice; selection.add(this); addActionListener(this); } public void actionPerformed(ActionEvent e) { changeLayout(choice); } } public static void main(String[] args) { GUILayoutExample g = new GUILayoutExample(); JFrame f = new JFrame("My GUI"); f.add(g); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }