【问题标题】:Accessing JTextField in a different class and adding it to a String在不同的类中访问 JTextField 并将其添加到字符串
【发布时间】:2014-04-06 19:14:57
【问题描述】:

我正在用 Java 编写一个程序,但我想在一个类中编写一个文件。在另一个类中,如果文件名已经存在,我想覆盖这个文件。 然而,第一个类使用一个 JTextField,您可以在其中输入一个数字。该数字将添加到文件名中。这很完美,但在第二堂课中就不行了。我使用了一个关联,但它给了我一个空指针异常。有人可以帮忙吗?

    public void actionPerformed(ActionEvent e) {
        //make file
        String fileName = "Week " + tf1.getText();
        File planning = new File(fileName + ".txt");

        // save file
        if (e.getSource() == b2) {
// file does't exists yet and all field are filled out
            if (!planning.exists() && allesGevuld()) {
                FileWriter fw;
                try {
                    fw = new FileWriter(planning, false);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write(toString());
                    bw.close();
                    fw.close();
                    JOptionPane.showMessageDialog(null,
                            "De weekplanning is opgeslagen!", "Succes",
                            JOptionPane.PLAIN_MESSAGE);
                    dispose();

                } catch (IOException e1) {
                    System.out.println("Exception ");
                }
                // no everything is filled in
            } else if (!allesGevuld()) {
                JOptionPane.showMessageDialog(null,
                        "Niet alle velden zijn ingevuld!", "Mislukt",
                        JOptionPane.PLAIN_MESSAGE);
            }
            // to overwriting frame
            else if (planning.exists() && allesGevuld()) {
                OverschrijvenFrame ov = new OverschrijvenFrame(planningFrame);
                ov.setVisible(true);

            }
        }
    }

二等:

if (e.getSource() == overschrijf) {
            // overschrijven van file
            String fileName = "Week"+ ;//this is where the number should be overwritten
            File planning = new File(fileName + ".txt");

            // delete the file which will be overwritten
            planning.delete();

            // new file
            File nieuw = new File(fileName + ".txt");

            FileWriter fw;
            try {
                fw = new FileWriter(nieuw);
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(planningFrame.toString());
                bw.close();
                fw.close();
                dispose();

            } catch (IOException e1) {
                System.out.println("Exception ");
            }
        }
    }

【问题讨论】:

  • 你的代码中JTextField在哪里?它在标题和问题中,但在代码中找不到任何位置。

标签: java string swing jtextfield


【解决方案1】:

你有什么问题?

如果您不能从二等舱调用 tf1.getText(),因为 tf1 在一等舱是私有的 -> 将其公开或添加公共静态变量以保存可以从第二类引用的文件名。

【讨论】:

    【解决方案2】:

    使用 Java IO

    http://docs.oracle.com/javase/7/docs/api/java/io/File.html#exists%28%29

    import java.io.*;
    //...
    new File("pathToFile").exists()
    

    或者使用 Java NIO

    http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html

    import java.nio.file.*;
    //...
    Files.exists(Paths.get("pathToFile"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      相关资源
      最近更新 更多