【问题标题】:Close Internal Frame after a certain task完成特定任务后关闭内部框架
【发布时间】:2013-06-27 18:35:45
【问题描述】:

这是我的代码:

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”处理所有登录框架。如果用户名/密码有效,它会创建一个布尔变量“有效”,它是真的。我用“panel.getValid()”调用它。如您所见,目的是在“有效”为真时退出登录框架。这可能吗?人们推荐什么?现在,使用“setClosed”它正在退出整个框架,而不仅仅是内部的“登录”框架。不知道为什么

【问题讨论】:

  • 1) 不要扩展框架或其他顶级容器。而是创建并使用一个实例。 2) 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。 3) 为了尽快获得更好的帮助,请发帖SSCCE

标签: java swing user-interface jinternalframe


【解决方案1】:

我认为更好的想法是创建 JDialog 用于登录(对于 JDialog 文档,请参阅this link),使用它的方式与使用内部框架类似。在 JDialog 上成功登录后调用 dispose()。

This question and answers 应该会有所帮助。

【讨论】:

  • @user2518777 如果这是您的首选解决方案,您可以接受答案
  • +1 ... @user2518777 另请参阅this 答案,它使用JDialog 进行输入验证。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 2015-11-05
  • 2016-06-02
相关资源
最近更新 更多