📄 search.java
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Search
{
JFrame f;
MyPanel p;
JButton b,b1,b2,b3,b4,b5;
JLabel l1,l2;
JTextField tf;
JTextField pf;
static String bookName,myBook=new String("data\\自定义词库.dat");
static String [] sWord,sMeaning,myWord,myMeaning;
int l,myl;
public Search(String bookName)
{
this.bookName=bookName;
init();
}
void init()
{
f = new JFrame("查询");
b = new JButton("确认");
b1 = new JButton("确认");
b2 = new JButton("添加");
b3 = new JButton("删除");
b4 = new JButton("修改");
b5 = new JButton("退出");
l1 = new JLabel("英文单词");
l2 = new JLabel("汉语词汇");
tf = new JTextField(20);
pf = new JTextField(20);
p = new MyPanel("image\\blue.jpg");
p.setLayout(new FlowLayout());
f.setContentPane(p);
p.add(l1);
p.add(tf);
p.add(b1);
p.add(l2);
p.add(pf);
p.add(b);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
f.setSize(400,150);
f.show();
b1.setOpaque(false);
b2.setOpaque(false);
b3.setOpaque(false);
b4.setOpaque(false);
b5.setOpaque(false);
l1.setOpaque(false);
l2.setOpaque(false);
b.setOpaque(false);
b1.setForeground(Color.yellow);
b2.setForeground(Color.yellow);
b3.setForeground(Color.yellow);
b4.setForeground(Color.yellow);
b5.setForeground(Color.yellow);
l1.setForeground(Color.yellow);
l2.setForeground(Color.yellow);
b.setForeground(Color.yellow);
b1.addActionListener(new english());
b2.addActionListener(new add());
b3.addActionListener(new delete());
b4.addActionListener(new amend());
b5.addActionListener(new exit());
b.addActionListener(new chinese());
getStrings();
getMyStrings();
}
void getStrings()
{
try {
BufferedReader br = new BufferedReader(new FileReader(bookName+".dat"));
String sLine;
char[] cArray;
int i,j,m,n;
l=0;
while ((sLine = br.readLine())!= null)
l++;
sWord = new String[l];
sMeaning = new String[l];
br.close();
br = new BufferedReader(new FileReader(bookName+".dat"));
l=0;
while ((sLine = br.readLine())!= null)
{
i = 0;j = 0;n = 0;
while( (sLine.charAt(i))!= '#' && i< sLine.length())
{
i++;
}
cArray = new char[i];
sLine.getChars(0,i,cArray,0);
sWord[l] = new String(sLine.copyValueOf(cArray));
while( (sLine.charAt(i))=='#' && i< sLine.length())
{
i++;
}
cArray = new char[sLine.length()];
sLine.getChars(0,sLine.length(),cArray,0);
for(m=0;m<sLine.length();m++)
{
if(cArray[m]=='#')n++;
}
if(n==1)
{
cArray = new char[sLine.length()-i];
sLine.getChars(i,sLine.length(),cArray,0);
}
else
{
j=i;
while((sLine.charAt(i))!='#'&& i< sLine.length())
{
i++;
}
cArray = new char[i-j];
sLine.getChars(j,i,cArray,0);
}
sMeaning[l] = new String(sLine.copyValueOf(cArray));
l++;
}
br.close();
}catch(FileNotFoundException f) {
System.out.println ("error" + f );
}catch(IOException f) {
System.out.println ("error" + f );
}
}
void getMyStrings()
{
try {
BufferedReader br = new BufferedReader(new FileReader(myBook));
String sLine;
char[] cArray;
int i,j,m,n;
myl=0;
while ((sLine = br.readLine())!= null)
myl++;
myWord = new String[myl];
myMeaning = new String[myl];
br.close();
br = new BufferedReader(new FileReader(myBook));
myl=0;
while ((sLine = br.readLine())!= null)
{
i = 0;j = 0;n = 0;
while( (sLine.charAt(i))!= '#' && i< sLine.length())
{
i++;
}
cArray = new char[i];
sLine.getChars(0,i,cArray,0);
myWord[myl] = new String(sLine.copyValueOf(cArray));
while( (sLine.charAt(i))=='#' && i< sLine.length())
{
i++;
}
cArray = new char[sLine.length()];
sLine.getChars(0,sLine.length(),cArray,0);
for(m=0;m<sLine.length();m++)
{
if(cArray[m]=='#')n++;
}
if(n==1)
{
cArray = new char[sLine.length()-i];
sLine.getChars(i,sLine.length(),cArray,0);
}
else
{
j=i;
while((sLine.charAt(i))!='#'&& i< sLine.length())
{
i++;
}
cArray = new char[i-j];
sLine.getChars(j,i,cArray,0);
}
myMeaning[myl] = new String(sLine.copyValueOf(cArray));
System.out.println(myWord[myl]);
System.out.println(myMeaning[myl]);
myl++;
}
br.close();
}catch(FileNotFoundException f) {
System.out.println ("error" + f );
}catch(IOException f) {
System.out.println ("error" + f );
}
}
class english implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int i;
for(i=0;i<l;i++)
{
if(tf.getText().equals(sWord[i]))
{
pf.setText(sMeaning[i]);
return;
}
else
pf.setText("无此单词");
}
}
}
class chinese implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int i,j,y,x,z=0;
String sLine,sLine1,result=new String();
char[] cArray,getArray;
y = pf.getText().length();
getArray = new char[y];
sLine = pf.getText();
sLine.getChars(0,y,getArray,0);
for(i=0;i<l;i++)
{
x = sMeaning[i].length();
cArray = new char[x];
sMeaning[i].getChars(0,x,cArray,0);
for(j=0;j<x;j++)
{
if(cArray[j]==getArray[0])
{
if((x-j)>=y)sLine1 = new String(cArray,j,y);
else
sLine1 = new String(cArray,j,x-j);
if(sLine1.equals(sLine))
{
if(z==0)result=sWord[i];
else
result=result+","+sWord[i];
tf.setText(result);z++;
}
}
}
}
if(z==0)
tf.setText("无此单词");
}
}
class add implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
String sLine1 = new String();
int i,x=0;
for(i=0;i<myl;i++)
{
if((tf.getText().equals(""))||(pf.getText().equals("")))x++;
if(myWord[i].compareTo(tf.getText())>0&&x<1)
{
sLine1=sLine1+tf.getText()+"#"+pf.getText()+"\n";
x++;
}
sLine1 = sLine1+myWord[i]+"#"+myMeaning[i]+"\n";
if(myWord[i].compareTo(tf.getText())==0)
{
x++;
}
}
if(x==0)sLine1=sLine1+tf.getText()+"#"+pf.getText()+"\n";
FileOutputStream out = new FileOutputStream(myBook);
byte buf[] = sLine1.getBytes();
out.write(buf);
out.close();
System.out.println(sLine1);
}
catch(Exception f)
{
System.out.println ("error" + f );
}
getMyStrings();
getStrings();
}
}
class amend implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String sLine = new String();
String sLine1 = new String(tf.getText());
String sLine2 = new String(pf.getText());
try
{
int x=0;
for(int i=0;i<myl;i++)
{
if((sLine2.equals(""))||(sLine1.equals("")))x++;
if(sWord[i].compareTo(sLine1)==0&&x<1)
{
sLine=sLine+sLine1+"#"+sLine2+"\n";
x++;
}
else
{
sLine = sLine+myWord[i]+"#"+myMeaning[i]+"\n";
}
}
FileOutputStream out = new FileOutputStream(myBook);
byte buf[] = sLine.getBytes();
out.write(buf);
out.close();
System.out.println(sLine);
}
catch(Exception f)
{
System.out.println ("error" + f );
}
getMyStrings();
getStrings();
}
}
class delete implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String sLine = new String();
String sLine1 = new String(tf.getText());
System.out.println(sLine1);
try
{
for(int i=0;i<myl;i++)
{
if(sWord[i].equals(sLine1))
{}
else
{
sLine = sLine+myWord[i]+"#"+myMeaning[i]+"\n";
}
}
FileOutputStream out = new FileOutputStream(myBook);
byte fuf[] = sLine.getBytes();
out.write(fuf);
out.close();
}
catch(Exception f)
{
System.out.println ("error" + f );
}
System.out.println(sLine1);
getMyStrings();
getStrings();
}
}
class exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
f.show(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -