Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GUI FileViewer程序文件加载问题_Java_User Interface_Java1.4 - Fatal编程技术网

Java GUI FileViewer程序文件加载问题

Java GUI FileViewer程序文件加载问题,java,user-interface,java1.4,Java,User Interface,Java1.4,我目前正在开发一个java 1.4.2 GUI文件读取/保存/加密程序。因为它超过500行,我不会在这里发布全部内容…我会发布我遇到问题的部分。我遇到的问题是加密功能,其中会弹出一个框来输入要加密的文件名,但是无论输入什么,在您有机会输入文件名之前,都会出现FileNotFound异常。打开和保存功能工作正常,只是加密部分 这是: public void encrypt() throws IOException { openWin = new JFrame("Encrypt F

我目前正在开发一个java 1.4.2 GUI文件读取/保存/加密程序。因为它超过500行,我不会在这里发布全部内容…我会发布我遇到问题的部分。我遇到的问题是加密功能,其中会弹出一个框来输入要加密的文件名,但是无论输入什么,在您有机会输入文件名之前,都会出现FileNotFound异常。打开和保存功能工作正常,只是加密部分

这是:

public void encrypt() throws IOException
{

        openWin = new JFrame("Encrypt File");

        Container openFile = openWin.getContentPane();


        JLabel L1;
        JPanel panel = new JPanel();
        JButton encryptButton;

        L1 = new JLabel ("Choose File to Encrypt: ");

        panel.add(L1);
        output = new JTextField(20);
        panel.add(output);

        encryptButton = new JButton("Encrypt File");
        encryptButton.addActionListener(this);
        panel.add(encryptButton);

        openFile.add(panel);

        openWin.setBounds(50,100,400,150);
        openWin.setVisible(true);


    //Get the current content pane
    contentPane = this.getContentPane();

    //refresh the content pane
    if(mainPanel !=null)
        contentPane.remove(mainPanel);

    //Create a new mainPanel
    mainPanel = new JPanel();
        //We need a buffered reader to read the file
    BufferedReader in = new BufferedReader(new FileReader(fileName));

    //Temp will hold heach line read in, text will be the final string
    String temp="";
    String text="";


    //read the first line
    temp = in.readLine();

    int length = temp.length();

    String encrypted = in.readLine();

    int index = temp.length() - 1;

    //loop until the file has ended
    while(index >= 0)
    {

        encrypted = encrypted + temp.charAt(index);

        index--;

        //read another line
        temp = in.readLine();
    }

    //create the text area.
    //send it (String data, height, width)

    page = new JTextArea(text,30,50);
    //Set line wrap to true, other wise it would just be one looooong line
    page.setLineWrap(true);

    //Here is where we create our scroll pane.
    scrollpane = new JScrollPane(page);


    //the page is connected to the scrollpane, the scrollpane gets connected to the mainPanel
    mainPanel.add(scrollpane);

    //The mainPanel is connected to the contentPane
    contentPane.add(mainPanel);

    //refresh the JFrame!
    validate();

}

知道我做错了什么吗?这是我第一次使用GUI编程。

哪个值有'fileName'变量?也许您应该使用JFileChooser


为java提供文件路径时是否使用双斜杠? 例如,代替

D:\Java\EncryptFile.txt
应该是

D:\\Java\\EncryptFile.txt

因为反斜杠是Java中的转义字符。

如果出现该异常是因为文件不存在,至少不是fileName变量中的文件。我敢肯定你在什么地方弄错了。在设置文件名的地方张贴代码。你能张贴异常的StackTrace吗?它只是“java.io.FileNotFoundException:”你能打印出文件的绝对名称吗?类似于:System.out.println(新文件名).getAbsolutePath());它永远不会达到读取文件的目的…当我从下拉菜单中单击encrypt时,它会给出一个异常。在出现允许您输入文件名的框之前。文件路径与java\bin中所有程序的路径相同。“打开”和“保存”功能正常工作,那么为什么不使用此功能呢?是否将文件扩展名(.txt)添加到文件名的末尾?文件名是公共类fileViewer下的“String fileName=”“;”