簡體   English   中英

JTextPane中的JTextArea-想要換行

[英]JTextArea in JTextPane - Want new line

我想將文本添加到JTextArea,並在垂直方向上具有自動滾動條。

但是,當水平鍵入時,當行中沒有空格時,我希望自動換行。如果僅使用JTextArea,則可以,但是當我將其放入JScrollPane時,則在需要時不進行換行。

我怎樣才能做到這一點?

謝謝!

您必須配置textArea以包裝:

textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

您可能會考慮閱讀基礎教程,以使您有效入門:-)

默認情況下, JTextArea不會包裝文本,因此您必須手動定義該行為:

textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

另外,請確保正確設置了JScrollPane

JScrollPane sp = new JScrollPane(textArea);

//JScrollPanes are just like JPanels (except for the scrollbars) so be careful not to just add the JComponent to your frame; add the container instead.

frame.add(sp);

作為附帶說明,請閱讀教程 @kleopatra,因此建議您在文本區域上獲得良好的堅實基礎。

JTextArea不是實現Scrollable接口嗎? 那么,為什么需要JScrollPane?

編輯您的評論,這對我有用:

JScrollPane sP= new JScrollPane(txtarea);
sP.setBounds(10,60,780,500);
sP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM