所以我制作了这个非常简单的程序,在应用程序完美运行50个单词之后放置字符串“50”。 问题就在这里:
if(words == 50){
text.insert("50", words * charsa);
}
这是测试代码:
package wordscount;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*
* @author Shady Kamal
*
*/
public class WordsCount {
boolean working;
int words = 0;
int wordneed = 50;
int charsa;
JTextArea text = new JTextArea(600, 600);
JFrame frame = new JFrame("Counter for fast reading");
JButton launch = new JButton("Start");
String atext;
boolean isword;
public WordsCount(){
frame.setSize(700, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
launch.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
words = 0;
atext = text.getText();
charsa = atext.length();
for (int i = 0; i < charsa; i++) {
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){
text.insert("50", words * charsa);
}
}
}
System.out.println("Chars:" + charsa);
System.out.println("Words:" + words);
}
});
frame.add(text, BorderLayout.CENTER);
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");
new WordsCount();
}
}
当我运行它并写出50或以上的单词时会抛出这个错误(其中的反讽是它最后说的“Build Sucessfull”,它对我来说似乎并不是那么成功[甚至它在构建编译和运行时工作并没有失败]这仍然是处理3行代码的巨大错误!)
run:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid insert
at javax.swing.JTextArea.insert(JTextArea.java:461)
at wordscount.WordsCount$1.actionPerformed(WordsCount.java:66)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 21 seconds)
答案 0 :(得分:3)
这是一个运行时错误,即使您的代码编译没有错误,它仍然可能发生。
Javadocs for the insert
method州:
抛出:
IllegalArgumentException - 如果pos是模型中的无效位置
words * charsa
(尚未)您的排名JTextArea
不得存在。确保位置已经存在或修改位置的公式,使其指向现有位置。
答案 1 :(得分:0)
insert
给你错误,因为charsa
是JTextArea
的大小,你甚至将它倍增,所以它明显超过JTextArea
大小。
我想这就是你想要的。试一试:
@Override
public void actionPerformed(ActionEvent e) {
int wordsSize = 0;
words = 0;
atext = text.getText();
charsa = atext.length();
for (int i = 0; i < charsa; i++) {
wordsSize++;
if(! Character.isLetter(atext.charAt(i)) && Character.isLetter(atext.charAt(i-1)))
{
isword = true;
}
else
{
isword= false;
}
if (isword){
words++;
if(words == 50){
text.insert("50", wordsSize);
}
}
}
System.out.println("Chars:" + charsa);
System.out.println("Words:" + words);
}