2013-12-14 53 views
-3

我不知道爲什麼我的代碼無法正常工作。當我試圖打開窗口(BazaDanych)時,只有window2(ZmienBaze)出現,我不知道爲什麼。 Window(BazaDanych)根本不開放。也許你能幫助我嗎?Jframe的東西壞了[我嘗試打開窗口1,但打開窗口2]

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

1)爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 2)請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/a/9554657/418556) –

回答

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

} 
+0

您還應該指出你的固定內容 –

+0

好的,謝謝。 Mhh我寧願以爲我有2個窗口和2個路徑,當我點擊窗口的路徑出現window2(不是window1),當我點擊window2的路徑時出現window2。 – Lukas

+0

如果這解決了您的問題,請將其標記爲已接受答案! –