3

我有一个用 Swing 编码的带有 JButton 的表单,我单击它来完成操作。但是当我单击它时,我的框架会挂起。在使用任务栏结束它之前,我无法退出它。问题是什么?这是我的代码:

jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
    }
});

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String[] stopwords = {"a", "sometime", "sometimes", "somewhere", "still",
            "such", "system", "take", "ten", "than", "that", "the", "their", 
            "them", "themselves", "then", "thence", "there", "thereafter",
            "thereby", "therefore", "therein", "thereupon", "these", "they",
            "thickv", "thin", "third", "this", "those", "though", "three",
            "through", "throughout", "thru", "thus", "to", "together", "too",
            "top", "toward", "towards", "twelve", "twenty", "two", "un",
            "under", "until", "up", "upon", "us", "very", "via", "was", "we",
            "well", "were", "what", "whatever", "when", "whence", "whenever",
            "where", "whereafter", "whereas","whereby", "wherein", "whereupon",
            "wherever", "whether", "which", "while","whither", "who", "whoever",
            "whole", "whom", "whose", "why", "will", "with", "within", "without",
            "would", "yet", "you", "your", "yours", "yourself", "yourselves",
            "contact", "grounds", "buyers",  "tried", "said,", "plan", "value",
            "principle.", "forces", "sent:", "is,", "was", "like", "discussion",
            "tmus", "diffrent.", "layout", "area.", "thanks", "thankyou",
            "hello", "bye", "rise","fell","fall","psqft.","http://","km","miles"};
    try {
        Scanner fip1 = new Scanner(new File(selectedFile));
        FileOutputStream out=new FileOutputStream("d:/StopWords.txt");
        while(fip1.hasNext()) {
            int flag=1;
            String s1=fip1.next();
            s1=s1.toLowerCase();
            for(int i=0;i<stopwords.length;i++){
                if(s1.equals(stopwords[i])) {
                    flag=0;
                }
            }
            if(flag!=1) {
             // System.out.println(s1);
             // jTextArea1.append("\n"+s1);
             PrintStream p=new PrintStream(out);
             p.println(s1);  
             p.close();
         }
     }
         JOptionPane.showMessageDialog(null,"STOP WORD REMOVAL IS DONE");        
     } catch(Exception e){
         System.err.println("cannot read file");
     }
}                                        
4

2 回答 2

1

这听起来像是一个循环,Java 有非常好的调试服务,通常内置在你的 IDE 中。您应该尝试这些,看看您是否可以追踪正在发生的事情。在处理按钮的地方停下来,然后按照此处的代码进行操作。

您也可以将 Hashmap 用于您的停用词列表,但这不是您的错误,只是一种改进。

编辑:正如@robin 指出的 Swing 动作必须迅速结束。

于 2013-03-07T14:50:31.010 回答
1

我认为这是因为扫描仪的使用。为什么不使用输出流?不管问题如何——你应该使用SwingWorker来做脏的 IO 工作,并保持 UI 响应。

于 2013-03-07T14:56:43.450 回答