2013-06-27 82 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'进行输入验证的答案。 –