1

我在将 class1 中的一个字段的文本设置为 class2 中的另一个字段时遇到问题。基本上,我有两节课。在class1中,我有一个方法允许用户搜索文件中的单词(从文件中读取),然后当找到该单词时,我想将其设置为class2“field1”。

例如,如果我搜索“San”,则 class2 中的搜索词应显示“San”,第二个词应显示“Aya”。

我不知道我哪里出错了,程序没有显示任何错误。任何帮助将不胜感激。提前致谢。

文件.txt

San Aya

public class MyFileReader {

    JTextField searchfield = new JTextField(10);
    JPanel panel = new JPanel();

    public MyFileReader() {
        panel.add(new JLabel("Search:"));
        panel.add(searchfield);
        panel.setLayout(new GridLayout(5, 2));
        int result = JOptionPane.showConfirmDialog(null, panel,
                "Search", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            MyContentManager contentManager = new MyContentManager();
            try {
                String stringSearch = searchfield.getText();
                BufferedReader bf = new BufferedReader(new FileReader("file.txt"));
                int linecount = 0;
                String line;
                ArrayList<String> list = new ArrayList<String>();
                while ((line = bf.readLine()) != null) {
                    list.add(line);
                    linecount++;
                    int indexfound = line.indexOf(stringSearch);
                    if (indexfound > -1) {
                        String[] word = line.split("\t");
                        String firstword = word[0];
                        String secondword = word[1];
                        contentManager.field1.setText(stringSearch);//This is the problem
                        contentManager.field2.setText(secondword);//This is the problem
                    }
                }
                bf.close();
            } catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }
        }
    }

    public static void main(String[] args) {
        new MyFileReader();
    }
}

类2

public class MyContentManager {

    JTextField field1 = new JTextField(10);
    JTextField field2 = new JTextField(10);
    JPanel panel = new JPanel();

    public MyContentManager() {
        panel.add(new JLabel("Searched For:"));
        panel.add(field1);
        panel.add(new JLabel("Second word:"));
        panel.add(field2);
        panel.setLayout(new GridLayout(5, 2));
        int result = JOptionPane.showConfirmDialog(null, panel,
                "Search found", JOptionPane.YES_NO_OPTION);
    }
}
4

3 回答 3

4

我会给你的第二类setter方法,让它产生一个可以通过getter方法获得的JPanel,然后简单地将它显示在一个JOptionPane中(如果需要)。例如:

DamClass1.java

class DamClass1 {
   JTextField searchfield = new JTextField(10);
   JPanel panel = new JPanel();

   public DamClass1() {
      panel.add(new JLabel("Search:"));
      panel.add(searchfield);
      panel.setLayout(new GridLayout(5, 2));
      int result = JOptionPane.showConfirmDialog(null, panel, "Search",
            JOptionPane.YES_NO_OPTION);
      if (result == JOptionPane.YES_OPTION) {
         DamClass2 c2 = new DamClass2();
         String stringSearch = searchfield.getText();

         if (stringSearch.equals("Foo")) {
            c2.setField1(stringSearch);
            c2.setField2("Bar");

            int result2 = JOptionPane.showConfirmDialog(panel, c2.getPanel(),
                  "Search found", JOptionPane.YES_NO_OPTION);
         }

         // commented to make the code runnable for me.
         // try {
         // BufferedReader bf = new BufferedReader(new FileReader("file.txt"));
         // int linecount = 0;
         // String line;
         // ArrayList<String> list = new ArrayList<String>();
         // while ((line = bf.readLine()) != null) {
         // list.add(line);
         // linecount++;
         // int indexfound = line.indexOf(stringSearch);
         // if (indexfound > -1) {
         // String[] word = line.split("\t");
         // String firstword = word[0];
         // String secondword = word[1];
         // c2.field1.setText(stringSearch);//This is the problem
         // c2.field2.setText(secondword);//This is the problem
         // }
         // }
         // bf.close();
         // } catch (IOException e) {
         // System.out.println("IO Error Occurred: " + e.toString());
         // }
      }
   }

   public static void main(String[] args) {
      DamClass1 s1 = new DamClass1();
   }
}

DamClass2.java

class DamClass2 {
   private JTextField field1 = new JTextField(10);
   private JTextField field2 = new JTextField(10);
   private JPanel panel = new JPanel();

   public DamClass2() {
      panel.add(new JLabel("Searched For:"));
      panel.add(field1);
      panel.add(new JLabel("Second word:"));
      panel.add(field2);
      panel.setLayout(new GridLayout(5, 2));
   }

   public JPanel getPanel() {
      return panel;
   }

   public void setField1(String text) {
      field1.setText(text);
   }

   public void setField2(String text) {
      field2.setText(text);
   }
}

在这里提出问题时,请努力发布格式更好的代码。

于 2013-02-23T18:39:17.790 回答
0

class2 是在搜索之前构建的,您必须延迟它的实例化并在其构造函数中添加两个参数以使用正确的值设置字段。

JOptionPane 显示在构造函数中,如果要按照注释使用 settter,则必须将对话框移至此 setter。

于 2013-02-23T17:57:34.430 回答
0

您需要将第二类更改为:

public class MyContentManager {

    public int showFieldsFound(String first, String second) {
        JTextField field1 = new JTextField(10);
        field1.setText(first);
        JTextField field2 = new JTextField(10);
        field2.setText(second)
        JPanel panel = new JPanel();
        panel.add(new JLabel("Searched For:"));
        panel.add(field1);
        panel.add(new JLabel("Second word:"));
        panel.add(field2);
        panel.setLayout(new GridLayout(5, 2));
        return JOptionPane.showConfirmDialog(null, panel,
                "Search found", JOptionPane.YES_NO_OPTION);
    }
}

这将处理在您找到它们时显示这些字段。现在您的第一堂课需要执行以下操作:

public class MyFileReader {

    JTextField searchfield = new JTextField(10);
    JPanel panel = new JPanel();

    public MyFileReader() {
        panel.add(new JLabel("Search:"));
        panel.add(searchfield);
        panel.setLayout(new GridLayout(5, 2));
        int result = JOptionPane.showConfirmDialog(null, panel,
                "Search", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            MyContentManager contentManager = new MyContentManager();
            try {
                String stringSearch = searchfield.getText();
                BufferedReader bf = new BufferedReader(new FileReader("file.txt"));
                int linecount = 0;
                String line;
                ArrayList<String> list = new ArrayList<String>();
                while ((line = bf.readLine()) != null) {
                    list.add(line);
                    linecount++;
                    int indexfound = line.indexOf(stringSearch);
                    if (indexfound > -1) {
                        String[] word = line.split("\t");
                        String firstword = word[0];
                        String secondword = word[1];
                        int resultFromShowing = contentManager.showFieldsFound(stringSearch, secondWord);
                    }
                }
                bf.close();
            } catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }
        }
    }

    public static void main(String[] args) {
        new MyFileReader();
    }
}

不确定您想对显示单词的结果做什么,但我已经更改了第二个类以返回值。

于 2013-02-23T18:05:26.630 回答