2013-03-28 109 views
0

我正在開發一個Java SWT程序,我需要一個登錄表單來進行用戶授權。如何在Java SWT中清除窗口歷史記錄?

我的程序的入口點是LoginWindow,當用戶成功登錄後,LoginWindow打開第二個窗口並隱藏它自己。當我關閉新窗口時,LoginWindow再次出現。如何阻止這種行爲?

public class LoginWindow 
{ 
    private Display    display; 
    private Shell    shell; 

    private Text    widgetLogin; 
    private Text    widgetPass; 

    private DatabaseHelper  db; 
    private PreferenceHelper pref; 

    /** 
    * @wbp.parser.entryPoint 
    */ 
    public static void main(String[] args) 
    { 
     LoginWindow window = new LoginWindow(); 
     window.open(); 
    } 

    public LoginWindow() 
    { 
     display = Display.getDefault(); 
     shell = new Shell(display, SWT.DIALOG_TRIM); 

     db = DatabaseHelper.getInstance(); 
     pref = PreferenceHelper.getInstance(); 

     // Auto login 
     { 
      String login = pref.getLogin(); 
      String password = pref.getPassword(); 

      if (login != null && password != null) 
      { 
       if (db.checkUserDetails(login, password)) 
       { 
        shell.close(); 

        MainWindow window = new MainWindow(); 
        window.open(); 
       } 
       else 
       { 
        pref.setLogin(null); 
        pref.setPassword(null); 
       } 
      } 
     } 
    } 

    public void open() 
    { 
     shell.setSize(450, 230); 
     shell.setText(JavaStrings.DIALOG_TITLE_LOGIN); 
     shell.setLocation(ContentHelper.windowCenter((display.getPrimaryMonitor()).getBounds(), shell.getBounds())); 
     shell.setImages(ContentHelper.loadAppicationIcons(display)); 
     GridLayout gl_shell = new GridLayout(2, true); 
     gl_shell.verticalSpacing = 10; 
     gl_shell.marginWidth = 10; 
     gl_shell.marginHeight = 10; 
     gl_shell.horizontalSpacing = 10; 
     shell.setLayout(gl_shell); 

     Label lblUsername = new Label(shell, SWT.NONE); 
     lblUsername.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1)); 
     lblUsername.setText(JavaStrings.TEXT_LOGIN); 

     widgetLogin = new Text(shell, SWT.BORDER); 
     widgetLogin.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); 
     widgetLogin.addKeyListener(new KeyAdapter() 
     { 
      public void keyReleased(KeyEvent e) 
      { 
       if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) 
       { 
        widgetPass.forceFocus(); 
       } 
      } 
     }); 

     Label lblPassword = new Label(shell, SWT.NONE); 
     lblPassword.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1)); 
     lblPassword.setText(JavaStrings.TEXT_PASS); 

     widgetPass = new Text(shell, SWT.BORDER | SWT.PASSWORD); 
     widgetPass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); 
     widgetPass.addKeyListener(new KeyAdapter() 
     { 
      public void keyReleased(KeyEvent e) 
      { 
       if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) 
       { 
        tryLogin(); 
       } 
      } 
     }); 

     Label lblForgetPassword = new Label(shell, SWT.NONE); 
     lblForgetPassword.setAlignment(SWT.RIGHT); 
     lblForgetPassword.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1)); 
     lblForgetPassword.setText(JavaStrings.TEXT_FORGET_PASS); 
     lblForgetPassword.addMouseListener(new MouseButtonClick()); 

     Button bRegister = new Button(shell, SWT.NONE); 
     bRegister.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); 
     bRegister.setText(JavaStrings.BUTTON_TEXT_REGISTER); 
     bRegister.addSelectionListener(new SelectionListener() 
     { 
      @Override 
      public void widgetSelected(SelectionEvent e) 
      { 
       RegisterWindow window = new RegisterWindow(shell); 
       window.open(); 
      } 

      @Override 
      public void widgetDefaultSelected(SelectionEvent e) 
      { 
       RegisterWindow window = new RegisterWindow(shell); 
       window.open(); 
      } 
     }); 

     Button bLogin = new Button(shell, SWT.NONE); 
     bLogin.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1)); 
     bLogin.setText(JavaStrings.BUTTON_TEXT_LOGIN); 
     bLogin.addSelectionListener(new ButtonClick()); 

     shell.open(); 
     shell.layout(); 

     while (!shell.isDisposed()) 
     { 
      if (!display.readAndDispatch()) 
      { 
       display.sleep(); 
      } 
     } 

     display.dispose(); 
    } 

    private class ButtonClick implements SelectionListener 
    { 
     public void widgetSelected(SelectionEvent e) 
     { 
      tryLogin(); 
     } 

     public void widgetDefaultSelected(SelectionEvent e) 
     { 
      tryLogin(); 
     } 
    } 

    private class MouseButtonClick extends MouseAdapter 
    { 
     @Override 
     public void mouseUp(MouseEvent e) 
     { 
      ResetPasswordDialog dialog = new ResetPasswordDialog(shell, SWT.DIALOG_TRIM); 
      dialog.open(); 
     } 
    } 

    private void tryLogin() 
    { 
     if (db.Status() == SqlStatus.OPEN) 
     { 
      String login = widgetLogin.getText().trim(); 
      String pass = widgetPass.getText().trim(); 

      if ((login.length() > 0) && (pass.length() > 0)) 
      { 
       widgetLogin.setEnabled(false); 
       widgetPass.setEnabled(false); 

       User user = db.selectUser(login, pass); 

       if (user != null) 
       { 
        pref.setLogin(user.getLogin()); 
        pref.setPassword(user.getPassword()); 

        shell.close(); 

        MainWindow window = new MainWindow(); 
        window.open(); 
       } 
       else 
       { 
        widgetLogin.setEnabled(true); 
        widgetPass.setEnabled(true); 

        // MessageBox 
       } 
      } 
      else 
      { 
       // MessageBox 
      } 
     } 
     else 
     { 
      // MessageBox 
     } 
    } 
} 

主窗口

public class MainWindow 
{ 
    private Display    display; 
    private Shell    shell; 

    public MainWindow() 
    { 
     display = Display.getDefault(); 
     shell = new Shell(); 
    } 

    public void open() 
    { 
     shell.setSize(500,500); 
     shell.setText(JavaConstants.APPLICATION_NAME); 
     shell.setImages(ContentHelper.loadAppicationIcons(display)); 

     GridLayout gl_shell = new GridLayout(1, false); 
     gl_shell.verticalSpacing = 0; 
     gl_shell.marginWidth = 0; 
     gl_shell.marginHeight = 0; 
     gl_shell.horizontalSpacing = 0; 
     shell.setLayout(gl_shell); 

     shell.open(); 
     shell.layout(); 

     while (!shell.isDisposed()) 
     { 
      if (!display.readAndDispatch()) 
      { 
       display.sleep(); 
      } 
     } 
    } 
} 

回答

0

的代碼片段您提供不提供太多的信息,但它看起來像你不要關閉LoginDialog。如果是這種情況,LoginDialog將「落後」您的MainWindow,因此如果關閉MainWindow,則會再次出現。


如果這不是你的問題的原因,這裏是一個另類:我最近的編程非常類似的東西,應用與MainWindow開始。此窗口將創建一個模態爲Dialog(您無法訪問MainWindow而不先關閉對話框)並提示用戶輸入憑證。該對話框然後執行登錄並且具有指示登錄是否成功的字段。從MainWindow開始,您可以在登錄失敗時簡單地檢查該字段並關閉應用程序。

+0

@Bez更新了我的代碼片段,我認爲問題是LoginWindow構造函數,我打開主窗口。 –

+0

@Андрей可以請您發佈MainWindow的代碼嗎? – Baz