Multiple Panel

This source code is how to make many panel, but show only one panel in a frame...
It is like an application that we have use in our daily life like game..
It destroy all component that not nessecery in frame and replace it with a condition...

This is simple source code how to show it that i have make....

Source code :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MultiplePanel extends JFrame implements ActionListener{
   
    private JLabel statLabel;
  
    private JButton firstButton;
    private JButton secondButton;
    private JButton thirdButton;
    private JButton backButton;
  
    private JPanel mainPanel;
    private JPanel firstPanel;
    private JPanel secondPanel;
    private JPanel thirdPanel;
  
    private Container canvas;

    public MultiplePanel(){
        canvas=getContentPane();
        canvas.setLayout(new BorderLayout());
       
        backButton=new JButton("Back");
        backButton.setBounds(75,60,100,25);
        backButton.addActionListener(this);
       
        createMainPanel();
        canvas.add(mainPanel,BorderLayout.CENTER);
       
        this.setBounds(100,100,250,150);
        this.setVisible(true);
    }
  
    public void actionPerformed(ActionEvent ae){    
        if(ae.getSource()==firstButton){
            createFirstPanel();
            canvas.invalidate();//to set canvas valid to false           
            canvas.remove(mainPanel);//remove mainPanel from canvas
            canvas.add(firstPanel,BorderLayout.CENTER);
            canvas.validate();//to set canvas valid to true
        }
        if(ae.getSource()==secondButton){
            createSecondPanel();
            canvas.invalidate();//to set canvas valid to false           
            canvas.remove(mainPanel);//remove mainPanel from canvas
            canvas.add(secondPanel,BorderLayout.CENTER);
            canvas.validate();//to set canvas valid to true
        }
        if(ae.getSource()==thirdButton){
            createThirdPanel();
            canvas.invalidate();//to set canvas valid to false           
            canvas.remove(mainPanel);//remove mainPanel from canvas
            canvas.add(thirdPanel,BorderLayout.CENTER);
            canvas.validate();//to set canvas valid to true
        }
      
        if(ae.getSource()==backButton){
            createMainPanel();
            canvas.invalidate();//to set canvas valid to false
            canvas.removeAll();//remove all from canvas
            canvas.add(mainPanel,BorderLayout.CENTER);
            canvas.validate();//to set canvas valid to true
        }
       
    }
  
    public void createMainPanel(){
        mainPanel=new JPanel();
        mainPanel.setLayout(null);
       
        firstButton=new JButton("First Panel");
        secondButton=new JButton("Second Panel");
        thirdButton=new JButton("Third Panel");
       
        firstButton.setBounds(40,20,160,25);
        secondButton.setBounds(40,50,160,25);
        thirdButton.setBounds(40,80,160,25);
        firstButton.addActionListener(this);
        secondButton.addActionListener(this);
        thirdButton.addActionListener(this);
       
        mainPanel.add(firstButton);
        mainPanel.add(secondButton);
        mainPanel.add(thirdButton);
         
    }
  
    public void createFirstPanel(){
        firstPanel=new JPanel();
        firstPanel.setLayout(null);
       
        statLabel=new JLabel("First Panel");     
        statLabel.setBounds(50,25,150,25);  
       
        firstPanel.add(statLabel);           
        firstPanel.add(backButton);
    }
    public void createSecondPanel(){
        secondPanel=new JPanel();
        secondPanel.setLayout(null);
       
        statLabel=new JLabel("Second Panel");     
        statLabel.setBounds(50,25,150,25);  
       
        secondPanel.add(statLabel);           
        secondPanel.add(backButton);
    }
    public void createThirdPanel(){
        thirdPanel=new JPanel();
        thirdPanel.setLayout(null);
       
        statLabel=new JLabel("Third Panel");     
        statLabel.setBounds(50,25,150,25);  
       
        thirdPanel.add(statLabel);           
        thirdPanel.add(backButton);
    }
  
    public static void main(String []args){
        MultiplePanel mp=new MultiplePanel();       
        mp.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Sample Running :












you can test it by your self..
i hope you like this souce code...
and please give some comment....
                                                                                                                                                      Created By : Z-man, 2012

1 comment: