简体   繁体   English

“添加一个条目”模式类的好名字

[英]good name for “add one more entry” pattern class

I am creating a GUI in java and there are multiple places where I want a form with a button to add one more entry ( see an example of the form here . It is in french, but I think it does not matter. ). 我在java中创建一个GUI,我想要一个带有按钮的表单,可以添加一个条目( 请参阅此处的表单示例 。它是法语,但我认为无所谓。)。 Since the logic to add/delete entries is the same in all places, it seems logic to create a parent class for each form. 由于添加/删除条目的逻辑在所有地方都是相同的,因此为每个表单创建父类似乎是逻辑。 My questions are : 我的问题是:

  • Is there already a class I don't know about that does the same thing? 是否已经有一个我不知道的课程做同样的事情? (it seems like there would be since it is a frequently used pattern, but I could not find it) (似乎会有,因为它是一个经常使用的模式,但我找不到它)
  • Is there a term for the "add one more entry" pattern? 是否有“添加一个条目”模式的术语?

在此输入图像描述

I have a solution. 我有一个解决方案。 You can create a String array (String[]) and use a folder of text files to track your entries. 您可以创建一个String数组(String [])并使用文本文件夹来跟踪您的条目。 Then, you can pull up your entries from the text files. 然后,您可以从文本文件中提取条目。 This way, you won't lose any data when you close the program. 这样,关闭程序时不会丢失任何数据。 Here is a walk through of what you should do: 以下是您应该做的事情:

  1. Make a folder in the location you are running the program to store text files. 在运行程序的位置创建一个文件夹来存储文本文件。

  2. Type the following code in the actionPreformed() argument of your jButton, or whatever trigger you have: 在jButton的actionPreformed()参数中键入以下代码,或者您拥有的任何触发器:

File f = new File("Name Of Your Folder");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(f.list()));
nameOfYourComboBox.setModel(new DefaultComboBoxModel(names.toArray()));
  1. Create a new class that writes text files based on the name the user enters. 创建一个新类,根据用户输入的名称编写文本文件。 You can put this code in your main[] argument: 您可以将此代码放在main []参数中:
BufferedWriter bw = null;
      try {
        JTextPane textPane1 = new JTextPane();
        //You can use other containers, I just used a text pane as an example.
        JTextPane textPane2 = new JTextPane();
        //This Pane is optional. It simply sets the contents of the file.

         //Specify the file name and path here
     File file = new File("/MyFolder'sNameGoesHere" + textPane1.getText());

     /* This logic will make sure that the file 
      * gets created if it is not present at the
      * specified location*/
      if (!file.exists()) {
         file.createNewFile();
      }

      FileWriter fw = new FileWriter(file);
      bw = new BufferedWriter(fw);
          bw.write(jTextPane2.getText());
          System.out.println("File written Successfully");

      } catch (IOException ioe) {
       ioe.printStackTrace();
    }
    finally
    { 
       try{
          if(bw!=null)
         bw.close();
       }catch(Exception ex){
           System.out.println("Error in closing the BufferedWriter"+ex);
        }
    }
  1. Make a button in your first class that calls the second class: 在第一个调用第二个类的类中创建一个按钮:

 NameOfMySecondClass nomsc = new NameOfMySecondClass(); nomsc.setVisible(true); //This will only work if your second class has a .setVisible() constructor. 

  1. Make optional delete buttons that remove files when the user selects them. 制作可选的删除按钮,用于在用户选择文件时删除文件。 You can use a JFileChooser if you want, although a combo box is fine for me. 如果你愿意,你可以使用JFileChooser,虽然组合框对我来说没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM