使用dispose()方法关闭Java中的JFrame时遇到问题

问题描述:

因此,这是问题所在。我正在创建一个使用JFrame的程序。基本上它所做的是打开一个窗口,询问你打开哪个窗口。该程序由多个GUI类和多个Client类组成,负责为每个GUI在打开时创建一个新窗口。所以当MainClient类被加载时,它会创建一个窗口来保存MainGUI类。从那里从ComboBox中选择一个选项并单击“continue”应该启动另一个客户端类打开另一个JFrame。这一切都工作正常,除了我不能为我的生活找出为什么我不能使用.dispose()方法来删除旧窗口。我所能做的就是使用setVisible(false)方法将其清除,但它仍然在我新窗口的背景中挂起。每当我尝试直接在setVisible()方法之后使用dispose方法时,出现此错误“无法找到符号 - 方法dispose()” 如果有人知道为什么会发生这种情况,我希望对此有所帮助!下面是导致该错误我MainGUI类的代码(该错误是接近底部注释出来,并用星号包围):使用dispose()方法关闭Java中的JFrame时遇到问题

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(); 
     } 


    } 
    } 
} 
+0

'gui'是'的JPanel ',不是'JFrame','JPanel'没有'dispose()'方法。 ['JPanel'文档](https://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html)。 –

MainGUI类扩展JPanel,不JFrame。如果你想出售持有MainGUI面板的实例的框架,你可以使用:

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

它也好像你正在使用一个以上的JFrame,看The Use of Multiple JFrames: Good or Bad Practice?