2016-11-13 254 views
0

因此,這是問題所在。我正在創建一個使用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)。 –

回答