class1의 필드 텍스트를 class2의 다른 필드로 설정하는 데 문제가 있습니다. 기본적으로 두 가지 수업이 있습니다. class1에는 파일에서 읽는 단어를 검색 할 수있는 메서드가 있습니다. 단어가 발견되면 class2 "field1"로 설정하려고합니다.class1에서 class2로 텍스트를 설정하는 방법은 무엇입니까?
예를 들어, "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();
}
}
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);
}
}
클래스에는 스윙 이외의 클래스와 비슷한 getter 및 setter 메서드가 필요합니다. class2 생성자에는 JOptionPane이 없어야합니다. 이벤트가 생성되지 않기 때문에 의미가 없습니다. –
getter 및 setter 메소드를 적용하는 방법은 무엇입니까? – Ramal
다른 문제는 어디서나 class2를 표시하는 위치가 표시되지 않는다는 것입니다. 클래스가 표시되지 않으면 검색 결과가 표시되지 않습니다. –