Multiple Frame (Many Windows)

this source code use to create a many frame in many window....
but all frame only can visible in one window...
if it already visible then it will do nothing...
else, it will generate new frame...
it can use if you have problem to display many form....
or you want to show every task in different frame...

Source Code :

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

public class MultipleFrame1 implements ActionListener{
   
    private JButton showFrame1;
    private JButton showFrame2;
    private JButton showFrame3;
   
    private JButton closeFrame1;
    private JButton closeFrame2;
    private JButton closeFrame3;
   
    private JLabel strLabel;
   
    private JFrame mainFrame;
    private JFrame firstFrame;
    private JFrame secondFrame;
    private JFrame thirdFrame;
       
    public MultipleFrame1(){
        mainFrame=new JFrame();
      
        Container canvas=mainFrame.getContentPane();
        canvas.setLayout(null);
       
        showFrame1=new JButton("Show First Frame");
        showFrame2=new JButton("Show Second Frame");
        showFrame3=new JButton("Show Third Frame");
       
        closeFrame1=new JButton("Close First Frame");
        closeFrame2=new JButton("Close Second Frame");
        closeFrame3=new JButton("Close Third Frame");
       
        showFrame1.setBounds(40,20,160,25);
        showFrame2.setBounds(40,50,160,25);
        showFrame3.setBounds(40,80,160,25);
       
        closeFrame1.setBounds(40,120,160,25);
        closeFrame2.setBounds(40,150,160,25);
        closeFrame3.setBounds(40,180,160,25);
       
        showFrame1.addActionListener(this);
        showFrame2.addActionListener(this);
        showFrame3.addActionListener(this);
       
        closeFrame1.addActionListener(this);
        closeFrame2.addActionListener(this);
        closeFrame3.addActionListener(this);
       
        canvas.add(showFrame1);
        canvas.add(showFrame2);
        canvas.add(showFrame3);
       
        canvas.add(closeFrame1);
        canvas.add(closeFrame2);
        canvas.add(closeFrame3);
       
        mainFrame.setBounds(300,300,250,250);
        mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
    }
   
    public void actionPerformed(ActionEvent ae){
       
        if(ae.getSource()==showFrame1){
            if(firstFrame==null){
                createFirstFrame();
            }
            else if(!firstFrame.isShowing()){
                createFirstFrame();
            }
        }
        if(ae.getSource()==showFrame2){
            if(secondFrame==null){
                createSecondFrame();
            }
            else if(!secondFrame.isShowing()){
                createSecondFrame();
            }
        }
        if(ae.getSource()==showFrame3){
            if(thirdFrame==null){
                createThirdFrame();
            }
            else if(!thirdFrame.isShowing()){
                createThirdFrame();
            }
        }
       
        if(ae.getSource()==closeFrame1){
            if(firstFrame!=null){
                if(firstFrame.isShowing()){
                    firstFrame.dispose();
                    firstFrame=null;
                }
            }
        }
        if(ae.getSource()==closeFrame2){
            if(secondFrame!=null){
                if(secondFrame.isShowing()){
                    secondFrame.dispose();
                    secondFrame=null;
                }
            }
        }
        if(ae.getSource()==closeFrame3){
            if(thirdFrame!=null){
                if(thirdFrame.isShowing()){
                    thirdFrame.dispose();
                    thirdFrame=null;
                }
            }
        }
    }
   
    public void createFirstFrame(){
        firstFrame=new JFrame();
        Container canvas=firstFrame.getContentPane();
        canvas.setLayout(null);
       
        strLabel=new JLabel("First Frame");
       
        strLabel.setBounds(50,25,150,25);
       
        canvas.add(strLabel);
       
        firstFrame.setBounds(550,200,250,100);
        firstFrame.setDefaultCloseOperation(firstFrame.EXIT_ON_CLOSE);
        firstFrame.setVisible(true);
    }
   
    public void createSecondFrame(){
        secondFrame=new JFrame();
        Container canvas=secondFrame.getContentPane();
        canvas.setLayout(null);
       
        strLabel=new JLabel("Second Frame");
       
        strLabel.setBounds(50,25,150,25);
       
        canvas.add(strLabel);
       
        secondFrame.setBounds(550,310,250,100);
        secondFrame.setDefaultCloseOperation(secondFrame.EXIT_ON_CLOSE);
        secondFrame.setVisible(true);
    }
   
    public void createThirdFrame(){
        thirdFrame=new JFrame();
        Container canvas=thirdFrame.getContentPane();
        canvas.setLayout(null);
       
        strLabel=new JLabel("Third Frame");
       
        strLabel.setBounds(50,25,150,25);
       
        canvas.add(strLabel);
       
        thirdFrame.setBounds(550,420,250,100);
        thirdFrame.setDefaultCloseOperation(thirdFrame.EXIT_ON_CLOSE);
        thirdFrame.setVisible(true);
    }
    public static void main(String []args){
        MultipleFrame1 mf=new MultipleFrame1();       
    }
   
}

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

No comments:

Post a Comment