2016-11-13 1 views
0

여기에 문제가 있습니다. 나는 JFrame을 사용하는 프로그램을 만들고있다. 본질적으로 그것이하는 일은 열려고하는 창을 묻는 창이 열립니다. 이 프로그램은 여러 개의 GUI 클래스와 각 GUI가 열릴 때 새 창을 만드는 여러 개의 Client 클래스로 구성됩니다. 따라서 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' 문서] (https://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html). –

답변

0

귀하의 MainGUI 클래스 JPanel하지 JFrame 확장합니다. 당신이 MainGUI 패널의 인스턴스를 유지 프레임을 폐기하고자하는 경우, 당신은 사용할 수 있습니다

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

을 하나 이상의 JFrameThe Use of Multiple JFrames: Good or Bad Practice?

관련 문제