如何在JPasswordField中启用剪切,复制? [英] How do enable cut, copy in JPasswordField?
问题描述
我注意到我无法剪切并复制 JPasswordField
?现在如何将所选密码的部分复制/剪切到剪贴板?有没有方法可以做到这一点?
I noticed that i was unable to cut and copy in JPasswordField
? Now how to copy/cut the selected part of the password to clipboard? Are there any methods to do this?
推荐答案
简单,使用此方法
JPasswordField jt=new JPasswordField(20);
// Put client property
jt.putClientProperty("JPasswordField.cutCopyAllowed",true);
add(jt);
默认情况下, JPasswordField
中的密码不允许剪切/复制。你需要做的只是启用它们。
By default, the password in the JPasswordField
is not allowed to be cut/copied. All you need to do is to enable them.
根据关于禁用粘贴的评论我没有找到属性,但我已经实现了使用它,(我不推荐这种方式)
As per the comment on disabling paste i didn't find a property, but i have achieved using this, (i dont recommend this way)
jt.getActionMap().put("a",null);
jt.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ctrl V"),"a");
另一种方法是覆盖 paste()
(我推荐这种方式),同时声明
Another way, is to do override the paste()
(i recommend this way) while declaring
JPasswordField jt=new JPasswordField(20){
public void paste(){}
};
更新:我误解了评论。但上面会禁用粘贴。但是,要禁用复制/剪切/粘贴中的任何一个,最好是要禁用所需的禁用方法而不执行任何实现。
Update: I misunderstood the comment. But the above does disabling paste. However to disable any one of the copy/cut/paste, it is better if the required method that is to be disabled is overrided with no implementation in it.
如果有是一个更好的方式,我很乐意听到。
If there is a much better way, i would love to hear.
这篇关于如何在JPasswordField中启用剪切,复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!