2016-11-13 18 views
0

ここに問題があります。私はJFrameを使用するプログラムを作成しています。基本的には、開いているウィンドウが開き、開くウィンドウが表示されます。このプログラムは、複数のGUIクラスと、各GUIが開かれたときに新しいウィンドウを作成する複数のクライアントクラスで構成されています。したがって、MainClientクラスがロードされると、MainGUIクラスを保持するウィンドウが作成されます。 ComboBoxからオプションを選択してから、「続行」をクリックすると、別の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' documentation](https://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html)。 –

答えて

0

あなたMainGUIクラスがJPanel、ないJFrameを拡張します。あなたがMainGUIパネルのインスタンスを保持するフレームを配置したい場合は、使用することができます。

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

を複数のJFrameを使用しているようにそれはまたようで、The Use of Multiple JFrames: Good or Bad Practice?

関連する問題