用JPasswordField暂时没找出来方法,不过注意到api文档里有一句话:JPasswordField 与使用 echoChar 设置的 java.awt.TextField 是根本一致的。所以我尝试用java.awt.TextField 实现,结果发现很简单。只用了一个setEchoChar(char c)方法,下面是我的测试代码(己运行成功):
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Text extends JFrame implements ActionListener{
private TextField tf = new TextField(6);
private JButton jb = new JButton("转换");
public Text(String str){
super(str);
this.setLayout(new FlowLayout());
//这里设置所有输入都以"*"输出
tf.setEchoChar('*');
this.add(tf);
this.add(jb);
jb.addActionListener(this);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jb)){
System.out.println("dsf");
//这里设置所有输入按原字符输出
tf.setEchoChar((char)0);
}
}
public static void main(String[] args) {
new Text("测试密码框");
}
}
Button b=new Button(shell,SWT.NONE);
b..addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
OTHERSHELL os=new OTHERSHELL(shell);
os.open();
}
});