无法使用dispose()方法关闭Java中的JFrame

时间:2016-11-13 21:56:16

标签: java jframe dispose bluej

所以这就是问题所在。我正在创建一个使用JFrame的程序。它的基本功能是打开一个窗口,询问您打开哪个窗口。该程序由多个GUI类和多个Client类组成,负责在每个GUI打开时为其创建一个新窗口。因此,当加载MainClient类时,它会创建一个窗口来保存MainGUI类。从那里选择ComboBox中的一个选项,然后单击"继续"应该启动另一个客户端类打开另一个JFrame。这一切都很好,除了我不能为我的生活弄清楚为什么我不能使用.dispose()方法删除旧窗口。我所能做的就是使用setVisible(false)方法清除它,但它仍然在我的新窗口的背景中挂起。每当我尝试在setVisible()方法之后直接使用dispose方法时,我得到这个错误"找不到符号 - 方法dispose()" 如果有人知道为什么会这样,我会对此有所帮助!这是导致错误的我的MainGUI类的代码(错误在底部附近被注释掉并被星号包围):

import java.awt.*;
import javax.swing.*;
import java.awt.event.*; 


public class MainGUI extends JPanel
{

  protected JLabel topLabel;
  protected JButton        continueButton;// To continue to the next form
  private MainHandler handler = new MainHandler(this); // An instance of the inner event handler
  protected final String[] OPTIONS = {"Search/Delete Employee Records","Insert New Employee",
  "Insert New Manager","Retrieve Department Metadata"};
  protected JComboBox       optionsCombo; //Combo box to select options from

  protected InsManGUI insGUI;

  public MainGUI ()
  {
      setLayout (new FlowLayout());

      topLabel = new JLabel("Please select which action you wish to perform");
      topLabel.setHorizontalAlignment(JLabel.CENTER);

      continueButton = new JButton("Continue");
      continueButton.setHorizontalAlignment(JLabel.CENTER);

      optionsCombo = new JComboBox(OPTIONS);


      add(topLabel);

      add(optionsCombo);

      add(continueButton);

      continueButton.addActionListener(new MainHandler(this));
      optionsCombo.addActionListener(new MainHandler(this));







  }    
}

// Inner eventhandler class to compute which form to open once the Continue button is clicked.
class MainHandler implements ActionListener
{
//Private varible to hold an instance of the MainGUI class
private MainGUI gui;




 public MainHandler(MainGUI gui)
   {
       //Set the private GUI varible to a particular GUI
   this.gui = gui;

   EmployeesDB.connect();

   }  


    public void actionPerformed(ActionEvent e)
   {
   if(e.getSource() == gui.continueButton)
   { 
       if(gui.optionsCombo.getSelectedItem() == "Insert New Manager")
       {
           gui.setVisible(false);

           InsManClient man = new InsManClient();

           //gui.dispose();*******************THIS LINE WON't //COMPILE*************************


       }

       if(gui.optionsCombo.getSelectedItem() == "Insert New Employee")
       {
           gui.setVisible(false);
           InsEmpClient emp = new InsEmpClient();
       }    

       if(gui.optionsCombo.getSelectedItem() == "Search/Delete Employee Records")
       {
           gui.setVisible(false);
           SearchClient ser = new SearchClient();
       }  

       if(gui.optionsCombo.getSelectedItem() == "Retrieve Department     Metadata")
       {
           gui.setVisible(false);
           MetaDataClient met = new MetaDataClient();
       }  


   }
   }
}

1 个答案:

答案 0 :(得分:0)

您的MainGUI课程延伸JPanel,而不是JFrame。如果您希望丢弃包含MainGUI面板实例的框架,您可以使用:

((JFrame) SwingUtilities.getWindowAncestor(gui)).dispose();

您似乎使用了多个JFrame,请参阅The Use of Multiple JFrames: Good or Bad Practice?