2013-06-27 84 views
-1

所以這是我的代碼:關閉內部框架

public class DesktopFrame extends JFrame{ 
    private JDesktopPane theDesktop; 
    private JInternalFrame login; 
    private JMenuBar bar; 
    private JMenu fileMenu; 
    private JMenuItem newLoginFrame; 
    private LoginPanel panel; 

    // set up GUI 
    public DesktopFrame(){ 
     super("Application"); 
     bar = new JMenuBar(); // create menu bar 
     bar.setBackground(new Color(255,215,0)); 
     fileMenu = new JMenu("File"); // create Add menu 
     fileMenu.setBackground(new Color(255,215,0)); 
     newLoginFrame = new JMenuItem("Login"); 
     newLoginFrame.setBackground(new Color(255,215,0)); 
     fileMenu.add(newLoginFrame); // add new frame item to Add menu 
     bar.add(fileMenu); // add Add menu to menu bar 
     setJMenuBar(bar); // set menu bar for this application 
     theDesktop = new JDesktopPane(); // create desktop pane 
     theDesktop.setBackground(Color.BLUE); 
     add(theDesktop); // add desktop pane to frame 
     // set up listener for newLoginFrame menu item 
     newLoginFrame.addActionListener(new ActionListener(){ // anonymous inner class 
      // display new internal window 
      public void actionPerformed(ActionEvent event){ 
       login = new JInternalFrame("Member Login", false, false, false, false); 
       panel = new LoginPanel(); 
       login.add(panel, BorderLayout.CENTER); // add panel 
       login.setSize(375,300); 
       login.setLocation(20,20); 
       theDesktop.add(login); // attach internal frame 
       login.setVisible(true); // show internal frame 
      } // end method actionPerformed 
     } // end anonymous inner class); // end call to addActionListener 
    } // end DesktopFrame constructor 

    public void getValid(){ 
     if(panel.getValid() == true){ 
      try{ 
       login.setClosed(true); 
      } 
      catch(PropertyVetoException p){  
      } 
     } 
    } 
} // end class DesktopFrame 

在這個文件中,也有另一種類「LoginPanel」負責處理所有的登錄框。如果用戶名/密碼起作用,它會創建一個布爾變量「valid」,這是真的。我用「panel.getValid()」調用它。正如你所看到的,當「有效」爲真時,目的是退出登錄框架。這可能嗎?人們推薦什麼?現在,使用「setClosed」它將退出整個框架,而不僅僅是內部「登錄」框架。我不知道爲什麼

+2

1)不要延長框架或其他頂層容器。而是創建並使用一個實例。 2)對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 3)爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

1

我認爲更好的主意是創建用於登錄的JDialog(對於JDialog文檔,請參閱this link),以與內部框架類似的方式使用它。在JDialog上成功登錄後調用dispose()。

This question and answers應該是有幫助的。

+0

天才謝謝 – user2518777

+0

@ user2518777如果這是你的prefferred解決方案,你可以接受回答 – 1ac0

+0

+1 ... @ user2518777另見[this](http://stackoverflow.com/questions/13055107/joptionpane-check-user-input- and-prevent-from-closing-until-conditions-are-met/13055405#13055405)使用'JDialog'進行輸入驗證的答案。 –