Java JTextfield 固定文本,允许在文本开头附加文本
我想知道如何在不可编辑的 JTextfield 中固定文本,并允许将文本附加到文本字段文本的开头。
例如,JTextfield(电子邮件)默认包含@domain.com。
您可以输入 [email protected],但不能执行 [电子邮件受保护]。
是否有某种文本/输入侦听器可以与此组件一起使用来实现此行为?
任何其他解决方案都会很好,因为我想强制用户使用特定的电子邮件域。
I was wondering how you would have fixed text in a JTextfield that isn't editable and allows text to be appended at the beginning of the textfield text.
JTextfield (email) contains @domain.com by default for example.
You can type in [email protected] but cannot do [email protected].
Is there some sort of text/input listener I can use alongside this component to implement this behaviour?
Any other solution's would be good as I want to force the user to use a specific email domain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保持简单,只需将常量附加到从文本字段获取的文本即可。此外,还应通知用户可能通过使用标签将某些内容添加到他的输入中。
如果您想允许多个域,请使用一些组合框或列表。
Keep it simple just append constant to text obtained from text field. Also user should be informed that something is added to his input possibly by using Label.
If you want to allow several domains then use some combobox or list.
我喜欢维克托的简单解决方案。
但是,如果您想要更复杂的解决方案,请查看: JTextField 中的有限选择/ JTextComponent?
它展示了如何在文本字段的开头固定文本。我猜您可以将其自定义为在文本字段末尾添加固定文本。
I like viktor's simple solution.
However, if you want a more complex solution, then check out: Limited selection in a JTextField/JTextComponent?
It shows how to have fixed text at the beginning of the text field. I would guess you can customize it to have the fixed text at the end of the text field.