Introduction
This article contains information about Layout Managers beyond that discussed in Part 1.
Layout Managers
For previously discussed Layout Managers, follow the link given below:
http://www.c-sharpcorner.com/UploadFile/9a9e6f/layout-managers-in-java-part-1/
Card Layout Manager
The card layout class manages two or more components that share the same display space in such a way that only one component is visible at a time.
Basically, each card is like a playing card in a stack, where only the top card is visible at any time, that's why it is known as the Card Layout Manager. Each card is normally a panel that can use any layout manager.
Example
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
-
- public class Card extends JFrame implements ActionListener {
- private CardLayout cardManager;
- private JPanel deck;
- private JButton controls[];
- private String names[] = { "First card", "Next card", "Previous card", "Last card" };
-
- public Card()
- {
- super( "CardLayout manager" );
-
- Container c = getContentPane();
-
-
- deck = new JPanel();
- cardManager = new CardLayout();
- deck.setLayout( cardManager );
-
-
- JLabel label1 = new JLabel( "card one", SwingConstants.CENTER );
- JPanel card1 = new JPanel();
- card1.setBackground( Color.GREEN);
- card1.add( label1 );
- deck.add( card1, label1.getText() );
-
-
- JLabel label2 = new JLabel( "card two", SwingConstants.CENTER );
- JPanel card2 = new JPanel();
- card2.setBackground( Color.blue );
- card2.add( label2 );
- deck.add( card2, label2.getText() );
-
-
- JLabel label3 = new JLabel( "card three" ,SwingConstants.CENTER);
- JPanel card3 = new JPanel();
- card3.setBackground( Color.RED);
- card3.add( label3);
- deck.add( card3, label3.getText() );
-
-
- JPanel buttons = new JPanel();
- buttons.setLayout( new GridLayout( 2, 2 ) );
- controls = new JButton[ names.length ];
-
- for ( int i = 0; i < controls.length; i++ ) {
- controls[ i ] = new JButton( names[ i ] );
- controls[ i ].addActionListener( this );
- buttons.add( controls[ i ] );
- }
-
-
- c.add( buttons, BorderLayout.WEST );
- c.add( deck, BorderLayout.EAST );
-
- setSize( 450,300 );
- show();
- }
-
- public void actionPerformed( ActionEvent e )
- {
- if ( e.getSource() == controls[ 0 ] )
- cardManager.first( deck );
- else if ( e.getSource() == controls[ 1 ] )
- cardManager.next( deck );
- else if ( e.getSource() == controls[ 2 ] )
- cardManager.previous( deck );
- else if ( e.getSource() == controls[ 3 ] )
- cardManager.last( deck );
- }
-
- public static void main( String args[] )
- {
- Card cardDeckDemo = new Card();
-
- cardDeckDemo.addWindowListener(
- new WindowAdapter() {
- public void windowClosing( WindowEvent e )
- {
- System.exit( 0 );
- }
- });
- }
- }
Output
Outputs after clicking "Next card" and "Last card" and then "Previous card":
Next card
Last card
Previous card will be the next card, in other words card two.
GridBag Layout Manager
The GridBag layout manages the components in rows or columns that allow specified components to span multiple rows and columns. Not all the rows and columns have the same height and width.
This is the most complex layout manager since it specifies the size and position characteristics of its components by specifying constraints for each component.
Example
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
-
- public class GridBag extends JFrame {
- private Container container;
- private GridBagLayout gbLayout;
- private GridBagConstraints gbConstraints;
-
- public GridBag()
- {
- super( "GridBagLayout manager" );
-
- container = getContentPane();
- gbLayout = new GridBagLayout();
- container.setLayout( gbLayout );
-
-
- gbConstraints = new GridBagConstraints();
-
- JTextArea ta = new JTextArea( "TextField1", 5, 10 );
- JTextArea tx = new JTextArea( "TextField2", 2, 2 );
- String names[] = { "Laptop", "Palmtop", "Tablet", "Mobile" };
- JComboBox cb = new JComboBox( names );
- JTextField tf = new JTextField( "TextField" );
- JButton b1 = new JButton( "Button 1" );
- JButton b2 = new JButton( "Button 2" );
- JButton b3 = new JButton( "Button 3" );
-
-
-
-
- gbConstraints.fill = GridBagConstraints.BOTH;
- addComponent( ta, 0, 0, 1, 3 );
-
-
-
- gbConstraints.fill = GridBagConstraints.HORIZONTAL;
- addComponent( b1, 0, 1, 2, 1 );
-
-
-
-
- addComponent( cb, 2, 1, 2, 1 );
-
-
- gbConstraints.weightx = 1000;
- gbConstraints.weighty = 1;
- gbConstraints.fill = GridBagConstraints.BOTH;
- addComponent( b2, 1, 1, 1, 1 );
-
-
-
- gbConstraints.weightx = 0;
- gbConstraints.weighty = 0;
- addComponent( b3, 1, 2, 1, 1 );
-
-
-
- addComponent( tf, 3, 0, 2, 1 );
-
-
-
- addComponent( tx, 3, 2, 1, 1 );
-
- setSize( 350, 200 );
- show();
- }
-
-
- private void addComponent( Component c, int row, int column, int width, int height )
- {
-
- gbConstraints.gridx = column;
- gbConstraints.gridy = row;
-
-
- gbConstraints.gridwidth = width;
- gbConstraints.gridheight = height;
-
-
- gbLayout.setConstraints( c, gbConstraints );
- container.add( c );
- }
-
- public static void main( String args[] )
- {
- GridBag app = new GridBag();
-
- app.addWindowListener(
- new WindowAdapter() {
- public void windowClosing( WindowEvent e )
- {
- System.exit( 0 );
- }
- });
- }
- }
Output
For further read about the topic , please go through the below link.