作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将 class1 中的字段文本设置为 class2 中的另一个字段时遇到问题。基本上,我有两个类(class)。在 class1 中,我有一个方法允许用户搜索文件中的单词(从文件中读取),然后当找到该单词时,我想将其设置为 class2“field1”。
例如,如果我搜索“San”,则在 class2 中搜索的词应显示为“San”,第二个词应显示为“Aya”。
我不知道哪里出错了,程序也没有显示任何错误。任何帮助将不胜感激。提前致谢。
文件.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();
}
}
类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);
}
}
最佳答案
我会给你的第二类 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);
}
}
请在此处提问时努力发布格式更好的代码。
关于java - 如何将 class1 的 setText 设置为 class2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15043481/
我是一名优秀的程序员,十分优秀!