-3

I do not know why my code is not working properly. When I am trying to open window(BazaDanych) only window2(ZmienBaze) appears and I dont know why. Window(BazaDanych) does not open at all. Maybe could you help me.

class GlowneMenu extends JFrame implements ActionListener  
{  

  public GlowneMenu()  
  {  
    setTitle("Menu");  
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
    ...<not important just Menu stuff>
    JMenu info = new JMenu("Info");
    ...<not important just JMenu and label stuff>

  }  

  public void actionPerformed(ActionEvent e){  

    new BazaDanych().setVisible(true);  
    this.dispose(); 

    new ZmienBaza().setVisible(true);  
    this.dispose();

  }  

  public static void main( String[] args){new GlowneMenu().setVisible(true);}  
}  
class BazaDanych extends JFrame{ 

  public BazaDanych()  
  {  
    setTitle("Baza danych");  
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    setLocation(400,100);  
    ...<not importent just this window stuff>
  }  

} 
class ZmienBaza extends JFrame{ 

      public ZmienBaza()  
      {  
        setTitle("Zmiana Bazy");  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        setLocation(400,100);  
        ...<not importent just this window stuff>

      }  
}

I figured out that when I delete this :* my code will work properly but only to one window. How can I add more windows in these way??

 *new ZmienBaza().setVisible(true);
            this.dispose();
Lukas
  • 69
  • 1
  • 10
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Dec 14 '13 at 13:03

1 Answers1

0

the following Code should do the Job:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GlowneMenu extends JFrame{  

    public GlowneMenu(){  
        JButton button = new JButton("Show");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionMethod();
            }
        });
        setTitle("Menu");  
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        getContentPane().add(button);
        setSize(new Dimension(300,200));
        setPreferredSize(new Dimension(300,200));
    }  


    protected void actionMethod(){
        new BazaDanych().setVisible(true);
        //this.dispose();
        new ZmienBaza().setVisible(true);
        this.dispose();
    }  

    public static void main( String[] args){
        new GlowneMenu().setVisible(true);
    }

    class BazaDanych extends JFrame{
        public BazaDanych(){  
            setTitle("Baza danych");  
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            setLocation(400,100);  
            setSize(new Dimension(300,200));
            setPreferredSize(new Dimension(300,200));
        }  

    } 
     class ZmienBaza extends JFrame{
        public ZmienBaza(){  
            setTitle("Zmiana Bazy");  
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            setLocation(100,400);  
            setSize(new Dimension(300,200));
        }  
    }

}  
Rami.Q
  • 2,486
  • 2
  • 19
  • 30
  • Ok thank you. Mhh I rather thought that I have 2 windows and 2 paths and when I click path to window appear window2(not window1) and when I click path to window2 appear window2. – Lukas Dec 14 '13 at 13:56