简体   繁体   English

当我点击按钮时,为什么我的挥杆挂起?

[英]why my swing just hangs when I click button?

I have a form coded in Swing with a JButton which I click to get an action done. 我有一个用Swut编码的表单和一个JButton,我点击它来完成一个动作。 But when I click it, my frame hangs. 但是当我点击它时,我的框架就会挂起。 I can't exit it until I end it using the taskbar. 在我使用任务栏结束之前,我无法退出。 What is the problem? 问题是什么? Here is my code: 这是我的代码:

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

That sounds like a loop, Java has very good debugging services, often built into your IDE. 这听起来像一个循环,Java有很好的调试服务,通常内置在你的IDE中。 You should try those to see if you can track down what's happening. 您应该尝试这些,看看您是否可以追踪正在发生的事情。 Put a stop where you process the button and follow the code fom here. 停止处理按钮的位置,并按照此处的代码进行操作。

You could use a Hashmap for your list of stop words as well, but that's not your bug, just an improvement. 您也可以使用Hashmap作为停用词列表,但这不是您的错误,只是一种改进。

Edit: as @robin pointed out Swing actions must end quickly. 编辑:正如@robin指出的那样,Swing动作必须快速结束。

I think it's because of the scanner usage. 我认为这是因为扫描仪的使用情况。 why not using outputStream? 为什么不使用outputStream? regardless of the problem- you should use SwingWorker to do the dirty IO work, and keep the UI responsive. 无论问题如何 - 您应该使用SwingWorker来执行脏IO工作,并保持UI响应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮摆动Java后,事情不显示 - Things dont show after I click my button swing Java 为什么单击按钮后我的应用程序崩溃? - Why does my app crash when i click the button? 为什么当我单击键盘上的“确定”按钮时清除了我的EditText? - Why my EditText is cleared when I click on “OK” button on the keyboard? 当我单击按钮时如何在Java的JTextField上打印Java辅助字符的字形 - how to print a glyph of supplementary characters in java onto my JTextField when i just click the button 动态删除Java Swing容器(我的代码挂在按钮上) - Removing Java Swing Containers dynamically (my code hangs on button press) 当我单击“生成”按钮时,仅工作一次 - when I click on the “Generate” button work just once time 为什么创建对话框时Swing应用程序最小化? - Why is my Swing application minimizing when I create a dialog? 为什么当我点击计算器应用程序中的算术函数按钮时我的应用程序停止? - Why does my app gets stopped when I click the arithmetic function button in my calculator app? 我的主活动启动了一个新的活动(WebView)。 这只是一页。 但是,当我单击“后退”按钮时,出现白屏了吗? - My main Activity launches a new Activity (WebView). It's just a page. But when I click the “Back” button…a white screen appears? 当我点击按钮时,我的应用程序崩溃了 - my app is crashing when i click on the button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM