Java applet没有完成

时间:2013-06-30 03:50:21

标签: java applet awt

我的applet在很大程度上起作用,但是它有一些歪斜的东西。我的代码通知用户他们需要输入文件名WORKS。但这就是结束,因为通知用户他们没有输入文本不起作用,也没有将文本写入文件。

就好像我的节目中途休息一样。我希望有人可以看看代码并告诉我任何明显的事情。我已经主演了6个小时,不再相信我的眼睛了。 小程序是非常基本和直接的。用户在工作目录中输入txt文件的文件名,然后将他们输入的测试字段写入该文件。

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) {
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

两件事。

其一,applet往往具有非常严格的沙盒安全限制,特别是编写文件的能力。

二,在try-catch块中,您应该显示某种消息或记录异常。

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

我还建议您放弃AWT框架,转而使用Swing框架 - 恕我直言

答案 1 :(得分:0)

只考虑有文字的场景,但是像“”这样的空文本,可能就是这种情况。在输入字符串周围使用修剪可以帮助您避免此问题。如果检查,这可能更安全:

if("".equals(str.trim()))
相关问题