Jframe出了问题[我尝试打开window1但打开window2]

时间:2013-12-14 13:02:58

标签: java swing jframe

我不知道为什么我的代码无法正常工作。当我试图打开窗口(BazaDanych)时,只有window2(ZmienBaze)出现,我不知道为什么。窗口(BazaDanych)根本不打开。也许你可以帮助我。

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>

      }  
}

我发现当我删除它时:*我的代码将正常运行,但只能运行一个窗口。如何以这种方式添加更多窗口??

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

1 个答案:

答案 0 :(得分:0)

以下代码应该完成工作:

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));
        }  
    }

}