简体   繁体   English

如何在Java SWT中清除窗口历史记录?

[英]How clear window History in Java SWT?

I'm working on an Java SWT program and i need a login form for user authorization. 我正在开发Java SWT程序,我需要用户授权的登录表单。

The entry point of my program is the LoginWindow, when the user has successfully logged in the LoginWindow opens a second Window and hides it self. 我的程序的入口点是LoginWindow,当用户成功登录LoginWindow时,它将打开第二个窗口并自行隐藏它。 When i close the new Window the LoginWindow shows up again. 当我关闭新窗口时,LoginWindow再次显示。 How to stop this behavior? 如何停止这种行为?

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
        }
    }
}

MainWindow 主窗口

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();
            }
        }
    }
}

The code snippet you provided does not supply much information, but it looks like you don't close the LoginDialog . 您提供的代码片段没有提供太多信息,但是您似乎没有关闭LoginDialog If this is the case, the LoginDialog will "sit behind" your MainWindow and thus show up again if you close the MainWindow . 在这种情况下, LoginDialog将“坐在”您的MainWindow后面,因此如果您关闭MainWindow ,则会再次出现。


If this is not the reason for your problem, here is an alternative: I have been programming something very similar recently and the application starts with the MainWindow . 如果这不是造成问题的原因,那么可以选择以下方法:最近我在编写一些非常相似的东西,并且应用程序从MainWindow开始。 This window will then create a Dialog which is modal (you cannot access MainWindow without closing the dialog first) and prompts the user to enter the credentials. 然后,此窗口将创建一个模式Dialog (您必须先关闭对话框才能访问MainWindow ),并提示用户输入凭据。 The Dialog then does the login and has a field that indicates if the login was successful. 然后,对话框进行登录,并有一个字段指示登录是否成功。 From the MainWindow you can then simply check the field and close the application when the login was not successful. 然后,您可以从MainWindow简单地检查字段并在登录失败时关闭应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM