英汉词典
- 简介
- 知识点简介
- 效果图
- 源代码
- Word.java
- DictionaryFunction.java
- DictionaryFrame.java
简介
花了1个多小时帮数媒的同学用以前的项目改了一个小型JavaFx项目。。只是完成任务 = =
知识点简介
随便写写,太简单了毫无意义,只是为了完成个报告。。
- 利用JavaFx来制作界面,通过ActionEvent添加了事件,实现了【查找单词】,【添加单词】,【删除单词】,【查看所有】,【修改单词】操作。
- 利用Alert类实现了弹框,包括提示类弹框与输入类弹框。
- 面向对象构造了Word类,字典的属性为中文单词与英文单词,方法为实现了封装性的方法。构造dictionaryFunction类,实现了添加单词,查询单词,删除单词,修改单词,显示所有单词的方法。
- 在dictionaryFunction利用数组存储word类。
- 灵活运用了顺序、选择、循环三种结构。
效果图
运行界面
【添加单词】
【查找单词】
【删除单词】
【查看所有】
【修改单词】
源代码
Word.java
public class Word {
String eWord;
String cWord;
public String geteWord() {
return eWord;
}
public void seteWord(String eWord) {
this.eWord = eWord;
}
public String getcWord() {
return cWord;
}
public void setcWord(String cWord) {
this.cWord = cWord;
}
public Word(String eWord, String cWord) {
this.eWord = eWord;
this.cWord = cWord;
}
public Word() {
this.eWord = null;
this.cWord = null;
}
public String toString() {
return (this.eWord + "===" + this.cWord);
}
}
DictionaryFunction.java
public class dictionaryFunction {
Word[] words;
int count;
public dictionaryFunction() {
words = new Word[100];
count = 0;
}
// 添加单词
public boolean add(Word word) {
if (count < 100) {
this.words[this.count++] = word;
return true;
} else
return false;
}
// 查询单词
public Word searchWord(String word) {
for (int i = 0; i < count; i++) {
if (this.words[i].geteWord().equalsIgnoreCase(word) || this.words[i].getcWord().equalsIgnoreCase(word))
return words[i];
}
return null;
}
// 显示所有单词
public void listAllWords() {
System.out.println("以下是所有的单词");
for (int i=0;i<count;i++)
System.out.println(this.words[i].toString());
}
// 删除单词
public boolean deleteWords(String word) {
int location = -1;
// 寻找要删除的元素,返回true
for (int i = 0; i < count; i++) {
if (this.words[i].geteWord().equalsIgnoreCase(word) || this.words[i].getcWord().equalsIgnoreCase(word)) {
location = i;
break;
}
}
// 找到删除,如果是最后一个元素,count--直接覆盖
if (location >= 0) {
for (int i = location; i < count - 1; i++) {
words[i] = words[i + 1];
}
count--;
return true;
}
// 找不到返回false
else
return false;
}
// 根据中文单词的含义,更新词典中的单词
public boolean updateWord(Word word) {
if (word != null) {
int i = indexOf(word.getcWord());
if (i >= 0) {
this.words[i] = word;
return true;
} else
return false;
} else
return false;
}
// 判断单词的中文,显示在列表中的位置
private int indexOf(String str) {
if (str == null)
return -1;
else {
for (int i = 0; i < this.count; i++) {
if (this.words[i].getcWord().equals(str))
return i;
}
return -1;
}
}
}
DictionaryFrame.java
import java.util.Optional;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class DictoryFrame extends javafx.application.Application {
static TextArea showArea; // 显示界面
Button buttonFind, buttonAdd, buttonDel, buttonAll, buttonChange; // 按钮
static dictionaryFuction function = new dictionaryFuction();
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane rootPane = new BorderPane();
rootPane.setPadding(new Insets(10));
showArea = new TextArea();
showArea.setPrefSize(300, 140);
showArea.setWrapText(true); // 自动
showArea.setEditable(false);
Font font = new Font("Cambria", 25);
showArea.setFont(font);
/*********开始界面**************/
showArea.setText("\t **************************\n");
showArea.appendText("\t\t 欢迎使用我的字典\n");
showArea.appendText("\t **************************\n");
/*****************************/
rootPane.setTop(showArea);
GridPane controlPane = new GridPane(); // 按钮
controlPane.setAlignment(Pos.CENTER);
controlPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
controlPane.setHgap(5.5);
controlPane.setVgap(5.5);
int buttonWidth = 100;
int buttonHeight = 30;
int x_index = 0;
int y_index = 0;
// 按钮字体
Font font2 = new Font("Cambria", 13);
buttonFind = new Button("查找单词");
buttonFind.setFont(font2);
buttonFind.setPrefSize(buttonWidth, buttonHeight);
controlPane.add(buttonFind, 0+x_index, 1+y_index);
buttonAdd = new Button("添加单词");
buttonAdd.setFont(font2);
buttonAdd.setPrefSize(buttonWidth, buttonHeight);
controlPane.add(buttonAdd, 2+x_index, 1+y_index);
buttonDel = new Button("删除单词");
buttonDel.setFont(font2);
buttonDel.setPrefSize(buttonWidth, buttonHeight);
controlPane.add(buttonDel, 4+x_index, 1+y_index);
buttonAll = new Button("查看所有");
buttonAll.setFont(font2);
buttonAll.setPrefSize(buttonWidth, buttonHeight);
controlPane.add(buttonAll, 6+x_index, 1+y_index);
buttonChange = new Button("修改单词 ");
buttonChange.setFont(font2);
buttonChange.setPrefSize(buttonWidth, buttonHeight);
controlPane.add(buttonChange, 8+x_index, 1+y_index);
/**********按钮的点击事件***********/
// 查看单词
buttonFind.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showWord();
}
});
// 增加单词
buttonAdd.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showAdd();
}
});
// 删除单词
buttonDel.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showDel();
}
});
// 显示所有
buttonAll.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showArea.setText("以下是所有单词:\n");
for(int i = 0 ; i < function.count ; i++){
showArea.appendText(function.words[i].toString() + "\n");
}
}
});
// 修改单词
buttonChange.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showUpdata();
}
});
/*******************点击事件********************/
rootPane.setCenter(controlPane);
Scene scene = new Scene(rootPane, 460, 300);
primaryStage.setTitle("英语词典");
primaryStage.setScene(scene);
primaryStage.show();
}
// 查找单词
public void showWord() {
try{
TextInputDialog dialog = new TextInputDialog();
dialog.setHeaderText(null);
dialog.setContentText("请输入要查找单词的中英文");
Optional<String> result = dialog.showAndWait();
String string = null;
if(result.isPresent()){
string = result.get();
}
Word word = function.searchWord(string);
if (word != null) {
showArea.setText("查找单词的中英文含义为:\n");
showArea.appendText(word.toString());
} else
showArea.setText("这个单词没找到");
}catch (Exception e) {
showArea.setText("这个单词没找到");
// 捕捉异常
}
}
// 添加单词
public void showAdd() {
try{
if (function.count < 100) {
TextInputDialog dialog = new TextInputDialog();
dialog.setHeaderText(null);
dialog.setContentText("输入要添加单词的中文含义");
Optional<String> result = dialog.showAndWait();
String cWord = result.get();
dialog.setContentText("输入要添加单词的英文含义");
result = dialog.showAndWait();
String eWord = result.get();
Word word = new Word(eWord, cWord);
function.add(word);
showArea.setText("添加单词成功");
} else
showArea.setText("字典已满!");
}catch (Exception e) {
// 捕捉异常
}
}
// 删除单词
public void showDel() {
try{
TextInputDialog dialog = new TextInputDialog();
dialog.setHeaderText(null);
dialog.setContentText("输入要删除单词的含义");
Optional<String> result = dialog.showAndWait();
String string = result.get();
boolean ifDel = function.deleteWords(string);
if (ifDel)
showArea.setText("删除成功!");
else
showArea.setText("删除失败!");
}catch (Exception e) {
// 捕捉异常
}
}
// 修改单词
public void showUpdata() {
try{
TextInputDialog dialog = new TextInputDialog();
dialog.setHeaderText(null);
dialog.setContentText("输入要修改单词的中文含义");
Optional<String> result = dialog.showAndWait();
String cStr = result.get();
dialog.setContentText("输入单词修改后的英文含义");
result = dialog.showAndWait();
String eStr = result.get();
Word word = new Word(eStr, cStr);
if (function.updateWord(word))
showArea.setText("单词修改成功");
else
showArea.setText("词典中没有这个单词");
}catch (Exception e) {
// 捕捉异常
}
}
public static void main(String[] args) {
launch(args);
}
}