如何调用接受字符串的方法?

时间:2015-01-28 16:18:37

标签: java

我正在尝试保存用户选择使用JFileChooser的.txt文件。

我有3种方法

Thing 1获取文件路径并将其存储到名为File1的字符串中。

Thing 2需要此字符串才能将文件保存在正确的位置。

然而,当我在GUI ActionPerformed中运行thing2时,它会给我一个错误,因为它需要传入一个字符串。我传入“String File1”但它不起作用。

thing1

JFileChooser chooser;
        String choosertitle = null;
        String File1 = null;

chooser = new JFileChooser(); 
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

chooser.setAcceptAllFileFilterUsed(true);

if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { File1 = chooser.getCurrentDirectory() + "";
  }
else {
  JOptionPane.showMessageDialog(null,"No Selection ");

  }

    fileTextField.setText(File1);
return File1;
事情2

      String nameOfFile = (""); 
    String choice = comboBox.getSelectedItem() + "";

  JOptionPane.showMessageDialog(null,File1);


    if ("All Messages".equals(choice)){
        nameOfFile = ("Messages");  
    } else if 
        ("All Email Address".equals(choice)){
         nameOfFile = ("Address Book");  
    }

    File f = new File (File1 + nameOfFile+ ".txt");
    FileWriter fw;
    try {
        fw = new FileWriter(f);
        fw.write("This is a file created by Joe ");
        fw.close();
    } catch (IOException ex) {
        Logger.getLogger(Export.class.getName()).log(Level.SEVERE, null, ex);
    }


    boolean allFieldsCheck = !choice.equals("Please Select What You Would Like To Export") && !fileTextField.equals(""); 
    if (allFieldsCheck == false) {
        InputError ipe = new InputError();
        ipe.setVisible(true);
    } else {
        //add new message 
    }

ActionPerformed

private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

       thing2(String File1);

    }                           

我想要的上述代码就是获取用户想要保存文件的位置,然后将其保存在那里。

任何帮助都会感激不尽,因为我不熟悉这个

3 个答案:

答案 0 :(得分:1)

您必须修改exportButtonActionPerformed()方法:

private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    thing2(File1);
}

thing1 中定义的变量File1必须从此方法中可见,例如:

public class AClass
{
    /* declared here in order to be visible both from thing1 and to ActionPerformed */
    private String File1 = null;

    /* thing1 */
    public void AMethod() {
        JFileChooser chooser = new JFileChooser(); 
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle(choosertitle);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        chooser.setAcceptAllFileFilterUsed(true);

        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            File1 = chooser.getCurrentDirectory() + "";
            fileTextField.setText(File1);
        } else
            JOptionPane.showMessageDialog(null,"No Selection ");
    }

    /* ... */

    /* ActionPerformed */
    private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        thing2(File1);
    }
}

答案 1 :(得分:0)

你应该这样称呼它:

private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {
    thing2(File1);
}

(顺便说一下:更像Java的命名会更容易理解。)

答案 2 :(得分:0)

首先定义变量File1,然后将其传递给您的方法。

String file1 = "file path";  // variable name should be start with small case
thing2(file1);