我如何计算JTextArea中的行?

时间:2014-03-11 14:13:40

标签: java swing text jtextarea

我尝试使用linewarp,但似乎它不会起作用。我需要制作一些能够计算您在JTextArea中插入的文本行的内容。

这是我的代码,直到现在我遇到了这么多麻烦。我的程序现在可以计算50个单词,当ok被触发时,它会计算单词和字母,每50个单词就会显示<< 50>>的符号。或<<< 100>>或....:

package wordscount;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;

/**
 *
 * @author Shady Kamal
 * 
 */

public class WordsCount{

boolean isword;
boolean working;
int words = 0;
int charsa = 0;
int max = 3000;
JTextArea text = new JTextArea(70, 70);
JTextArea wordsText = new JTextArea();
JTextArea charsText = new JTextArea();
JFrame frame = new JFrame("Fast Reading 50 words - Made by Shady Kamal ©");
JButton launch = new JButton("Start");
JButton fullScreenButton = new JButton("Full Screen");
JLabel charsLabel = new JLabel();
JLabel wordsLabel = new JLabel();
String atext;
Dimension screenDimension= new Dimension();
Timer timer;



public WordsCount(){

    wordsText.setText("Words: " + "\n");
    wordsText.setEditable(false);
    wordsText.setEnabled(true);
    charsText.setText("Letters: " + "\n");
    charsText.setEnabled(true);
    charsText.setEditable(false);
    wordsText.setSize(10, 2);
    charsText.setSize(10, 2);

    frame.setSize(700, 700);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    launch.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {

        int wordsSize = 0;
        words = 0;
        atext = text.getText();
        charsa = atext.length();
        launch.setName("Start - Pressed");

        if(words <= 49){
            launch.setEnabled(true);
        }else{
            launch.setEnabled(false);
        }


        for (int i = 0; i < charsa; i++) {

            wordsSize++;

            if ( Character.isLetter(atext.charAt(i)) == false ){
                   isword = false;
               }else if (i == 0){
                   isword = true;
               }else if ( Character.isLetter(atext.charAt(i-1)) ){
                   isword = false;
               }else if ( atext.charAt(i-1) == '\'' && i > 1 && 
                          Character.isLetter(atext.charAt(i-2)) ){
                   isword = false;
                }else{
                   isword = true;
                }
            if (isword){
                words++;
                if(words == 50 || words == 50*2 || words == 50*3 || words == 50*4 
                        || words == 50*5 || words == 50*6 || words == 50*7 
                        || words == 50*8 || words == 50*9 || words == 50*10 
                        || words == 50*11 || words == 50*12 || words == 50*13 
                        || words == 50*14 || words == 50*15 || words == 50*16 
                        || words == 50*17 || words == 50*18 || words == 50*19 
                        || words == 50*20 || words == 50*21 || words == 50*22 
                        || words == 50*23 || words == 50*24 || words == 50*25 
                        || words == 50*26 || words == 50*27 || words == 50*28 
                        || words == 50*29 || words == 50*30 || words == 50*31 
                        || words == 50*32 || words == 50*33 || words == 50*34 
                        || words == 50*35 || words == 50*36 || words == 50*37 
                        || words == 50*38 || words == 50*39 || words == 50*40 
                        || words == 50*41 || words == 50*42 || words == 50*43
                        || words == 50*44 || words == 50*45 || words == 50*46 
                        || words == 50*47 || words == 50*48 || words == 50*49 
                        || words == 50*50 || words == 50*51 || words == 50*52
                        || words == 50*53 || words == 50*54 || words == 50*55
                        || words == 50*56 || words == 50*57 || words == 50*58
                        || words == 50*59 || words == max){
                    text.insert(" >>" + words + "<< ", wordsSize);
                }
               }
              }
              System.out.println("Chars: " + charsa);
              System.out.println("Words: " + words);
              wordsText.setText("Words: \n" + words);
              charsText.setText("Letters: \n" + charsa);

            }
       });

    charsLabel.add(charsText);
    wordsLabel.add(wordsText);
    frame.add(text, BorderLayout.CENTER);
    frame.add(wordsText, BorderLayout.EAST);
    frame.add(charsText, BorderLayout.WEST);
    frame.getContentPane().add(new JScrollPane(text),BorderLayout.CENTER);
    frame.add(launch, BorderLayout.SOUTH);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

public static void main(String[] args) {

    JOptionPane.showMessageDialog(null, ""
            + "                                Instructions: \n"
            + "_________________________________________\n"
            + "Just paste text right here in the TextBox\n"
            + "and click the \"Start\" button when your'e ready\n"
            + "the program will do the rest.\n"
            + "PAY ATTENTION!\n"
            + "1)It works only up to 3,000 words Just for \n"
            + "preventing bugs and lags and Making it smoothier and easier to run\n"
            );

    new WordsCount();

}

}

3 个答案:

答案 0 :(得分:1)

最简单的方法是通过拆分&#39; \ n&#39;来将字符串拆分成行。字符,只返回结果中的行数:

String[] lines = aText.split('\n');
int lineCount = lines.length;

答案 1 :(得分:1)

查看Text Utilities类。它有两种方法可供您使用:

  1. getLInes()
  2. getWrappedLines()
  3. 取决于你的要求。

答案 2 :(得分:0)

我终于找到了解决方案:

public double countLines(javax.swing.JTextArea textArea, double max_width) {
    javax.swing.text.PlainDocument doc = (javax.swing.text.PlainDocument) textArea.getDocument();
    double counting = 0;
    for (int i = 0; i < textArea.getLineCount(); i++) {
        try {
            int start = textArea.getLineStartOffset(i);
            int length = textArea.getLineEndOffset(i) - start; // calculating the length of the line
            // if the width of the line in greater than the max width, the division would return the number of lines
            counting += Math.ceil(textArea.getFontMetrics(textArea.getFont()).stringWidth(doc.getText(start, length)) / max_width);
        } catch (javax.swing.text.BadLocationException ex) {
            System.err.println("ERROR\n" + ex.getMessage());
        }
    }
    return counting;
}

它适用于所有情况,并完全计算☺