Ошибка компиляции - Java
Формулировка задачи:
import javafx.application.*; import javafx.scene.*; import javafx.stage.*; import javafx.scene.layout.*; import javafx.scene.control.*; import javafx.geometry.*; import javafx.event.*; import java.io.*; import java.util.*; import javafx.scene.text.*; class MyDictionary { private static final String FILENAME = "Dictionary.txt"; private static final String FILEPATH = "E:\\NetBeansProjects\\Dictionary"; private File file; private FileReader file_reader; private FileWriter file_writer; private BufferedReader buffered_reader; private BufferedWriter buffered_writer; MyDictionary() { file = new File(FILEPATH, FILENAME); } public void AddWord(String word, String translate) { try { file_writer = new FileWriter(file, true); file_writer.write(word + "\t" + translate + "\r\n"); file_writer.close(); } catch(IOException e) {System.out.println("Error");} } public void DeleteWord(String word) { try { file_reader = new FileReader(file); buffered_reader = new BufferedReader(file_reader); Vector<String> vector_string = new Vector<String>(); String string; while((string = buffered_reader.readLine()) != null) { if(string.contains(word)) { continue; } vector_string.add(string); } buffered_reader.close(); file_writer = new FileWriter(file); for(int i = 0; i < vector_string.size(); i++) { file_writer.write(vector_string.get(i) + "\r\n"); } file_writer.close(); } catch(IOException e) {System.out.println("Error");} } public Vector<String> getWords() { Vector<String> vector_string = new Vector<String>(); try { file_reader = new FileReader(file); buffered_reader = new BufferedReader(file_reader); String string; while((string = buffered_reader.readLine()) != null && vector_string.add(string)); buffered_reader.close(); } catch(IOException e) {System.out.println("Error");} return vector_string; } public void ClearDic() { try { file_writer = new FileWriter(file); file_writer.close(); } catch(IOException e) {System.out.println("Error");} } } public class Dictionary extends Application { public static void main(String[] args) {launch(args);} public void start(Stage stage) { MyDictionary dic = new MyDictionary(); stage.setTitle("Dictionary"); GridPane grid = new GridPane(); grid.setHgap(10); grid.setVgap(10); Scene scene = new Scene(grid, 400, 200); stage.setScene(scene); MenuBar menu_bar = new MenuBar(); Menu M_file = new Menu("File"); MenuItem MI_exit = new MenuItem("Exit"); M_file.getItems().addAll(MI_exit); Menu M_dictionary = new Menu("Dictionary"); MenuItem MI_AddWord = new MenuItem("Add word"); MenuItem MI_DeleteWord = new MenuItem("Delete word"); MenuItem MI_ShowWords = new MenuItem("Show words"); MenuItem MI_ClearDic = new MenuItem("Clear dictionary"); M_dictionary.getItems().addAll(MI_AddWord, MI_DeleteWord, MI_ShowWords, MI_ClearDic); Menu M_StartTraining = new Menu("Start training"); MenuItem MI_DefaultTraining = new MenuItem("Default training"); M_StartTraining.getItems().add(MI_DefaultTraining); menu_bar.getMenus().addAll(M_file, M_dictionary, M_StartTraining); grid.add(menu_bar, 0, 0); MI_exit.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { Runtime.getRuntime().exit(0); } }); MI_AddWord.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { TextField TF_word = new TextField(); TextField TF_translate = new TextField(); TF_word.setPromptText("Enter word"); TF_translate.setPromptText("Enter translate"); grid.getChildren().clear(); grid.add(menu_bar, 0, 0); grid.add(TF_word, 0, 1); grid.add(TF_translate, 1, 1); TF_translate.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { dic.AddWord(TF_word.getText(), TF_translate.getText()); grid.getChildren().clear(); grid.add(menu_bar, 0, 0); } }); } }); MI_DeleteWord.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { TextField TF_word = new TextField(); TF_word.setPromptText("Enter word which you want to delete"); grid.getChildren().clear(); grid.add(menu_bar, 0, 0); grid.add(TF_word, 0, 1); // TF_Word находиться в 0 колонке и 1 рядке TF_word.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { dic.DeleteWord(TF_word.getText()); grid.getChildren().clear(); grid.add(menu_bar, 0, 0); } }); } }); MI_ShowWords.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { grid.getChildren().clear(); grid.add(menu_bar, 0, 0); for(int i = 0; i < dic.getWords().size(); i++) { Text text = new Text(); text.setText(dic.getWords().get(i)); grid.add(text, 0, i+1); } } }); MI_ClearDic.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { dic.ClearDic(); } }); MI_DefaultTraining.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { for(int i = 0; i < dic.getWords().size(); i++) { Text T_Word = new Text(); TextField TF_Translate = new TextField(); T_Word.setText(dic.getWords().get(i)); TF_Translate.setPromptText("Enter translate"); grid.getChildren().clear(); grid.add(menu_bar, 0, 0); grid.add(T_Word, 0, 1); grid.add(TF_Translate, 1, 1); TF_Translate.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { if(dic.getWords().get(i).contains(TF_Translate.getText())) { Text text = new Text("Correct"); grid.getChildren().clear(); grid.add(menu_bar, 0, 0); grid.add(text, 0, 1); } } }); } } }); stage.show(); } }
if(dic.getWords().get(i).contains(TF_Translate.getText()))
Чтобы долго не искали методы и классы, напишу интерфейсы и обьявления
MyDictionary dic = new MyDictionary(); public Vector<String> getWords(); TextField TF_Translate = new TextField();
Решение задачи: «Ошибка компиляции»
textual
Листинг программы
final int k = i; TF_Translate.setOnAction(new EventHandler<ActionEvent> () { public void handle(ActionEvent action_event) { if(dic.getWords().get(k).contains(TF_Translate.getText())) { Text text = new Text("Correct"); grid.getChildren().clear(); grid.add(menu_bar, 0, 0); grid.add(text, 0, 1); } } });
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д