1  import java.awt.*;
  2  import java.awt.event.*;
  3  import javax.swing.*;
  4  
  5  public class ShowLayout extends JApplet {
  6    // Get the url for HTML files
  7    private String flowLayoutDesc = "FlowLayout arranges components " +
  8      "according to their preferredSize in " +
  9      "a left-to-right flow, much like lines of text in a paragraph.";
 10    private String gridLayoutDesc = "GridLayout arranges ...";
 11    private String boxLayoutDesc = "BoxLayout arranges ...";
 12  
 13    private JRadioButton jrbFlowLayout =
 14      new JRadioButton("FlowLayout");
 15    private JRadioButton jrbGridLayout =
 16      new JRadioButton("GridLayout", true);
 17    private JRadioButton jrbBoxLayout =
 18      new JRadioButton("BoxLayout");
 19  
 20    private JPanel jpComponents = new JPanel();
 21    private JTextArea jtfDescription = new JTextArea();
 22  
 23    // Create layout managers
 24    private FlowLayout flowLayout = new FlowLayout();
 25    private GridLayout gridLayout = new GridLayout(2, 2, 3, 3);
 26    private BoxLayout boxLayout =
 27      new BoxLayout(jpComponents, BoxLayout.X_AXIS);
 28  
 29    public ShowLayout() {
 30      // Create a box to hold radio buttons
 31      Box jpChooseLayout = Box.createVerticalBox();
 32      jpChooseLayout.add(jrbFlowLayout);
 33      jpChooseLayout.add(jrbGridLayout);
 34      jpChooseLayout.add(jrbBoxLayout);
 35  
 36      // Group radio buttons
 37      ButtonGroup btg = new ButtonGroup();
 38      btg.add(jrbFlowLayout);
 39      btg.add(jrbGridLayout);
 40      btg.add(jrbBoxLayout);
 41  
 42      // Wrap lines and words
 43      jtfDescription.setLineWrap(true);
 44      jtfDescription.setWrapStyleWord(true);
 45  
 46      // Add fours buttons to jpComponents
 47      jpComponents.add(new JButton("Button 1"));
 48      jpComponents.add(new JButton("Button 2"));
 49      jpComponents.add(new JButton("Button 3"));
 50      jpComponents.add(new JButton("Button 4"));
 51  
 52      // Create two split panes to hold jpChooseLayout, jpComponents,
 53      // and jtfDescription
 54      JSplitPane jSplitPane2 = new JSplitPane(
 55        JSplitPane.VERTICAL_SPLIT, jpComponents,
 56        new JScrollPane(jtfDescription));
 57      JSplitPane jSplitPane1 = new JSplitPane(
 58        JSplitPane.HORIZONTAL_SPLIT, jpChooseLayout, jSplitPane2);
 59  
 60      // Set FlowLayout as default
 61      jpComponents.setLayout(flowLayout);
 62      jpComponents.revalidate();
 63      jtfDescription.setText(flowLayoutDesc);
 64  
 65      add(jSplitPane1, BorderLayout.CENTER);
 66  
 67      // Register listeners
 68      jrbFlowLayout.addActionListener(new ActionListener() {
 69        @Override
 70        public void actionPerformed(ActionEvent e) {
 71          jpComponents.setLayout(flowLayout);
 72          jtfDescription.setText(flowLayoutDesc);
 73          jpComponents.revalidate();
 74        }
 75      });
 76      jrbGridLayout.addActionListener(new ActionListener() {
 77        @Override
 78        public void actionPerformed(ActionEvent e) {
 79          jpComponents.setLayout(gridLayout);
 80          jtfDescription.setText(gridLayoutDesc);
 81          jpComponents.revalidate();
 82        }
 83      });
 84      jrbBoxLayout.addActionListener(new ActionListener() {
 85        @Override
 86        public void actionPerformed(ActionEvent e) {
 87          jpComponents.setLayout(boxLayout);
 88          jtfDescription.setText(boxLayoutDesc);
 89          jpComponents.revalidate();
 90        }
 91      });
 92    }
 93  
 94    public static void main(String[] args) {
 95      ShowLayout applet = new ShowLayout();
 96      JFrame frame = new JFrame();
 97      //EXIT_ON_CLOSE == 3
 98      frame.setDefaultCloseOperation(3);
 99      frame.setTitle("ShowLayout");
100      frame.getContentPane().add(applet, BorderLayout.CENTER);
101      applet.init();
102      applet.start();
103      frame.setSize(400,320);
104      frame.setLocationRelativeTo(null);
105      frame.setVisible(true);
106    }
107  }