在另一个类中访问JTextField并将其添加到String中(Accessing JTextField in a different class and adding it to a String)

编程入门 行业动态 更新时间:2024-10-27 18:23:48
在另一个类中访问JTextField并将其添加到String中(Accessing JTextField in a different class and adding it to a String)

我正在用Java创建一个程序,我遇到的问题是我想在一个类中编写一个文件。 在另一个类中,我想覆盖此文件,如果该文件的名称已经存在。 但是,第一个类使用JTextField,您可以在其中输入数字。 该数字将添加到文件名中。 这样做很完美,但在第二节课中则不然。 我使用了一个关联,但它给了我一个nullpointer异常。 有人可以帮忙吗?

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 "); } } }

I'm making a program with Java and I have the problem that I want to write a file in one class. In another class I want to overwrite this file, if the name of the file already exsists. However the first class uses a JTextField in which you can enter a number. That number is than added to the filename. That works perfect, but in the second class it won't. I used an association, but it gave me a nullpointer exception. Can someone help?

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

Second class:

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 "); } } }

最满意答案

你有什么问题呢?

如果你不能从第二个类调用tf1.getText(),因为tf1在第一个类中是私有的 - >使它成为公共或添加公共静态变量来保存可以从第二个类引用的文件名。

What your problem?

If you cannot call tf1.getText() from second class because tf1 is private in first class -> make it public or add public static variable to hold the file name that can refer from second class.

更多推荐

本文发布于:2023-07-26 18:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   并将其   JTextField   String   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!