Java 如何将文本从类1设置为类2?
我在将class1中的一个字段的文本设置为class2中的另一个字段时遇到问题。基本上,我有两门课。在class1中,我有一个方法,允许用户搜索文件中的单词(从文件中读取),然后当找到单词时,我想将其设置为class2“field1” 例如,如果我搜索“San”,在class2中搜索的单词应该显示“San”,第二个单词应该显示“Aya” 我不知道我哪里出错了,程序没有显示任何错误。任何帮助都将不胜感激。提前谢谢 file.txt 圣阿亚Java 如何将文本从类1设置为类2?,java,swing,oop,Java,Swing,Oop,我在将class1中的一个字段的文本设置为class2中的另一个字段时遇到问题。基本上,我有两门课。在class1中,我有一个方法,允许用户搜索文件中的单词(从文件中读取),然后当找到单词时,我想将其设置为class2“field1” 例如,如果我搜索“San”,在class2中搜索的单词应该显示“San”,第二个单词应该显示“Aya” 我不知道我哪里出错了,程序没有显示任何错误。任何帮助都将不胜感激。提前谢谢 file.txt 圣阿亚 public class MyFileReader {
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是在搜索之前构建的,您必须延迟它的实例化,并在其构造函数中添加两个参数,以使用正确的值设置字段
JOptionPane显示在构造函数中,如果要使用注释中的setter,则必须将对话框移动到此setter。class2是在搜索之前构建的,您必须延迟它的实例化,并在其构造函数中添加两个参数,以设置具有正确值的字段
JOptionPane显示在构造函数中,如果要使用注释中的setter,则必须将对话框移动到此setter。您需要将第二个类更改为类似的类:
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();
}
}
公共类MyFileReader{
JTextField searchfield=新的JTextField(10);
JPanel面板=新的JPanel();
公共MyFileReader(){
添加(新JLabel(“搜索:”);
panel.add(搜索字段);
面板设置布局(新网格布局(5,2));
int result=JOptionPane.showConfirmDialog(null,panel,
“搜索”,JOptionPane.YES\u NO\u选项);
if(result==JOptionPane.YES\u选项){
MyContentManager contentManager=新建MyContentManager();
试一试{
String stringSearch=searchfield.getText();
BufferedReader bf=新的BufferedReader(新文件读取器(“file.txt”);
int linecount=0;
弦线;
ArrayList=新建ArrayList();
而((line=bf.readLine())!=null){
列表。添加(行);
linecount++;
int indexfound=line.indexOf(stringSearch);
如果(indexfound>-1){
String[]word=line.split(“\t”);
字符串firstword=word[0];
字符串secondword=单词[1];
int resultfromshiving=contentManager.showFieldsFound(stringSearch,secondWord);
}
}
bf.close();
}捕获(IOE异常){
System.out.println(“发生IO错误:+e.toString());
}
}
}
公共静态void main(字符串[]args){
新的MyFileReader();
}
}
不确定要对显示单词的结果执行什么操作,但我已更改了第二个类以返回值。您需要将第二个类更改为类似以下内容:
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();
}
}
公共类MyFileReader{
JTextField searchfield=新的JTextField(10);
JPanel面板=新的JPanel();
公共MyFileReader(){
添加(新JLabel(“搜索:”);
panel.add(搜索字段);
面板设置布局(新网格布局(5,2));
int result=JOptionPane.showConfirmDialog(null,panel,
“搜索”,JOptionPane.YES\u NO\u选项);
if(result==JOptionPane.YES\u选项){
MyContentManager contentManager=新建MyContentManager();
试一试{
String stringSearch=searchfield.getText();
BufferedReader bf=新的BufferedReader(新文件读取器(“file.txt”);
int linecount=0;
弦线;
ArrayList=新建ArrayList();
而((line=bf.readLine())!=null){
列表。添加(行);
linecount++;
int indexfound=line.indexOf(stringSearch);
如果(indexfound>-1){
String[]word=line.split(“\t”);
字符串firstword=word[0];
字符串secondword=单词[1];
int resultfromshiving=contentManager.showFieldsFound(stringSearch,secondWord);
}
}
bf.close();
}捕获(IOE异常){
System.out.println(“发生IO错误:+e.toString());
}
}
}
公共静态void main(字符串[]args){
新的MyFileReader();
}
}
不确定您想对显示单词的结果做什么,但我已更改了第二个类以返回值。我将给您的第二个类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();
}
}
在这里提问时,请努力发布格式更好的代码 我将给出您的第二类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();
}
}
在这里提问时,请努力发布格式更好的代码 您的类需要与任何非Swing类类似的getter和setter方法。class2构造函数不应该有JOptionPane,因为它不是事件创建的。如何应用getter和setter方法?您的另一个问题是,我看不到在哪里显示class2。如果未显示该类,您将永远看不到搜索结果