该小应用是本人学习Java前期制作的程序,可能还有着一些Bug没有修复,敬请谅解!该应用用到了图形化界面、I/O流本地磁盘读取/写入数据、键盘事件、焦点事件、监听事件等知识。该应用拥有功能:
file菜单项:打开文件、保存文件、另存为文件、写入反馈文件。
edit菜单项:修改数据、检查中文拼写、检查英文拼写、查找单词。
源代码:
package recite;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.filechooser.*;
public class Recite {
public static void main(String args[]) {
new Win();
}
}
class Win extends JFrame implements ActionListener,FocusListener,KeyListener{
JMenu menu,edit;
JMenuItem open,save,save_as,modify,checkCH,checkEN,feedback,find;
JMenuBar menuBar;
JPanel nPanel,sPanel;
JFileChooser fileChooser;
Icon leftIcon=new ImageIcon("D:\\Program\\left.png"),
rightIcon=new ImageIcon("D:\\Program\\right.png"),
greenLight=new ImageIcon("D:\\Program\\d5.png"),
blueLight=new ImageIcon("D:\\Program\\d1.png"),
orangeLight=new ImageIcon("D:\\Program\\d9.png"),
whiteLight=new ImageIcon("D:\\Program\\d2.png");
JButton left,right,greenB,blueB,orangeB;
int pageNum=1,pageTotal=100,selectionState,checkCHFlag=0,checkENFlag=1,modifyFlag=0,array1=100,array2=12,saveState=0,modifyed=0;
Box baseBox,vBox[]=new Box[array2],hBox[]=new Box[array2];
JTextField mess[]=new JTextField[array2],input[]=new JTextField[array2],answer[]=new JTextField[array2],focusField;
JLabel page,status_bar;
String data1[][]=new String[array1][array2],data2[][]=new String[array1][array2];
Read autoReadFile=new Read();
File file=new File("D:/Program/Data.txt");
WindowAdapt windowAdapt=new WindowAdapt();
Win(){
init();
setSize(800,800);
setLocationRelativeTo(null);
setVisible(true);
windowAdapt.getFrame(this);
addWindowListener(windowAdapt);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
input[0].requestFocus();
}
void init() {
setTitle("Recite");
fileChooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("Text","txt");
fileChooser.setFileFilter(filter);
nPanel=new JPanel();
sPanel=new JPanel();
left=new JButton();
left.setIcon(leftIcon);
left.setContentAreaFilled(false);
left.setBorderPainted(false);
left.setFocusable(false);
right=new JButton();
right.setIcon(rightIcon);
right.setContentAreaFilled(false);
right.setBorderPainted(false);
right.setFocusable(false);
greenB=new JButton();
greenB.setIcon(whiteLight);
greenB.setContentAreaFilled(false);
greenB.setBorderPainted(false);
greenB.setFocusable(false);
blueB=new JButton();
blueB.setIcon(whiteLight);
blueB.setContentAreaFilled(false);
blueB.setBorderPainted(false);
blueB.setFocusable(false);
orangeB=new JButton();
orangeB.setIcon(orangeLight);
orangeB.setContentAreaFilled(false);
orangeB.setBorderPainted(false);
orangeB.setFocusable(false);
baseBox=Box.createVerticalBox();
for(int i=0;i1) {
page.setText(--pageNum+"/"+pageTotal);
for(int i=0;i1) {
page.setText(--pageNum+"/"+pageTotal);
for(int i=0;i
package recite;
import java.io.*;
public class Save {
Save(File file,String data1[][],String data2[][],int pageNum,int array2){
try {
Writer writer=new FileWriter(file);
BufferedWriter bWriter=new BufferedWriter(writer);
for(int i=0;i<100;i++) {
for(int j=0;j
package recite;
import java.io.*;
import javax.swing.*;
public class Read {
int pageNum;
void readFile(File file,String data1[][],String data2[][],JTextField mess[],int array2){
try {
File userDataFile=new File(file.getParent(),"userDataFile.txt");
System.out.println("user_Data_File.txt exists:"+userDataFile.exists());
Reader reader=new FileReader(file),
reader2=new FileReader(userDataFile);
BufferedReader bReader=new BufferedReader(reader),
bReader2=new BufferedReader(reader2);
String s=null;
int i=0,j=0,x=0,y=0;
while((s=bReader.readLine())!=null) {
if(!s.startsWith("-")) {
data1[i][j++]=s;
if(j>=array2) {
i++;
j=0;
}
}
else {
s=s.replaceAll("-","");
data2[x][y++]=s;
if(y>=array2) {
x++;
y=0;
}
}
}
pageNum=Integer.parseInt(bReader2.readLine());
for(int m=0;m
package recite;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Find extends JFrame implements KeyListener{
private static Find find=null;
WinAdapt winAdapt=new WinAdapt();
JTextField input;
JTextArea result;
Box one_VBox,base_Box;
String data1[][],data2[][];
int array1,array2;
private Find() {}
public static Find getInstance() {
if(find==null) {
find=new Find();
find.init();
}
else
;
return find;
}
void init() {
setTitle("Find");
setSize(500,350);
setLocationRelativeTo(null);
setVisible(true);
addWindowListener(winAdapt);
}
void function(String data1[][],int array1,String data2[][],int array2) {
this.data1=data1;
this.data2=data2;
this.array1=array1;
this.array2=array2;
setLayout(new FlowLayout());
input=new JTextField(35);
result=new JTextArea(12,6);
result.setEditable(false);
one_VBox=Box.createVerticalBox();
base_Box=Box.createHorizontalBox();
one_VBox.add(input);
one_VBox.add(Box.createVerticalStrut(15));
one_VBox.add(new JScrollPane(result));
base_Box.add(one_VBox);
add(base_Box);
input.addKeyListener(this);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
int key=e.getKeyCode(),t=1;
if(key==KeyEvent.VK_ENTER) {
result.setText(" ");
String s=input.getText();
try {
for(int i=0;i
package recite;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Feedback extends JFrame {
JTextArea text;
WindowAdap windowAdap=new WindowAdap();
private static Feedback feedback=null;
private Feedback(){}
public static Feedback getInstance() {
if(feedback==null) {
feedback=new Feedback();
feedback.init();
}
else
;
return feedback;
}
public void init() {
this.setLayout(new FlowLayout());
text=new JTextArea(25,42);
text.setLineWrap(true);
add(new JScrollPane(text));
addWindowListener(windowAdap);
try {
String s;
File file=new File("D:\\Program\\feedback.txt");
Reader reader=new FileReader(file);
BufferedReader bReader=new BufferedReader(reader);
while((s=bReader.readLine())!=null)
text.append(s+"\n");
bReader.close();
}
catch(Exception ex) {
System.out.println(ex.toString());
}
setTitle("feedback");
setSize(500,500);
setLocationRelativeTo(null);
setVisible(true);
}
class WindowAdap extends WindowAdapter{
public void windowClosing(WindowEvent e) {
try {
File file=new File("D:\\Program\\feedback.txt");
Writer writer=new FileWriter(file);
BufferedWriter bWriter=new BufferedWriter(writer);
bWriter.write(text.getText());
bWriter.close();
}
catch(Exception ex) {}
feedback=null;
}
}
}
程序界面:
除了源代码,还需要在本地D://Program文件夹中插入如下四张图片或者文件名相同,大小接近的任意六张图片作为按钮