Affichage des articles dont le libellé est layoutmanager. Afficher tous les articles
Affichage des articles dont le libellé est layoutmanager. Afficher tous les articles

JAVA - How To Use CardLayout In Java NetBeans

CardLayout Example In Java NetBeans

                                                                                                                                                            

In this java Code we will see How To Use The BorderLayout LayoutManager In Java NetBeans

Source Code:


import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Window {
       static JFrame frame = new JFrame();
       static JPanel PANEL = new JPanel(new BorderLayout());
       static JPanel panelButton = new JPanel();
       static JPanel panelText = new JPanel();
       static JPanel panelLabel = new JPanel();
       static JPanel panelRed = new JPanel();
       static JPanel panelGreen = new JPanel();
       static JPanel panelBlack = new JPanel();
       static JPanel panelWhite = new JPanel();
       static CardLayout card = new CardLayout();
       
    public static void main(String[] args){

        panelText.setName("panelText");
        panelLabel.setName("panelLabel");
        
        JButton buttonText = new JButton("TextField");
        JButton buttonLabel = new JButton("Label");
        JButton buttonFirst = new JButton("First");
        JButton buttonLast = new JButton("Last");
        JButton buttonNext = new JButton("Next");
        JButton buttonPrevious = new JButton("Previous");
        
        JTextField text1 = new JTextField(20);
        JTextField text2 = new JTextField(20);
        JTextField text3 = new JTextField(20);
        
        JLabel label1 = new JLabel("label1");
        JLabel label2 = new JLabel("label2");
        JLabel label3 = new JLabel("label3");
      
        panelButton.add(buttonText);
        panelButton.add(buttonLabel);
        panelButton.add(buttonFirst);
        panelButton.add(buttonLast);
        panelButton.add(buttonNext);
        panelButton.add(buttonPrevious);
        
        panelText.add(text1);
        panelText.add(text2);
        panelText.add(text3);
        
        panelLabel.add(label1);
        panelLabel.add(label2);
        panelLabel.add(label3);
        
        panelRed.setBackground(Color.red);
        panelGreen.setBackground(Color.green);
        panelBlack.setBackground(Color.black);
        panelWhite.setBackground(Color.white);
        
        
        // show the card who contain JTextFields
        buttonText.addActionListener(new ActionListener(){
         
            @Override
            public void actionPerformed(ActionEvent e){
                card.show(PANEL, "panelText");
            }
        });
        
        // show the card who contain JLabels
        buttonLabel.addActionListener(new ActionListener(){
         
            @Override
            public void actionPerformed(ActionEvent e){
                card.show(PANEL, "panelLabel");
            }
        });
        
         // show the first card
        buttonFirst.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
               card.first(PANEL);
            }
        });
        
        // show the last card
        buttonLast.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
             
                card.last(PANEL);
            }
        });
        
        // show the next card
        buttonNext.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            
                card.next(PANEL);
            }
        });
        
        // show the previous card
        buttonPrevious.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                card.previous(PANEL);
            }
        });

        // set a Hgap to the cardlayout
        card.setHgap(20);
        
        // set a Vgap to the cardlayout
        card.setVgap(20);
        
        PANEL.setLayout(card);
        PANEL.add(panelText,"panelText");
        PANEL.add(panelLabel,"panelLabel");
        PANEL.add(panelRed,"RED");
        PANEL.add(panelGreen,"GREEN");
        PANEL.add(panelBlack,"BLACK");
        PANEL.add(panelWhite,"WHITE");

        card.layoutContainer(PANEL);
        
        frame.getContentPane().add(panelButton,BorderLayout.NORTH);
        frame.getContentPane().add(PANEL,BorderLayout.CENTER);
        
        frame.setTitle("CardLayout");
        frame.setSize(500, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

//OUTPUT:


java cardlayout
using cardlayout in java




JAVA - How To Use BorderLayout In Java NetBeans

BorderLayout Example In Java NetBeans

                                                                                                                                                            

In this java Code we will see How To Use The BorderLayout LayoutManager In Java NetBeans

Source Code:

package javaapp;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingLayout {

    public static void main(String[] args){
        
      JFrame frame = new JFrame("BorderLayout");
      
      JPanel panel1 = new JPanel();
      
      panel1.setLayout(new BorderLayout());
      
      // set the button in the top of the container
      panel1.add(new JButton("NORTH"), BorderLayout.NORTH);
      
      // set the button in the right of the container
      panel1.add(new JButton("EAST"), BorderLayout.EAST);
      
      // set the button in the middle of the container
      panel1.add(new JButton("CENTER"), BorderLayout.CENTER);
      
      // set the button in the top of left container
      panel1.add(new JButton("WEST"), BorderLayout.WEST);
      
      // set the button in the bottom of the container
      panel1.add(new JButton("SOUTH"), BorderLayout.SOUTH);
     
      // create an instance of borderlayout with gaps
      BorderLayout bl = new BorderLayout(50,50);
      
      JPanel panel2 = new JPanel(bl);
      
      panel2.add(new JButton("NORTH"), BorderLayout.NORTH);
      panel2.add(new JButton("EAST"), BorderLayout.EAST);
      panel2.add(new JButton("CENTER"), BorderLayout.CENTER);
      panel2.add(new JButton("WEST"), BorderLayout.WEST);
      panel2.add(new JButton("SOUTH"), BorderLayout.SOUTH);
      
        bl.removeLayoutComponent(new JButton("NORTH"));
      
      // return the state of the borderlayout Hgap and Vgap
      System.out.println(bl.toString());
      
      // return the borderlayout Hgap and Vgap
      System.out.println(bl.getHgap()+" - "+bl.getVgap());     
      
      frame.add(panel2);     
       
      frame.setSize(400, 400);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      
    }
}


//OUTPUT:

borderlayout in java
using the panel1

java borderlayout
using the panel2

java.awt.BorderLayout[hgap=50,vgap=50]
50 - 50