簡體   English   中英

使用dispose()方法關閉Java中的JFrame時遇到問題

[英]Trouble using the dispose() method to close a JFrame in Java

這就是問題所在。 我正在創建一個使用JFrame的程序。 本質上,它的作用是打開一個窗口,詢問您要打開哪個窗口。 該程序由多個GUI類和多個Client類組成,負責在每個GUI打開時為其創建新窗口。 因此,在加載MainClient類時,它將創建一個窗口來保存MainGUI類。 從那里,從ComboBox中選擇一個選項,然后單擊“ continue”,應啟動另一個客戶端類,打開另一個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();
       }  


   }
   }
}

您的MainGUI類擴展了JPanel ,而不擴展了JFrame 如果您希望MainGUI面板實例的框架,則可以使用:

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

好像您正在使用多個JFrame ,請參閱《使用多個JFrame:好的還是不好的做法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM