简体   繁体   English

在JLabel中显示JTextField中的大字符串

[英]Display large String from JTextField in a JLabel

I would like to transfer a large String from a JTextField to a JLabel ,but it is not displaying as expected 我想将大StringJTextField传输到JLabel ,但未按预期显示

Eg: 例如:

wwwwxxyy... wwwwxxyy ...

As upon reaching the end of the JLabel it stops, but i would like it to continue to the next line 当到达JLabel的结尾时,它停止了,但是我希望它继续到下一行

Please guide me 请指导我

My code: 我的代码:

String url=txturl.getText();
String uname = txuser.getText();
String pwd = pass.getText();
pass.setVisible(false);
txuser.setVisible(false);
txturl.setVisible(false);
JLabel path=new JLabel();
JLabel username=new JLabel();
JLabel password=new JLabel();
path.setText("path is:"+url.toString());
username.setText("usename is:"+uname);
password.setText("password is:"+pwd);

If you want to display a lot of text, then a JLabel is not the best choice. 如果要显示很多文本,那么JLabel不是最佳选择。 A suitable alternative would be a JTextArea due to the word wrapping capabilities 由于自动换行功能,合适的替代方法是JTextArea

For example, using Lorem Ipsum as the text to display and the text area set to wrap text with: textArea.setLineWrap(true); 例如,使用Lorem Ipsum作为要显示的文本,并将文本区域设置为使用以下textArea.setLineWrap(true);包装文本: textArea.setLineWrap(true); you can see the benefits: 您会看到好处:

JLabel和JTextArea

(Include textArea.setEditable(false); if you don't want the contents to be modifiable by the user) (包括textArea.setEditable(false);如果您不希望用户修改内容)

For more information see: How to Use Text Areas 有关更多信息,请参见: 如何使用文本区域


Another point to note is that judging by this line: String pwd = pass.getText(); 还要注意的一点是,从这一行来看: String pwd = pass.getText(); , you are not using a JPasswordField as getText() is deprecated. ,您不使用JPasswordField因为不赞成使用getText() If you are then consider switching to getPassword() 如果要考虑的话,请切换到getPassword()

If not, you should consider the benefits of using a JPasswordField and reading up on how to use them here: How to Use Password Fields 如果没有,您应该考虑使用JPasswordField的好处,并在此处阅读如何使用它们: 如何使用密码字段

A key benefit being that the password isn't visible during typing: 一个关键的好处是在键入过程中密码不可见:

JTextField与JPasswordField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM