【问题标题】:Closing jFrame after clicking JButton单击 JButton 后关闭 jFrame
【发布时间】:2013-09-26 16:43:59
【问题描述】:

我在 NetBeans 中设计了两个 JFrame。

当我单击“规则”按钮(即放置在 JFrame1 上)时,它会打开第二个 JFrame(但 JFrame2 在 JFrame1 的窗口上打开,这就是我不想要的)。 在第二个 JFrame 中有一个“关闭”按钮。但是当我点击这个按钮时,我希望 JFrame1 被打开并且它也在工作,但是 JFrame2 实际上并没有关闭并且 JFrame1 出现在 JFrame2 上。

简而言之,主要形式是JFrame1。当我从 JFrame1 单击“规则”按钮时,它会在 JFrame1 上打开 JFrame2,在 JFrame2 中单击主窗体(即 JFrame1)时会出现一个“关闭”按钮,但它是通过 JFrame2 启动的。

场景是 JFframe1 -> JFrame2 -> JFrame1

现在我的问题是点击“规则”按钮后,JFrame1 应该关闭,JFrame2 显示在屏幕上,反之亦然。

【问题讨论】:

  • 对要关闭的框架使用dispose() 方法。但不建议使用多个JFrames,而是查看多个对话框或内部框架。
  • 您也不妨考虑The Use of Multiple JFrames, Good/Bad Practice?。使用框架进行表单切换本质上是非常糟糕的设计。我会考虑使用JPanels 作为应用程序的主要容器,并使用JTabbedPanes 或CardLayout 允许用户在它们之间切换 - 恕我直言...

标签: java swing jbutton


【解决方案1】:

假设你的按钮有一个actionListener,点击“规则按钮”后放入:

      JFrame1.dispose();  //Remove JFrame 1
      JFrame2.setVisible(true) //Show other frame

然后将它们反转为相反的反应

【讨论】:

    【解决方案2】:

    这样的东西应该在创建 JFrame2 的构造函数或方法上:

    btnCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //call another method in the same class which will close this Jframe
            CloseFrame();
        }
    });
    

    应该关闭JFrame2的方法

    public void CloseFrame(){
        super.dispose();
    }
    

    【讨论】:

    • 不只是给出代码,还试着解释一下为什么这段代码需要在构造函数/方法上。
    • 对不起,我忘了解释 CloseFrame 方法应该在哪里。现在解释清楚了。
    【解决方案3】:

    无论如何,我都不是专家,但是,我也遇到了这个问题。如果您将第二个 JFrame 设置为隐藏,当您点击“取消”时,它将关闭第二个 JFrame。

    //this is the code for the "cancel" button action listener 
    public void actionPerformed(ActionEvent e) {
        setVisible(false);//hides the second JFrame and returns to the primary
    

    【讨论】:

      【解决方案4】:

      这对我有用(Frame1 称为 RegScreenFrame2 称为 MainScreen):

      RegScreen.this.setVisible(false);
      
      new MainScreen().setVisible(true);
      

      希望这会有所帮助 :) Regscreen 是启动时打开的原始框架。

      【讨论】:

        【解决方案5】:

        如果这不起作用,试试这个

        JFrame1.dispose();  //Remove JFrame 1
              JFrame2.setVisible(true) //Show other frame
        JFrame2.setVisible(true);
        this.dispose();
        

        【讨论】:

          【解决方案6】:
          1. 拥有一个带有 main() 方法的 MainClass。
          2. 让具有 main() 方法的 MainClass 封装您的 JFrame1 和 JFrame2 引用变量。除非您有特定原因,否则不要让 JFrame1 或 JFrame2 包含 main()。
          3. 在其中一个 JFrame 对象中单击某些内容后,实例化/使另一个 JFrame 对象可见并通过您的 MainProgram.JFrame 对象方法处理自身。

          例子:

              //btn event inside 1st JFrame/window
              private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                   MainProgram.openResultsForm();  //MainProgram opens 2nd window
                   MainProgram.queryEntryForm.dispose();   //MainProgam closes this,
                                                           //the 1st window
              }
          

          【讨论】:

            【解决方案7】:

            好吧,如果你已经有一个 actionListener,你应该添加这个:

            JFrame1.dispose(); // This will close the frame

            JFrame1 是框架的名称。 如果您想打开另一个框架,请添加:

            JFrame2.setVisible(true); // This will put the other frame visible

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-06-27
              • 2011-03-08
              • 1970-01-01
              • 1970-01-01
              • 2013-12-09
              • 2011-01-22
              • 1970-01-01
              • 2022-01-18
              相关资源
              最近更新 更多