gpt4 book ai didi

java - SWT:用于密码输入的InputDialog

转载 作者:搜寻专家 更新时间:2023-11-01 01:10:48 28 4
gpt4 key购买 nike

如何使用 SWT InputDialog 对象输入密码,将普通字符替换为通常的 *

还是不可能?

最佳答案

只需创建您自己的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;
}
}

结果是这样的:

enter image description here

关于java - SWT:用于密码输入的InputDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13104658/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com