jButtonActionPerformed(ActionEvent e)方法中的代码无法正常工作

时间:2018-03-28 07:34:39

标签: java swing

我有jTextFieldjLabeljButton。我想将jTextField设置为空,并在执行jLabel操作后将jButton更新为一些新文字。

这是我的jButtonActionPerformed(ActionEvent e)方法的代码:

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    BufferedWriter fw;
    StringBuilder guessword = new StringBuilder(word);

    try {
        fw = new BufferedWriter(new FileWriter("C:\\Users\\Arihant\\JavaApplication1\\src\\javaapplication1\\scores.txt", true));

        while(guesses != 0) {
            jLabel26.setText(Integer.toString(guesses));
            guesschar = jTextField2.getText().charAt(0);
            flag = 0;

            for(int j = 0; j < word.length() / 2; j++) {                    
                if(Character.toLowerCase(guesschar) == temp[j] && (guessword.toString().indexOf(Character.toLowerCase(guesschar)) < 0)) {
                    flag = 1;

                    for(int k = 0; k < word.length(); k++) {
                        if(Character.toLowerCase(guesschar) == word.charAt(k))
                            guessword.setCharAt(k, Character.toLowerCase(guesschar));
                    }
                    if(guessword.toString().equals(word)) {                            
                        switch (difficulty) {
                            case "EASY":
                                points = guesses * 50;
                                break;
                            case "MODERATE":
                                points = guesses * 100;
                                break;
                            case "HARD":
                                points = guesses * 200;
                                break;
                            default:
                                points = 0;
                                break;
                        }

                        try {
                            fw.append(name + " " + points);
                            fw.newLine();
                            fw.close();
                        } catch (IOException ex) {
                            Logger.getLogger(HangMan.class.getName()).log(Level.SEVERE, null, ex);
                            System.out.println(ex);
                        }

                        jPanel13.removeAll();
                        jPanel13.add(jPanel4);
                        jPanel13.repaint();
                        jPanel13.revalidate();
                        jLabel29.setText("<html>YOU WIN!<br/> The word was: </html>" + word);
                        jLabel31.setText("You scored: " + points + " points.");
                        jLabel28.setText("Play again?");
                    }
                }
            }
            if(flag == 0) {
                guesses--;
            }
            jPanel13.repaint();
            jPanel13.revalidate();
            jLabel1.setText(guessword.toString());
            jTextField2.setText("");
            Document document = jTextField2.getDocument();
            document.addDocumentListener(new JButtonStateController(jButton8, 0));
            ((AbstractDocument) jTextField2.getDocument()).setDocumentFilter(new JTextFieldFilter(0));
        }
    } catch (IOException ex) {
        Logger.getLogger(HangMan.class.getName()).log(Level.SEVERE, null, ex);   
        System.out.println(ex);
    }
}          

当我点击jButton8时,它会跳过try-catch块中的整个代码并直接转到jPanel13.removeAll();

每次点击jTextField2时,我都希望将jButton8设置为空,将jButton8设置为禁用。

告诉我,我错在哪里以及如何改进我的代码?

0 个答案:

没有答案