【发布时间】:2014-06-24 13:16:18
【问题描述】:
我不知道我的代码有什么问题,但密码不会变成点。请帮我。 >.
这是我的用户界面代码:
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 正常工作。这个项目并不是我真正制作的,这就是为什么我不了解它的一些用途。请帮帮我。
【问题讨论】:
-
我认为你必须在 password_id 上调用方法
setEchoChar(char c) -
1)
setBounds(100, 100, 469, 428);应该是pack()但需要在添加组件后调用。 2) 为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。 3)见Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是的。) -
@Jens 好吧,即使我说它仍然不起作用。
-
@AndrewThompson 哦,我明白了。谢谢你,但由于我不是这样做的人,我不知道他为什么放那些。而且由于我是新手,所以我不知道如何发布 MCVE。我只想隐藏密码。
-
你绑定回显字符了吗?
setEchoChar
标签: java swing jpasswordfield