0

「テキストフィールドの内容をテキストファイルに書き込む」ためのコードを作成しました。エラーはありませんが、テキストファイルには何も書き込まれません。誰かが私を助けて私の間違いについて教えてもらえますか?または、コードを編集して、コード全体を再度配置します。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    jButton1.addActionListener(new ActionListener(){
        public void actionPerformed(final ActionEvent e){
            handleActionPerformed(e);
        }
    });
}                                        

/**
* @param args the command line arguments
*/

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

//do you really need to pass ActionEvent in this case?
protected void handleActionPerformed(ActionEvent e) {
    BufferedWriter writer = null;
    try
    {
        String text = jTextField1.getText(); //get the text from the text field
        writer = new BufferedWriter(new FileWriter("D:\\Definition.txt"));
        writer.write(text); //write it in the file
        writer.flush(); //flush the write-buffer
    }
    catch (IOException ioe)
    {
        ioe.printStackTrace();
    }
    finally { //always close the stream in finally block
        try {
            if(writer != null)
                writer.close();
        }
        catch(IOException b) {
            b.printStackTrace();
        }
    }
}
4

1 に答える 1

3

handleActionPerformed(...)何もしません。これはうまくいくはずです...

jButton1.addActionListener(new ActionListener() {
  public void actionPerformed(final ActionEvent e) {
      handleActionPerformed(e);
  }                                     
}

//do you really need to pass ActionEvent in this case?
protected void handleActionPerformed(ActionEvent e) { 
 BufferedWriter writer = null;
 try
 {
   String text = jTextField1.getText(); //get the text from the text field
   writer = new BufferedWriter(new FileWriter("C:\\Definition.txt"));
   writer.write(text); //write it in the file
   writer.flush(); //flush the write-buffer
 }
 catch (IOException ioe)
 {
   ioe.printStackTrace();
 } 
 finally { //always close the stream in finally block
   try {
    if(writer != null) 
       writer.close();
   } catch(IOException e) {
     e.printStackTrace();
   } 
 }
}
于 2012-09-13T19:47:40.200 に答える