JTextArea自动换行后如何获得每一行的文字我试着这样做
对于没有回车换行的换行
FontMetrics fm = text.getFontMetrics(text.getFont());
fm.stringWidth(str);
然后比较textArea的宽度从而获得一行有多少文字
这样获得的文字不能考虑到textrea一行尾出现的空白
|faddshakjhkahsdjkfasdfas, |
|asdfasdfasdfsdfasdfasdfasd|
怎么办啊

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【softfanok】截止到2008-06-27 10:27:35的历史汇总数据(不包括此帖):
    发帖数:1                  发帖分:20                 
    结贴数:1                  结贴分:20                 
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   


        /**
         * 获取JTextArea中指定行的文本
         * @param textArea 目标JTextArea
         * @param line 行号,从0开始
         * @return 指定行的文本
         */
        public String getTextByLine(JTextArea textArea, int line)
        {
            String text = "";
            
            if(line >= textArea.getLineCount())
            {
                return text;
            }
            
            try
            {
                int start = textArea.getLineStartOffset(line);
                int end = textArea.getLineEndOffset(line);
                text = textArea.getText(start, end - start).replaceAll("\n", "");
            }
            catch(BadLocationException e)
            {
                e.printStackTrace();
            }
            
            return text;
        }