2013-08-15 5 views
4

이 코드 세그먼트는 JFrame을 만들 때 호출되며 dispose() 행에 도달하면 닫히지 않습니다. 다른 JFrame이 열리기 때문에이 블록으로 들어간다는 것을 알고 있습니다. 유일한 문제는 닫히지 않는다는 것입니다. 왜 그런지 알아?JFrame이 닫히지 않을 때

public LogIn(String title) 
{ 
    super(title); 
    checker = new Open(""); 
    deserializeOpen(); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(300, 300); 
    getContentPane().setLayout(new BorderLayout()); 
    getContentPane().setBackground(Color.orange); 
    Login = new JButton("Login"); 
    Create = new JButton("New Profile"); 
    Login.addActionListener(this); 
    Create.addActionListener(this); 
    buttons = new JPanel(); 
    buttons.setBackground(Color.orange); 
    buttons.setLayout(new GridLayout(0,2)); 
    buttons.add(Login); 
    buttons.add(Create); 
    Title = new JLabel("Scrambler"); 
    Title.setFont(new Font("Times New Roman", Font.BOLD, 24)); 
    Name = new JTextField(4); 
    name = new JLabel("Name:"); 
    password = new JPasswordField(4); 
    pass = new JLabel("Password:"); 
    Text = new JPanel(); 
    Text.setLayout(new GridLayout(6,0)); 
    Text.setBackground(Color.orange); 
    Text.add(Title); 
    Text.add(name); 
    Text.add(Name); 
    Text.add(pass); 
    Text.add(password); 
    getContentPane().add(Text, BorderLayout.CENTER); 
    getContentPane().add(buttons, BorderLayout.SOUTH); 
    show(); 
} 
    public void deserializeOpen() 
{ 
    try 
    { 
     FileInputStream door = null; 
     try 
     { 
      door = new FileInputStream("Check.ser"); 
     } 
     catch (FileNotFoundException e) 
     { 
      new Activator(); 
      dispose(); 
     } 
     if(door!=null) 
     { 
     ObjectInputStream reader = new ObjectInputStream(door); 
     checker = (Open) reader.readObject(); 
     } 
    } 
    catch (IOException e){e.printStackTrace();} 
    catch (ClassNotFoundException e){e.printStackTrace();}   
} 

이러한 코드의 두 세그먼트있다가, 몸은 내가 처분() 라인이라고 확신 문제

의 원인이되는 하나의 첫 번째 부분과 직렬화 하나입니다 내가 dispose()에 대해 System.out.print() 권한을 두어 인쇄했기 때문에 도달했습니다.

+1

뭔가가 EDT를 차단하거나 프레임의 'DefaultCloseOperation'이'DO_NOTHING'으로 설정되었습니다 ... – MadProgrammer

+0

그냥 확인합니다. DefaultCloseOpeation은 EXIT로 설정됩니다. EDT를 막는 무언가를 고치려면 어떻게해야할까요? 나는 완전한 초보자입니다. – Achi113s

+1

코드 스 니펫에 따라 알기가 어렵습니다. [Concurrency in Swing] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – MadProgrammer

답변

0

JFrame 하위 클래스의 생성자에서 deserializeOpen()을 호출하여 프레임을 처리합니다 아직 표시되지 않았으므로) 프레임을 여는 show()으로 전화하십시오. 따라서 개봉을 시도했을 수도 있습니다. 코드 대신 코드를 열면이 열립니다.

덧붙여서 : show() 메서드는 JDK1.5부터 사용되지 않으므로 대신 setVisible(true)을 사용해야합니다. 생성자에서

public void deserializeOpen() throws Exception { ... } 

: 나는, 그 대신 개방의 프레임을 열 것인지 여부를 결정할 수있는 방법과 폐쇄를 당신이 deserializeOpen()에 예외를 전파하는 것이 좋습니다와 외부를 잡을 것

try { 
    deserializeOpen(); 
    setVisible(true); 
} catch(Exception e) { 
    e.printStacktrace(); // or any other error handling 
} 
+1

정말 고맙습니다. 그리고 나는 그것을 몰랐다. 나는 지금 show()를 사용하지 않을 것이다. – Achi113s

관련 문제