2012-12-10 2 views
1

내 스윙 채팅 응용 프로그램에서 사용자를 로그 아웃하는 데 사용되는 로그 아웃 버튼이 있는데 제대로 작동합니다. 이제 스윙 응용 프로그램 창을 닫을 때 사용자를 로그 아웃해야합니다.응용 프로그램이 닫힐 때 메소드 호출

자바 스크립트를 사용하여 브라우저를 닫을 때 웹 응용 프로그램에서이 작업을 수행했지만 지금은 스윙 응용 프로그램에서이 작업을 수행해야합니다.

어떻게하면됩니까?

답변

6
  • 전화 JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
  • 프레임에 WindowListener를 추가합니다.
  • 청취자의 적절한 메소드를 대체하여 닫기 메소드를 호출 한 다음 프레임을 보이지 않게 설정하고 처리하십시오.

E.G. 귀하의 지원에 대한

Dialog, checking for frame exit

import java.awt.*; 
import java.awt.event.*; 
import java.net.URI; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

class CheckExit { 

    public static void doSomething() { 
     try { 
      // do something irritating.. 
      URI uri = new URI(
        "http://stackoverflow.com/users/418556/andrew-thompson"); 
      Desktop.getDesktop().browse(uri); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) throws Exception { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       // the GUI as seen by the user (without frame) 
       JPanel gui = new JPanel(new BorderLayout()); 
       gui.setPreferredSize(new Dimension(400, 100)); 
       gui.setBackground(Color.WHITE); 

       final JFrame f = new JFrame("Demo"); 
       f.setLocationByPlatform(true); 
       f.add(gui); 

       // Tell the frame to 'do nothing'. 
       f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 

       WindowListener listener = new WindowAdapter() { 

        @Override 
        public void windowClosing(WindowEvent we) { 
         int result = JOptionPane.showConfirmDialog(
           f, "Close the application"); 
         if (result==JOptionPane.OK_OPTION) { 
          doSomething(); 
          f.setVisible(false); 
          f.dispose(); 
         } 
        } 
       }; 
       f.addWindowListener(listener); 

       // ensures the frame is the minimum size it needs to be 
       // in order display the components within it 
       f.pack(); 
       // should be done last, to avoid flickering, moving, 
       // resizing artifacts. 
       f.setVisible(true); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

지원해 주셔서 감사합니다 ... 창문을 닫으려고해도 작동했지만 확인 상자가 필요합니다 ... –

+0

답장을 보내 주셔서 감사 드리며 잘 작동했지만 취소 버튼을 누르면 내 애플리케이션이 닫혔습니다. . 취소 버튼을 누르면 응용 프로그램 창을 닫을 필요가 없습니다 ... –

+0

감사합니다. 예상 결과를 얻었습니다 .... 고맙습니다 ... –

1

JFrame의 윈도우 이벤트를 사용하면 필요한 메소드 (예 : windowclosed();)가 있습니다. ,

당신이

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 

을 말할 수 있지만

이 당신이 방법의 windowClosing에 우선 X (닫기 버튼)을 눌러 경우의 WindowListener는 여전히 작동 다음의 WindowListener이

편집 오기 ' 이 코드를 사용하여

이 작업을 수행 할 것입니다

+0

감사합니다 ... 그것은 일을하지만 난 내 편집을보고 ... –

+0

을 창을 닫으려고 할 때 또한 확인 상자가 필요합니다, 답장을 보내 – SomeJavaGuy

+0

감사합니다) ... 하지만 취소 또는 아니오 버튼을 누르면 애플리케이션 애플리케이션을 닫을 필요가 없습니다. Pls help me .. –

관련 문제