0

I am making a project. I need about 5 JFrame but all are interrelated.

this is my main frame code

public void mframe(boolean b) //function for main JFrame...
{
    JFrame.setDefaultLookAndFeelDecorated(true);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    main m=new main();                 // main constructor call ...
    JFrame frame = new JFrame("       Demo Project          submited by:  Ankit    ");
    frame.setJMenuBar(m.menuBar);   // set JMenueBar in JFrame frame...

    try {   // try to add a background pic and icon
        frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("pics/index.jpg")))));

        frame.setIconImage(ImageIO.read(new File("pics/girl.png")));

        } catch (IOException e) {
        e.printStackTrace();
    }

    frame.setLayout(null);
    frame.setOpacity(0.88f);
    frame.setSize(510,420);
    frame.setResizable(false);
    frame.setVisible(b); 

if (!gd.isWindowTranslucencySupported(TRANSLUCENT))
    {

    frame.setJMenuBar(m.menuBar); 
    frame.setVisible(b);
    frame.setSize(500,400);

    }
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public static void main(String agr[])
{

main m = new main();
m.mframe(true);

}

public void actionPerformed(ActionEvent e)
{

    if(e.getSource()==exit)
    {
            System.exit(0);

    }

    else if(e.getSource()==Ankit)
    {
            Contact c = new Contact();
            c.cframe(true);

            // dispose();

            main m1 = new main();
            m1.mframe(false);

    }


}

and this is my second frame , contact frame code

public void cframe(boolean b)
{
    JFrame.setDefaultLookAndFeelDecorated(true);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    Contact co=new Contact();

    JFrame cframe = new JFrame("          Ankit Ujlayan  (b.tech IT)      ");
    try {
        cframe.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("pics/cont.jpg")))));

        cframe.setIconImage(ImageIO.read(new File("pics/girl.png")));

        } catch (IOException e) {
        e.printStackTrace();
    }


    cframe.setLayout(null);
    cframe.setOpacity(0.87f);
    cframe.setSize(510,420);

    cframe.setJMenuBar(co.cmenu);

    cframe.setVisible(b);
    cframe.setResizable(false);
    cframe.add(co.p1);
    cframe.add(co.p2);
    cframe.add(co.p3);


    //cframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String arg[])
{

    //Contact c = new Contact();
    //c.cframe(false);

}
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==close)
    {

        System.exit(0);
    }

    if(e.getSource()==back)
    {
    main m=new main();
    m.mframe(true);

    //dispose();

    Contact c1= new Contact();
    c1.cframe(false);  // I call cframe by passing Boolean "false" 


    }
} 

I have try to dispose it many time and also google for it but I fail to dispose it .....

Ankit
  • 3
  • 3
  • 3
    See [this link](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) regarding the use of multiple `JFrames`. Also, do not run Java Swing GUI code outside the [Event Dispatch Thread (EDT)](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html). – David Yee Jul 17 '14 at 09:51
  • 1
    `cframe.setLayout(null);` Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. – Andrew Thompson Jul 17 '14 at 09:53
  • If I make my main JFrame like this main m=new main(); m.setSize(445,460); m.setLayout(null); m.show(); then it is working as I want but Opacity and Image doesn't set in my frame..... is there any other way to do this – Ankit Jul 17 '14 at 10:12

1 Answers1

0

As I am try, How can I dispose my current Jframe when I open a 2nd one by ActionListener. Now my code is working properly as I want. I think it will be helpfull to make a decorated JFrame.

public void cframe(boolean b) {

    Contact co=new Contact();

    JFrame.setDefaultLookAndFeelDecorated(true);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();


    JFrame cframe = new Contact();        // This is the way to add Contact(It have my Panel, Lable , Menu etc) Class in JFrame. 
    try {
        cframe.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("pics/cont.jpg")))));

        cframe.setIconImage(ImageIO.read(new File("pics/girl.png")));

        } catch (IOException e) {
        e.printStackTrace();
    }


    cframe.setLayout(null);
    cframe.setOpacity(0.87f);
    cframe.setSize(510,420);
    cframe.setVisible(b);
    cframe.setResizable(false);
    cframe.add(co.p1);
    cframe.add(co.p2);
    cframe.add(co.p3);

    if (!gd.isWindowTranslucencySupported(TRANSLUCENT))
    {

    cframe.setVisible(b);
    cframe.setSize(510,420);

    }

    //cframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String arg[])
{

    Contact c = new Contact();
    c.cframe(false);

}public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==close)
    {

        System.exit(0);
    }

    if(e.getSource()==back)
    {
    main m=new main();
    m.mframe(true);

    //Contact c1= new Contact();
    //c1.cframe(false);

    dispose();
    }
}

Thank you .... :-)

Ankit
  • 3
  • 3