我试过使用“线经”,但似乎不起作用。我需要做一些东西来计算你在jtextarea中插入的文本行数。
这是我的代码,直到现在我有这么多的麻烦。我的程序现在可以计算50个单词,当“确定”被触发时,它会计算单词和字母,每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条答案
按热度按时间wwwo4jvm1#
我终于找到了解决办法:
它适用于任何情况,并精确计算☺
hm2xizp92#
最简单的方法是通过在“\n”字符上拆分,将字符串拆分为行,然后返回结果中的行数:
dsf9zpds3#
查看文本实用程序类。它有两种方法可以使用:
getlines()
getwrappedlines()
取决于你的要求。