This applet demonstrates various layout managers. The applet itself uses a border layout with a JPanel in the center, a JComboBox menu to the North, and a JLabel
to the south. The center panel uses a CardLayout. Each card in the card layout contains a number of buttons and uses a different layout manager. The JComboBox menu is used to select among these cards. The JLabel reports events as they occur.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | /******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LayoutDemo extends JApplet implements ActionListener, ItemListener { CardLayout cards; // the layout manager for the center panel JPanel cardPanel; // the center panel JComboBox panelChoice; // menu for selecting which card to show JLabel message; // a message shown at the bottom of the applet public void init() { panelChoice = new JComboBox(); // Set up the menu panelChoice.setBackground(Color.white); panelChoice.addItem("FlowLayout"); // Add in the names of the cards. panelChoice.addItem("FlowLayout with Big Hgap"); panelChoice.addItem("Vertical BoxLayout"); panelChoice.addItem("Horizontal BoxLayout with Struts"); panelChoice.addItem("BorderLayout"); panelChoice.addItem("GridLayout(3,2)"); panelChoice.addItem("GridLayout(1,0)"); panelChoice.addItem("GridLayout(0,1)"); panelChoice.addItemListener(this); message = new JLabel("Layout Demo", JLabel.CENTER); // Set up the mesage message.setBackground(Color.white); message.setOpaque(true); // so background color will show message.setBorder(BorderFactory.createEmptyBorder(5,0,3,0)); message.setForeground(Color.red); cardPanel = new JPanel(); // Set up the center panel cardPanel.setBackground(Color.white); cards = new CardLayout(); cardPanel.setLayout(cards); setBackground(Color.blue); getContentPane().setBackground(Color.blue); getContentPane().setLayout(new BorderLayout(3,3)); getContentPane().add("Center",cardPanel); getContentPane().add("North",panelChoice); getContentPane().add("South",message); JPanel panel; // Will represent various cards to be added to the center panel. Box box; // For the cards that use a BoxLayout. // Set up each "card" in the center panel to have its own layout // manager and to contain a variety of buttons. panel = new JPanel(); // use default FlowLayout for panel panel.setBackground(Color.white); cardPanel.add(panel, "FlowLayout"); addButton(panel,"First Button"); // ( addButton is a untility method, defined below ) addButton(panel,"Second Button"); addButton(panel,"Third Button"); addButton(panel,"Fourth Button"); addButton(panel,"Fifth Button"); addButton(panel,"Sixth Button"); addButton(panel,"Seventh Button"); panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.CENTER,30000,5)); panel.setBackground(Color.white); cardPanel.add(panel,"FlowLayout with Big Hgap"); addButton(panel," A Button"); addButton(panel,"Another Button"); addButton(panel,"A Third Button"); addButton(panel,"A Fourth Button"); addButton(panel,"A Final Button"); box = Box.createVerticalBox(); box.setBackground(Color.white); cardPanel.add(box,"Vertical BoxLayout"); addButton(box,"Button One"); addButton(box,"Button Two"); addButton(box,"Button Three"); addButton(box,"Button Four"); addButton(box,"Button Five"); addButton(box,"Button Six"); box = Box.createHorizontalBox(); box.setBackground(Color.white); cardPanel.add(box,"Horizontal BoxLayout with Struts"); addButton(box,"1st"); addButton(box,"2nd"); box.add( Box.createHorizontalStrut(10) ); addButton(box,"3rd"); addButton(box,"4th"); box.add( Box.createHorizontalStrut(10) ); addButton(box,"5th"); panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBackground(Color.white); cardPanel.add(panel,"BorderLayout"); addButton(panel,"Center Button", BorderLayout.CENTER); addButton(panel,"North Button", BorderLayout.NORTH); addButton(panel,"South Button", BorderLayout.SOUTH); addButton(panel,"East Button", BorderLayout.EAST); addButton(panel,"West Button", BorderLayout.WEST); panel = new JPanel(); panel.setLayout(new GridLayout(3,2)); panel.setBackground(Color.white); cardPanel.add(panel,"GridLayout(3,2)"); addButton(panel,"Button 1"); addButton(panel,"Button 2"); addButton(panel,"Button 3"); addButton(panel,"Button 4"); addButton(panel,"Button 5"); addButton(panel,"Button 6"); panel = new JPanel(); panel.setLayout(new GridLayout(1,0)); panel.setBackground(Color.white); cardPanel.add(panel,"GridLayout(1,0)"); addButton(panel,"Button 1"); addButton(panel,"Button 2"); addButton(panel,"Button 3"); addButton(panel,"Button 4"); panel = new JPanel(); panel.setLayout(new GridLayout(0,1)); panel.setBackground(Color.white); cardPanel.add(panel,"GridLayout(0,1)"); addButton(panel,"Button 1"); addButton(panel,"Button 2"); addButton(panel,"Button 3"); addButton(panel,"Button 4"); addButton(panel,"Button 5"); addButton(panel,"Button 6"); } // end init() public Insets getInsets() { // specify borders around the edges of the applet return new Insets(3,3,3,3); } void addButton(Container p, String name) { // Create a button with the given name and add it // to the given panel. Set up the button to send // events to the applet. JButton b = new JButton(name); p.add(b); b.addActionListener(this); } void addButton(JPanel p, String name, Object option) { // Same as above, but use the "option" object // as an additional parameter in the add method. JButton b = new JButton(name); p.add(b, option); b.addActionListener(this); } public void actionPerformed(ActionEvent evt) { // A button was pressed. Report the name // of the button by setting the message text. String buttonName = evt.getActionCommand(); message.setText("Button \"" + buttonName + "\" was pressed."); } public void itemStateChanged(ItemEvent evt) { // The user has selected an item from the JComboBox. // Change the displayed card to match. String panelName = (String)panelChoice.getSelectedItem(); cards.show(cardPanel, panelName); message.setText("Panel \"" + panelName + "\" was selected."); } } // end class LayoutDemo |