2012-03-01 2 views
1

저는이 작업을 얼마 동안 해왔으며 지금 당장 도움을 청합니다. actionPerformed 메서드에서 닫을 텍스트 입력 필드를 포함하는 JFrame 얻으려면 노력하고있어,하지만 아무것도 작동하도록 얻을 수 없습니다. JFrame.dispose는 올바른 Jframe에 액세스 할 수 없으며 setVisible (false)도 완전히 잘못하지 않는 한 똑같이 쓸모가 없습니다.다른 메서드에서 다른 JFrame 닫기

//halp 
import javax.swing.*; 
import java.awt.*; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 

class PersonInput extends JPanel 
       implements ActionListener { 

//Fields for data entry 
private JFormattedTextField firstField, lastField, dateField; 

public String x[] = new String[3]; 

public PersonInput() { 

    //Values for the fields 
    String first = "First Name"; 
    String last = "Last Name"; 
    String date = "MM/DD/YYYY"; 


    //Create the text fields and set them up. 
    firstField = new JFormattedTextField(); 
    firstField.setValue(new String(first)); 

    lastField = new JFormattedTextField(); 
    lastField.setValue(new String(last)); 

    dateField = new JFormattedTextField(); 
    dateField.setValue(new String(date)); 
    dateField.setColumns(10); 

    JButton ok = new JButton("OK"); 
    ok.setVerticalTextPosition(AbstractButton.BOTTOM); 
    ok.setHorizontalTextPosition(AbstractButton.CENTER); 
    ok.setActionCommand("ok"); 
    ok.addActionListener(this); 
    ok.setToolTipText("Confirms user input and continues with the program."); 

    JPanel buttons = new JPanel(new GridLayout(0,1)); 
    buttons.add(ok); 


    //Layout the text fields in a panel. 
    JPanel fieldPane = new JPanel(new GridLayout(0,1)); 
    fieldPane.add(firstField); 
    fieldPane.add(lastField); 
    fieldPane.add(dateField); 

    //Put the panels in this panel, labels on left, 
    //text fields on right. 
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 
    add(fieldPane, BorderLayout.CENTER); 
    add(buttons, BorderLayout.LINE_END); 


} 

public void actionPerformed(ActionEvent e) { 
    if ("ok".equals(e.getActionCommand())) 
    { 
      JFrame frame1 = new JFrame("People Sorter"); 

     x[0] = firstField.getText(); 
     x[1] = lastField.getText(); 
     x[2] = dateField.getText(); 

     JOptionPane.showMessageDialog(frame1, "Person has been added."); 
     dispPerson(); 
     frame.setVisible(false); 
    } 
} 

public void dispPerson() 
{ 
    System.out.println(x[0] + x[1] + x[2]); 

} 
public static void createAndShowGUI() { 
    //Create and set up the window. 
    JFrame frame = new JFrame("Person Input"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //Add contents to the window. 
    frame.add(new PersonInput()); 

    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
} 

public static void main(String[] args) { 

    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      //Turn off metal's use of bold fonts 
     UIManager.put("swing.boldMetal", Boolean.FALSE); 
      createAndShowGUI(); 
     } 
    }); 
} 
} 

누구나 아이디어가 있다면 나는 모든 귀입니다. 나는 하루 종일 강조했다. 많은 시간을 빌려 줘서 고마워!

편집 : 그냥 명확히하기 위해, 내가 닫으려고하는 프레임은 createAndShowGUI 메소드에서 인스턴스화 된 프레임입니다.

+2

는 신경 쓰지 기억 나는 발견 SwingUtilities.getWindowAncestor (이) .dispose를 사용하여(); 내 actionPerformed 메서드에서 활성 JFrame을 닫을 수 있습니다. –

+1

좋은 직장 :-) 자신의 질문에 대답 해보십시오. – kleopatra

답변

0

정적 인 콘텐츠와 비 정적 인 콘텐츠를 병합하려고하는 것 같습니다. 짧은 설명을 위해 정적 컨텐츠는 해당 클래스의 인스턴스 (오브젝트)를 작성할 필요없이 참조 될 수 있습니다. 어느 createAndShowGUI 호출 할 수 있다는 것을 의미한다 : 다른 정적 메소드 내부

(주 같은) 클래스 참조 PersonInput.createAndShowGUI() 에서 또는 개체에서, 그러나 그 방법 또는 속성은 항상 같은 정적 속성이 공유 될 것입니다 .

문제점을 해결하는 두 가지 방법을 제안 할 수 있습니다.

하나 PersonInput

//halp 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
class PersonInput extends JPanel 
       implements ActionListener { 
//Fields for data entry 
private JFormattedTextField firstField, lastField, dateField; 

public String x[] = new String[3]; 


JFrame frame; 
public PersonInput(JFrame frame) { 
this.frame = frame; 
//the rest of your code 
} 

다른 방법에있어서, 외부 프레임 객체가 그것이 정지 선언되는 대상 프레임을 전달한다.

static JFrame frame = new JFrame("Person Input");; 
public static void createAndShowGUI() { 
    //Create and set up the window. 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //Add contents to the window. 
    frame.add(new PersonInput()); 
    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
} 

, static variable cannot be referenced from a static context