2013-06-30 47 views
1

我的小應用程序大部分工作正常,但有一些與之相悖的東西。我的代碼通知用戶他們需要輸入文件名WORKS。但是,這是它的結束,因爲通知用戶他們沒有輸入文字不起作用,並且文字也不會寫入文件。Java小應用程序未完成

就好像我的程序中途中斷了一樣。我希望有人能夠看看代碼,並讓我知道任何明顯的事情。我已經主演了6個小時,不再相信我的眼睛。該小程序是非常基本和直截了當的。用戶在工作目錄中輸入txt文件的文件名,然後將他們輸入到測試字段中的whataver寫入文件。

import java.applet.Applet; 
import java.awt.BorderLayout; 
import java.awt.Button; 
import java.awt.Color; 
import java.awt.HeadlessException; 
import java.awt.Label; 
import java.awt.TextArea; 
import java.awt.TextField; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import javax.swing.JOptionPane; 

public class wwalker2 extends Applet { 

    Button write = new Button("WriteToFile"); 
    Label label1 = new Label("Enter the file name:"); 
    TextField text = new TextField(20); 
    Label label2 = new Label("Write your text:"); 
    TextArea area = new TextArea(10, 20); 

    public void init() { 
     add(label1); 
     label1.setBackground(Color.orange); 
     add(text); 
     add(label2); 
     label2.setBackground(Color.orange); 
     add(area); 
     add(write, BorderLayout.CENTER); 
     write.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent z) { 
       WriteText writeText = new WriteText(); 
      } 
     }); 
    } 

    public class WriteText { 

     WriteText() { 
      try { 
       String str = text.getText(); 
       if (str.equals("")) { 
        JOptionPane.showMessageDialog(null, 
        "It's not that smart... You have to enter the path and filename"); 
        text.requestFocus(); 
       } else { 
        File f = new File(str); 
        if (f.exists()) { 
         BufferedWriter out = new BufferedWriter(new FileWriter(f, true)); 
         if (area.getText().equals("")) { 
          JOptionPane.showMessageDialog(null, "You haven't written anything yet!"); 
          area.requestFocus(); 
         } else { 
          out.write(area.getText()); 
          if (f.canWrite()) { 
           JOptionPane.showMessageDialog(null, "There is now some text in " + str); 
           text.setText(""); 
           area.setText(""); 
           text.requestFocus(); 
          } else { 
           JOptionPane.showMessageDialog(null, "There isn't any text in " + str); 
          } 
          out.close(); 
         } 
        } else { 
         JOptionPane.showMessageDialog(null, "Error 404 File not found!"); 
         text.setText(""); 
         text.requestFocus(); 
        } 
       } 
      } catch (HeadlessException | IOException x) { 
      } 
     } 
    } 
} 
+0

你不能「先試後抓」'HeadlessException'? – fge

+0

1)爲什麼要編寫一個小程序?如果由於規格而到期。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 2)爲什麼選擇AWT而不是Swing?在[Swing extras over AWT]上看到這個答案(http://stackoverflow.com/a/6255978/418556)有很多很好的理由放棄使用AWT組件。如果您需要支持較老的基於AWT的API,請參閱[混合重量級和輕量級組件](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html)。 –

回答

0

只是想一個場景,當有文字但空文本的東西像「」,可能是這樣的情況。在輸入字符串周圍使用修剪可能會幫助您避免此問題。這可能是一個更安全的,如果檢查:

if("".equals(str.trim())) 
1

兩件事。

其中,小程序通常有非常嚴格的沙盒安全限制,特別是寫入文件的能力。

二,在你的try-catch塊中,你應該顯示某種消息或記錄異常。

} catch (HeadlessException | IOException x) { 
    JOptionPane.showMessageDialog(this, "Failed to write because of " + x.getMessage()); 
    x.printStackTrace(); 
} 

我也建議你贊成的Swing框架的溝AWT框架 - 早於恕我直言