java swing 实现 英汉词典,读写操作

package com.exercise.Test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Test {
static boolean bool = false;
public static void main(String[] args) throws FileNotFoundException {
// 创建 JFrame 实例
JFrame frame = new JFrame("英汉小词典");
// Setting the width and height of frame
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* 创建面板,这个类似于 HTML 的 div 标签
* 我们可以创建多个面板并在 JFrame 中指定位置
* 面板中我们可以添加文本字段,按钮及其他组件。
*/
JPanel panel = new JPanel();
// 添加面板
frame.add(panel);
placeComponents(panel);
// 设置界面可见
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) throws FileNotFoundException {
File file = new File("E:\\read.txt");
/* 布局部分我们这边不多做介绍
* 这边设置布局为 null
*/
panel.setLayout(null);
// 创建 JLabel
JLabel test1 = new JLabel("输入单词:");
/* 这个方法定义了组件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
test1.setBounds(10,20,80,25);
panel.add(test1);
/*
*
*/
JTextField word = new JTextField(20);
word.setBounds(80,20,165,25);
panel.add(word);
//
JLabel test2 = new JLabel("输入翻译:");
test2.setBounds(250,20,80,25);
panel.add(test2);
/*
*这个类似用于输入的文本域
*
*/
JTextField translate = new JTextField(20);
translate.setBounds(310,20,165,25);
panel.add(translate);
// 创建录入按钮
JButton input = new JButton("录入");
input.setBounds(500, 20, 80, 25);
panel.add(input);
input.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input(file, word.getText(),translate.getText());
}
});
JButton out = new JButton("显示");
out.setBounds(40, 140, 80, 25);
panel.add(out);
JTextArea area = new JTextArea(3, 10);// 构造一个文本域
area.setBounds(140, 100, 300, 200);
panel.add(area);
StringBuffer buffer = new StringBuffer();
out.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String encoding="UTF-8";
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read;
try {
read = new InputStreamReader(new FileInputStream(file),encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
//
buffer.append(lineTxt+"\r\n");
}
// System.out.println(buffer);
area.setText(buffer.toString());
read.close();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}//考虑到编码格式
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
/**
* 写入信息
* @param file
* @param word
* @param translate
*/
public static void input(File file,String word,String translate) {
FileOutputStream outputStream;
try {
if(bool) {
outputStream = new FileOutputStream(file,true);;
}else {
outputStream = new FileOutputStream(file);
}
bool = true;
String wordStr = "单词: "+word;
String translateStr =" 解释: "+translate;
outputStream.write(wordStr.getBytes());
outputStream.write(translateStr.getBytes());
outputStream.write("\r\n".getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

浙公网安备 33010602011771号