JTextField:当文本太长时,如何在JTextField的左侧设置文本 [英] JTextField : How to set text on the left of JTextField when text too long

查看:161
本文介绍了JTextField:当文本太长时,如何在JTextField的左侧设置文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在JTextField中显示很长的String.如果String太长,即使我使用setHorizontalAlignment(JTextField.LEFT)

I have a long String that I want to display in a JTextField. If the String is too long, it is showing the right-portion of the String, rather than the left portion, even when I use setHorizontalAlignment(JTextField.LEFT)

例如,如果我的String"JTextField示例,此文本太长" ,则应显示为...

For example, if my String is "JTextField example , this text is too long", it should show as...

|----------------------|
| JTextField example ..|
|----------------------|

但是它显示为...

|----------------------|
| this text is too long|
|----------------------|

有人可以建议如何解决此问题.

Could someone please suggest how this can be fixed.

推荐答案

当字段的大小大于字符串的字符数时,horizo​​ntalAlignment可以正常工作,但是如果字段的大小小于字符串的字符数,则只对LEFT_ALIGNMENT进行处理.其创建的setText,而不与以后的setText一起使用.

the horizontalAlignement works fine when the size of the field is bigger than the number of chars of the string, but if it is smaller it only does the LEFT_ALIGNMENT with the setText of its creation, not with any later setText.

您可以使用以下命令将插入符号的位置强制为文本的开头:

You could force the position of the caret to the begining of the text using:

myTextField.setCaretPosition(0);

这篇关于JTextField:当文本太长时,如何在JTextField的左侧设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆