📄 wordbox.java
字号:
package program;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import support.*;
public class wordbox extends JFrame
implements ActionListener,ItemListener {
/*自定义控件*/
public ALabel iclbl1=new ALabel("");
public ALabel iclbl2=new ALabel("");
public ALabel iclbl4=new ALabel("");
public ImageIcon im=new ImageIcon("ic1.gif");
public ImageIcon im2=new ImageIcon("ic2.gif");
public ImageIcon im3=new ImageIcon("ic3.gif");
public TextArea meantxt=new TextArea("TextArea");
public int meanheight=0;
//查询单词操作部分
public JButton insbtn=new JButton("Insert");//插入单词
public JButton delbtn=new JButton("Delete");//删除单词
public JButton serbtn=new JButton("Search");//查找单词
public JTextField wordtxt=new JTextField();//单词文本框
//查询过单词列表
public List reclst=new List();//查询过单词列表
public JButton rdelbtn=new JButton("Delete it");
//动态信息显示模块
// public ALabel mtxlbl=new ALabel
// ("BoxSpace[0][0]\nBoxLocation=[0]\nWord=[null]");//动态显示抽取的单词
public List storlst=new List();//当前所查询的柱体存储空间内的单词
//弹出标签
//public ALabel msglbl=new ALabel("message");
//背景图片lbl
public JLabel backlbl=new JLabel("BackImage");
//矩阵选择
public JComboBox choice1;
public JComboBox choice2;
String [] conter1=new String[60];
String [] conter2=new String[60];
/*自定义变量*/
private ImageIcon image;//背景图像
private WordList wlst;//单词库
private Word word;//单词个体
private Record record;//记录
private Timer t1;//特效控制计时器
private int chx=0,chy=0;//更新单词库列表
/*构造函数*/
public wordbox(){super();init();}
public wordbox(String str){super(str);init();}
public void init()
{/*初始化函数*/
for(int i=0;i<60;i++)
{
conter1[i]="MatrixX="+i;
conter2[i]="MatrixY="+i;
}
choice1=new JComboBox(conter1);
choice2=new JComboBox(conter2);
/*控件及窗体初始化*/
Container c=this.getContentPane();
c.setBackground(new Color(150,204,206));
c.setLayout(null);
c.add(wordtxt);
c.add(insbtn);
c.add(delbtn);
c.add(serbtn);
c.add(reclst);
c.add(storlst);
c.add(choice1);
c.add(choice2);
c.add(rdelbtn);
c.add(iclbl1);
c.add(iclbl2);
c.add(iclbl4);
c.add(meantxt);
this.setSize(330,450);
this.setResizable(false);
/*设定关闭窗体时操作*/
this.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{/*当关闭窗体时存储字典及查询记录信息*/
wlst.Save("words.txt");
record.setRecords(reclst.getItems());
record.Save("record.txt");
System.exit(1);}});
/*自变量初始化*/
wlst=new WordList();//创建字典对象
wlst.Load("words.txt");//加载字典文件
word=new Word();//创建单词对象
record=new Record();//创建记录对象
record.Load("record.txt");//加载记录文件
this.stolstUpdata(33,51);
layoutHelper(this.getSize());
this.reclstUpdata(record.getRecords());
/*设定时间特效开始*/
rdelbtn.setEnabled(false);
}
public void layoutHelper(Dimension f)
{/*自定义布局管理*/
int x=0,y=0,w=0,h=0;
/*背景图片管理backlbl*/
backlbl.setSize(f);//设定该控件大小
backlbl.setLocation(0,0);
backlbl.setIcon(image);//设定背景图片
/*单词查询模块wordtxt,insbtn,delbtn,serbtn*/
/*iclbl*/
x=2;y=2;
w=(int)f.getWidth()-x-9;h=23;
iclbl1.setSize(w,h);
iclbl1.setLocation(x,y);
iclbl1.setIcon(im);
/*wordtxt*/
x=2;y=25;
w=(int)f.getWidth()-x-9;h=23;
wordtxt.setSize(w,h);
wordtxt.setLocation(x,y);
wordtxt.setFont(new Font("TimesRoman",Font.PLAIN,12));
wordtxt.addActionListener(this);
/*insbtn*/
x=wordtxt.getX();
y=wordtxt.getHeight()+wordtxt.getY()+2;
w=wordtxt.getWidth()/3-1;h=23;
insbtn.setSize(w,h);
insbtn.setLocation(x,y);
insbtn.addActionListener(this);
/*delbtn*/
x=insbtn.getX()+insbtn.getWidth()+2;
y=wordtxt.getHeight()+wordtxt.getY()+2;
w=wordtxt.getWidth()/3-1;h=23;
delbtn.setSize(w,h);
delbtn.setLocation(x,y);
delbtn.addActionListener(this);
/*serbtn*/
x=delbtn.getX()+delbtn.getWidth()+2;
y=wordtxt.getHeight()+wordtxt.getY()+2;
w=wordtxt.getWidth()/3-1;h=23;
serbtn.setSize(w,h);
serbtn.setLocation(x,y);
serbtn.addActionListener(this);
/*meantxt*/
x=wordtxt.getX();
y=insbtn.getHeight()+insbtn.getY()+4;
w=wordtxt.getWidth();h=meanheight;//(int)f.getHeight()/5;
meantxt.setSize(w,h);
meantxt.setLocation(x,y);
meantxt.setFont(new Font("TimesRoman",Font.PLAIN,12));
/*iclbl2*/
x=wordtxt.getX();y=meantxt.getY()+meantxt.getHeight()+2;
w=(int)(f.getWidth())-x;h=23;
iclbl2.setSize(w,h);
iclbl2.setLocation(x,y);
iclbl2.setIcon(im2);
/*storlst*/
x=wordtxt.getX();y=iclbl2.getY()+iclbl2.getHeight()+2;
w=(int)f.getWidth()/2-4;
h=((int)f.getHeight()-y)-80;
storlst.setSize(w,h);
storlst.setLocation(x,y);
storlst.setFont(new Font("TimesRoman",Font.PLAIN,12));
storlst.addItemListener(this);
/*矩阵选择模块*/
/*choice1*/
x=storlst.getX();
y=storlst.getY()+storlst.getHeight()+2;
w=storlst.getWidth()/2;h=20;
choice1.setSize(w,h);
choice1.setLocation(x,y);
choice1.addItemListener(this);
/*choice2*/
x=choice1.getX()+choice1.getWidth()+3;
y=choice1.getY();
w=(storlst.getWidth()-x)+2;h=20;
choice2.setSize(w,h);
choice2.setLocation(x,y);
choice2.addItemListener(this);
/*reclst*/
x=storlst.getX()+storlst.getWidth()+2;
y=storlst.getY();
w=storlst.getWidth()-5;h=storlst.getHeight();
reclst.setSize(w,h);
reclst.setLocation(x,y);
reclst.setBackground(Color.WHITE);
reclst.addItemListener(this);
/*rdelbtn*/
x=reclst.getX();
y=reclst.getY()+reclst.getHeight()+2;
w=reclst.getWidth();h=20;
rdelbtn.setSize(w,h);
rdelbtn.setLocation(x,y);
rdelbtn.addActionListener(this);
/*iclbl4*/
x=wordtxt.getX();y=rdelbtn.getY()+rdelbtn.getHeight()+2;
w=(int)(f.getWidth())-x;h=23;
iclbl4.setSize(w,h);
iclbl4.setLocation(x,y);
iclbl4.setIcon(im3);
}
/*各类辅助函数*/
public void stolstUpdata(int x,int y)
{/*更新单词库列表中的单词*/
storlst.removeAll();
if(wlst.getWordsMount(x,y)>0)
{/*只有当检索的柱形空间内有单词时才进行更新*/
String []str=new String[wlst.getWordsMount(x,y)];//声明JLabel数组存储单词含义
for(int i=0;i<wlst.getWordsMount(x,y);i++)
{
str[i]="["+wlst.getWordbox(x,y,i+1).getEngWord()
+"] means :["+wlst.getWordbox(x,y,i+1).getChWord()
+"]";
storlst.add(str[i]);
}
/*storlst=new JList(str);
storlst.setFont(new Font("TimesRoman",Font.PLAIN,12));
stopane.getViewport().setView(storlst);*/
}
}
public void reclstUpdata(String []s)
{/*更新记录列表中的单词*/
//reclst=new JList(record.getRecords());
//recpane.getViewport().setView(reclst);
reclst.removeAll();
try{
for(int i=0;i<s.length;i++)
{
if(!s[i].equals(""))
reclst.add(s[i]);
}}catch(Exception e){};
}
public String strCut(String str,char ch1,char ch2)
{/*从制定字符串中截取指定标识符内的字符串*/
String temp="";
try{char [] ch=str.toCharArray();
int start=0,end=0;
for(int i=0;i<ch.length;i++)
{
if(ch[i]==ch1)start=i;
if(ch[i]==ch2){
end=i;
break;
}
}
for(int j=start+1;j<end;j++)temp+=ch[j];}catch(Exception e){}
return temp;
}
public boolean isRecorded(String str)
{/*判断记录列表中是否有该单词*/
String []item=reclst.getItems();
int conter=0;
try{
while(!str.equals(item[conter])&&conter<item.length)
{
conter++;
}}catch(Exception e){}
if(conter<item.length) return true;
return false;
}
/*各类单词操作辅助函数*/
public void insertWord(String eng,String ch)
{/*插入操作*/
word=new Word();
word.setEngWord(eng);
word.setChWord(ch);
word.setTimes(0);
wlst.InsertWord(word);
}
public void deleteWord(String eng)
{/*删除操作*/
word=new Word();
word.setEngWord(eng);
wlst.DeleteWord(word);
reclst.remove(word.getEngWord());
}
public void checkWord(String eng)
{/*查找操作*/
word=new Word();
word.setEngWord(eng);
wlst.CheckTheWord(word);
word=wlst.getTemp();
word.setTimes(word.getTimes()+1);
}
public void DisplayPart(int x,int y,Word w)
{/*展示模块设定*/
this.stolstUpdata(x,y);
choice1.setSelectedIndex(x);
choice2.setSelectedIndex(y);
this.repaint();
}
public void paint(Graphics g)
{/*重载绘图方法*/
layoutHelper(this.getSize());
super.paint(g);
}
/*各类事件监听器*/
public void actionPerformed(ActionEvent e)
{
if(!wordtxt.getText().equals("")&&e.getActionCommand().equals("Confirm"))
{/*插入单词按钮*/
insertWord(wordtxt.getText(),meantxt.getText());
wlst.wordDiged(word.getEngWord());
this.stolstUpdata(wlst.getOrder1(),wlst.getOrder2());
insbtn.setText("Insert");
meanheight=0;
this.repaint();
}
if(!wordtxt.getText().equals("")&&e.getActionCommand().equals("Insert"))
{
meanheight= meanheight=(int)this.getHeight()/5;
insbtn.setText("Confirm");
this.repaint();
meantxt.setText("");
}
if(e.getSource().equals(wordtxt))
{/*单词文本框*/
wordtxt.selectAll();
checkWord(wordtxt.getText());
String str="BoxSpace["+wlst.getOrder1()+"]["+wlst.getOrder2()+
"]\nBoxLocation=["+(storlst.getSelectedIndex()+1)
+"]\nWord=["+word.getEngWord()+"]\nMeans:"+
this.getStrSized(word.getChWord(),'\n',meantxt.getWidth()
,meantxt.getFont().getSize());
meantxt.setText(str);
meanheight=(int)this.getHeight()/5;
if(!isRecorded(word.getEngWord())&&!word.getEngWord().equals(""))
reclst.add(word.getEngWord());
wlst.wordDiged(word.getEngWord());
this.DisplayPart(wlst.getOrder1(),wlst.getOrder2(),word);
serbtn.setText("Close");
}
if(!delbtn.getText().equals("")&&e.getSource().equals(delbtn))
{/*删除单词*/
try{
deleteWord(wordtxt.getText());
wlst.wordDiged(word.getEngWord());
this.stolstUpdata(wlst.getOrder1(),wlst.getOrder2());}catch(Exception exp){}
}
if(e.getActionCommand().equals("Search"))
{/*查找单词*/
wordtxt.selectAll();
checkWord(wordtxt.getText());
String str="BoxSpace["+wlst.getOrder1()+"]["+wlst.getOrder2()+
"]\nBoxLocation=["+(storlst.getSelectedIndex()+1)
+"]\nWord=["+word.getEngWord()+"]\nMeans:"+
this.getStrSized(word.getChWord(),'\n',meantxt.getWidth()
,meantxt.getFont().getSize());
meantxt.setText(str);
meanheight=(int)this.getHeight()/5;
if(!isRecorded(word.getEngWord())&&!word.getEngWord().equals(""))
reclst.add(word.getEngWord());
wlst.wordDiged(word.getEngWord());
this.DisplayPart(wlst.getOrder1(),wlst.getOrder2(),word);
serbtn.setText("Close");
}
if(e.getActionCommand().equals("Close"))
{
wordtxt.selectAll();
meanheight=0;
this.layoutHelper(this.getSize());
serbtn.setText("Search");
}
if(e.getSource().equals(t1))
{/*时间特效*/
}
if(e.getSource().equals(rdelbtn))
{
try{reclst.remove(reclst.getSelectedItem());}
catch(Exception exp){}
if(reclst.getSelectedIndex()>0)
{
reclst.select(reclst.getSelectedIndex()-1);
}else{rdelbtn.setEnabled(false);}
}
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource().equals(storlst))
{/*单击storlst中的项目则进行查询该单词*/
checkWord(this.strCut(storlst.getSelectedItem(),'[',']'));
String str="BoxSpace["+wlst.getOrder1()+"]["+wlst.getOrder2()+
"]\nBoxLocation=["+(storlst.getSelectedIndex()+1)
+"]\nWord=["+word.getEngWord()+"]\nMeans:"+
this.getStrSized(word.getChWord(),'\n',meantxt.getWidth()
,meantxt.getFont().getSize());
meantxt.setText(str);
meanheight=(int)this.getHeight()/5;
wordtxt.setText(word.getEngWord());
if(!isRecorded(word.getEngWord())&&!word.getEngWord().equals(""))
reclst.add(word.getEngWord());
wlst.wordDiged(word.getEngWord());
this.repaint();
serbtn.setText("Close");
}
if(e.getSource().equals(reclst))
{/*单击reclst中的项目则进行查询该单词*/
checkWord(reclst.getSelectedItem());
String str="BoxSpace["+wlst.getOrder1()+"]["+wlst.getOrder2()+
"]\nBoxLocation=["+(storlst.getSelectedIndex()+1)
+"]\nWord=["+word.getEngWord()+"]\nMeans:"+
this.getStrSized(word.getChWord(),'\n',meantxt.getWidth()
,meantxt.getFont().getSize());
meantxt.setText(str);
meanheight=(int)this.getHeight()/5;
wordtxt.setText(word.getEngWord());
wlst.wordDiged(word.getEngWord());
this.DisplayPart(wlst.getOrder1(),wlst.getOrder2(),word);
rdelbtn.setEnabled(true);
serbtn.setText("Close");
}
if(e.getSource().equals(choice1))
{
chx=this.getInteger((String)choice1.getSelectedItem());
chy=this.getInteger((String)choice2.getSelectedItem());
this.stolstUpdata(chx,chy);
}
if(e.getSource().equals(choice2))
{
chx=this.getInteger((String)choice1.getSelectedItem());
chy=this.getInteger((String)choice2.getSelectedItem());
this.stolstUpdata(chx,chy);
}
}
public String getStrSized(String str,char c,int width,int fsize)
{
//按照控件的大小设定字符串的断点
int div=2*(width)/(fsize);
char [] ch=str.toCharArray();
String tempstr="";
try{
for(int i=1;i<ch.length+1;i++)
{
tempstr+=ch[i-1];
if(i!=1&&i%div==0)
{
tempstr+=c;
}
}}catch(Exception e){}
return tempstr;
}
public int getInteger(String str)
{
char[] ch=str.toCharArray();
String t="";
for(int i=8;i<ch.length;i++)
t+=ch[i];
return Integer.parseInt(t);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -