4

InputDialog通常の文字を通常の文字に置き換えて、SWT オブジェクトを使用してパスワードを入力するにはどうすればよい*ですか?

それとも不可能ですか?

4

2 に答える 2

9

独自のものを作成するだけDialogです:

public static void main(String[] args) {
    PasswordDialog dialog = new PasswordDialog(new Shell());
    dialog.open();

    System.out.println(dialog.getPassword());
}

public static class PasswordDialog extends Dialog {
    private Text passwordField;
    private String passwordString;

    public PasswordDialog(Shell parentShell) {
        super(parentShell);
    }

    @Override
    protected void configureShell(Shell newShell)
    {
        super.configureShell(newShell);
        newShell.setText("Please enter password");
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite comp = (Composite) super.createDialogArea(parent);

        GridLayout layout = (GridLayout) comp.getLayout();
        layout.numColumns = 2;

        Label passwordLabel = new Label(comp, SWT.RIGHT);
        passwordLabel.setText("Password: ");
        passwordField = new Text(comp, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);

        GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
        passwordField.setLayoutData(data);

        return comp;
    }

    @Override
    protected void okPressed()
    {
        passwordString = passwordField.getText();
        super.okPressed();
    }

    @Override
    protected void cancelPressed()
    {
        passwordField.setText("");
        super.cancelPressed();
    }

    public String getPassword()
    {
        return passwordString;
    }
}

結果は次のようになります。

ここに画像の説明を入力

于 2012-10-28T09:28:24.740 に答える
5

InputDialog をサブクラス化し、テキスト コントロールに使用されるスタイルをオーバーライドできます。

public class PasswordDialog extends InputDialog {

    public PasswordDialog(Shell parentShell, String dialogTitle, String dialogMessage, String initialValue, IInputValidator validator) {
        super(parentShell, dialogTitle, dialogMessage, initialValue, validator);
    }

    @Override
    protected int getInputTextStyle() {
        return super.getInputTextStyle() | SWT.PASSWORD;
    }
}
于 2014-04-22T10:13:47.333 に答える