2012-07-20 3 views
1

NetBeans 플랫폼 응용 프로그램을 사용하고 있습니다. 사용자가 메인 윈도우의 X를 클릭하면 응용 프로그램에서 아무 것도하지 않고 암호 JDialog를 표시하기를 원합니다. 비밀번호가 맞으면 앱을 닫고 앱을 닫지 마십시오. 어떻게해야합니까? JDialog 패스워드를 보여줄 리스너 클래스를 만들었지 만, 어플리케이션을 닫지 않게하는 방법은 무엇입니까? JFrame의 setDefaultCloseOperation과 유사하고 닫을 때 아무 것도하지 않도록 설정합니다. 에서NetBeans 플랫폼이 닫을 때 아무런 반응이 없습니다.

public class Listener extends WindowAdapter { 

     private Frame frame; 

     @Override 
     public void windowActivated(WindowEvent event) { 
      frame = WindowManager.getDefault().getMainWindow(); 
      frame.setSize(946, 768); 
     } 

     @Override 
     public void windowClosing(WindowEvent event) { 
      ShutDownMainWindowJDialog shutDownMainWindowJDialog; 
      shutDownMainWindowJDialog = new ShutDownMainWindowJDialog(null, true); 
      shutDownMainWindowJDialog.exeShutDownMainWindowJDialog(); 
      shutDownMainWindowJDialog.setLocationRelativeTo(frame); 
      shutDownMainWindowJDialog.setVisible(true); 
     } 
    } 

public class Installer extends ModuleInstall { 

    @Override 
    public void restored() { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       Frame frame = WindowManager.getDefault().getMainWindow(); 
       frame.addWindowListener(new Listener()); 
      } 
     }); 
    } 
} 
+1

왜 그냥 JFrame의를 사용할 수 있습니까? –

+0

어떻게? JFrame someJFrame = WindowManager.getDefault(). getMainWindow(); 나에게 오류를 준다 – jadrijan

+0

새로운 JFrame을 만드시겠습니까? 하하. 'JFrame frame = new JFrame();'그러나 아래의 제 대답은 여러분이'Frame'을 사용하기로 결정했다면 효과가 있습니다. 'JFrame'을 확장 한 클래스를 메인 윈도우에 사용할 수 있습니다. –

답변

4

쉽습니다. 따라서 인스톨러로 모듈을 만들고 닫는 메소드를 덮어 쓰고, 대화 상자를 보여주고 false를 반환합니다 (close app가 아닙니다).

지르 카

그래서이 결론 : 활성/ 이 모듈 설치에 추가

package master; 

import org.openide.modules.ModuleInstall; 

/** 
* Manages a module's lifecycle. Remember that an installer is optional and 
* often not needed at all. 
*/ 

public class Installer extends ModuleInstall { 

@Override 
public void close() { 
    //your module shutdown 
} 

@Override 
public boolean closing() { 

// this is place for your Dialog() and return: 
// 
//  true if you want to enable close the app 
//  other return false 
/* 
if you force shutdown app try LifecycleManager.getDefault().exit(); 
System.exit(0) is very dummy, because it does not respect betweenmodule dependencyis 

} 
} 

지르 카

+0

한 달 전에이 사실을 알았지 만, 어쨌든 당신의 답변에 감사드립니다. 정말 감사드립니다. – jadrijan

-1

당신의 windowClosing()이 맞다면 사용자와 dispose() 프레임에서 암호를 얻을.

public class PasswordToClose extends WindowAdapter { 

    private JFrame frame; 

    public PasswordToClose(JFrame frame) { 
    this.frame = frame; 
    } 

    public static void main(String[] args) { 
    JFrame frame = new JFrame("Password to close"); 
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 

    frame.addWindowListener(new PasswordToClose(frame)); 
    frame.setSize(200, 200); 
    frame.setVisible(true); 
    } 

    @Override 
    public void windowClosing(WindowEvent evt) { 
    String password = JOptionPane.showInputDialog(frame, "Enter password"); 
    if ("secret".equals(password)) { 
     frame.dispose(); 
    } 
    } 
} 
+0

그는 JFrame을 사용할 수 없다고 주장합니다. 나는 이미 JFrame과 setDefaultClose를 사용한다고 제안했지만, 마음에 들지 않았습니다. –

+0

안녕하세요, NB가 모듈 시스템을 기반으로하는 프레임 워크이기 때문에 windowClosing에 대한 생각이 좋지 않다고 생각합니다. 한 모듈은 "closing action"을위한 것이다 ;-) 간단한 overwride 닫기 또는 닫기 사용. Jirka – user1722245

관련 문제