[英]JTextArea word wrap resizing
所以,我在JPanel(BoxLayout)上有JTextArea。 我也有Box填充器填充JPanel的其余部分。 我需要我的JTextArea以單行高度開始(我可以管理它),並在需要時擴展和減少。
自動換行已啟用,我只需要在添加/刪除新行時調整它的高度。
我嘗試使用documentListener和getLineCount(),但它無法識別wordwrap-newlines。
如果可能的話,我想避免弄亂字體。
而且,沒有滾動面板。 JTextArea必須始終完全顯示。
JTextArea
具有相當特殊的副作用,在適當的條件下,它可以自行增長。 當我試圖設置一個簡單的雙行文本編輯器時(每行限制字符長度,最多兩行),我偶然偶然發現了這個...
基本上,給定合適的布局管理器,這個組件可以自己增長 - 它實際上是有道理的,但讓我感到驚訝......
此外,您可能希望使用ComponentListener
來監視ComponentListener
何時更改大小,如果這是您感興趣的...
public class TestTextArea extends JFrame {
public TestTextArea() {
setLayout(new GridBagLayout());
JTextArea textArea = new JTextArea();
textArea.setColumns(10);
textArea.setRows(1);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
add(textArea);
setSize(200, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
textArea.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent ce) {
System.out.println("I've changed size");
}
});
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new TestTextArea();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.