如何将classText从class1设置为class2?

时间:2013-02-23 17:36:49

标签: java swing oop

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

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

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

file.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();
    }
}

的Class2

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

3 个答案:

答案 0 :(得分: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);
   }
}

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

答案 1 :(得分:0)

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

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

答案 2 :(得分: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();
    }
}

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