java - 我如何计算 JTextArea 中的行数?

标签 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();

}

}

最佳答案

我终于找到了解决方案:

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;
}

它适用于各种情况,并且计算准确 ☺

关于java - 我如何计算 JTextArea 中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22328337/

相关文章:

java - 如何拆分ArrayList的元素?

java - 使用 miglayout 的垂直按钮栏布局

java - 编辑或刷新 JList

java - 如何在不终止主(调用)程序的情况下关闭另一个具有 JFrame.EXIT_ON_CLOSE 的 Swing 程序

c - 有没有办法在读取文本文件后删除第一个字符?

java - 将基于文本的游戏制作成可执行的 jar - Java

JAVA - 如何在不同的类中使用 Enum 作为公共(public)静态最终字符串?

java - 使用 double 值的绝对值对 HashMap 进行排序

JAVA setVisible 不隐藏组件

WPF - TextBlock 文本 + 超链接