我不知道我的代码有什么问题,但密码不会变成点。请帮我。 >。<
这是我的UI代码:
private final JPanel contentPanel = new JPanel();
public JPasswordField password_id;
public JFormattedTextField user_ID;
public JButton btnAccept;
private JFormattedTextField previouslyFocusedTextBox = user_ID;
public login(final JFrame parent, boolean modal) {
super(parent, modal);
setBounds(100, 100, 469, 428);
getContentPane().setLayout(new BorderLayout());
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final int x = (screenSize.width - getWidth()) / 2;
final int y = (screenSize.height - getHeight()) / 2;
setLocation(x, y);
setResizable(false);
FlowLayout flow_2 = new FlowLayout(0,0,0);
final JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(128, 0, 128));
panel_1.setPreferredSize(new Dimension(10, 90));
panel_1.setBounds(new Rectangle(0, 0, 100, 100));
panel_1.setBorder(new EmptyBorder(50, 90, 0, 0));
panel_1.setLayout(flow_2);
header_Login.add(panel_1, BorderLayout.SOUTH);
user_ID = new JFormattedTextField();
user_ID.setFocusTraversalKeysEnabled(true);
user_ID.setFocusable(true);
user_ID.addFocusListener(this);
user_ID.setBounds(0, 0, 423, 45);
panel_1.add(user_ID);
user_ID.setMinimumSize(new Dimension(8, 32));
user_ID.setUI(new JTextFieldHintUI("Enter ID Here", Color.gray));
user_ID.setHorizontalAlignment(SwingConstants.LEFT);
user_ID.setFont(new Font("Calibri", Font.BOLD, 35));
user_ID.setPreferredSize( new Dimension(10, 40) );
user_ID.setColumns(20);
FlowLayout flow_3 = new FlowLayout(0,0,0);
JPanel body_Login = new JPanel();
body_Login.setBackground(new Color(128, 0, 128));
body_Login.setBorder(new EmptyBorder(0, 0, 0, 0));
getContentPane().add(body_Login, BorderLayout.CENTER);
body_Login.setLayout(flow_3);
password_id = new JPasswordField ();
password_id.setFocusTraversalKeysEnabled(true);
password_id.setFocusable(true);
password_id.addFocusListener(this);
body_Login.add(password_id);
password_id.setMinimumSize(new Dimension(8, 32));
password_id.setUI(new JTextFieldHintUI("Enter PASSWORD Here", Color.gray));
password_id.setHorizontalAlignment(SwingConstants.LEFT);
password_id.setFont(new Font("Calibri", Font.BOLD, 35));
password_id.setPreferredSize( new Dimension(100, 40) );
password_id.setColumns(20);
在代码的最后我得到了这个:
public void focusGained(FocusEvent ev) {
//System.out.println("CHANGE");
if(ev.getSource() instanceof JFormattedTextField) {
previouslyFocusedTextBox = (JFormattedTextField) ev.getSource();
}
}
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
}
我知道它有点乱,而且我仍然是手动编程UI的新手,所以这就是为什么我不知道我的方式。无论如何,我的JPasswordField不会将文本转换为点。我已经让它在不久前工作了,但我又回来了,认为它不起作用。无论如何,我不能让它再次运作,我真的不知道我将做些什么来使它工作。密码的原始代码是:
public JFormattedTextField password_id;
password_id = new JFormattedTextField((Format) null);
当我将代码更改为JPasswordField时,字段不会关注密码字段,可能是因为previousFocusedTextBox中的FormattedTextField。我真的不知道我应该做些什么来让JPasswordField工作。这个项目并非由我真正制作,这就是为什么我不理解它的一些用途。请帮帮我。
答案 0 :(得分:0)
使用JPaswordField而不是JTextFormated Field