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:
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:
using cardlayout in java |