如何从 class1 到 class2 设置文本? [英] How to setText from class1 to class2?

查看:51
本文介绍了如何从 class1 到 class2 设置文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem setting the text of a field in class1 to another field in class2. Basically, I have two classes. In class1 i have a method that allows the user to search for a word that is in a file(reading from file) and then when the word is found, i want to set it to the class2 "field1".

例如,如果我搜索San",则在 class2 中搜索的单词应显示为San",第二个单词应显示为Aya".

For example, if i search for "San", the searched word in class2 should show "San" and the second word should show "Aya".

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

I dont know where iam going wrong and the program doesn't show any errors. Any help will be appreciated. Thanks in advance.

文件.txt

三亚

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

推荐答案

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

I would give your 2nd class setter methods, have it produce a JPanel that can be obtained via a getter method, and simply display it in a JOptionPane (if desired). For instance:

DamClass1.java

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

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

在此处提问时,请努力发布格式更好的代码.

Please put in some effort to post better formatted code when asking a question here.

这篇关于如何从 class1 到 class2 设置文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆