import java.awt.*;
import java.net.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import sun.audio.*;
import javax.swing.JOptionPane;
class DataWindow extends JFrame implements ActionListener
{
JFrame topFrame;
JDesktopPane desktop;
FileDialog filedialog_save; //声明文件对话框
JTextField englishtext;
JTextArea chinesetext;
JButton b1,b2,b3,b4,b5;
JLabel label;
JMenuBar mbar;
JMenu mfile,medit,mhelp;
JMenuItem edic,cdic,back_data,quit,addedit,modedit,deledit,hhelp,about;
DataWindow()
{
super("英汉小词典");
this.setBounds(250,250,600,400);
this.setVisible(true);
getContentPane().add(new JScrollPane(chinesetext));
mbar = new JMenuBar();
setJMenuBar(mbar);
mbar.setOpaque(true);
mfile = new JMenu("文件");
medit = new JMenu("编辑");
mhelp = new JMenu("帮助");
mbar.add(mfile);mbar.add(medit);mbar.add(mhelp);
edic = new JMenuItem("英汉词典");
cdic = new JMenuItem("汉英词典");
back_data = new JMenuItem("备份词库");
quit = new JMenuItem(" 退出");//文件菜单
addedit = new JMenuItem("添加词汇");
modedit = new JMenuItem("修改词汇");
deledit = new JMenuItem("删除词汇");//编辑菜单
hhelp = new JMenuItem("帮助");
about = new JMenuItem("关于...");//帮助菜单
mfile.add(edic);
mfile.add(cdic);
mfile.add(back_data);
mfile.addSeparator();
mfile.add(quit); //mfile
medit.add(addedit);
medit.add(modedit);
medit.add(deledit); //medit
mhelp.add(hhelp);
mhelp.add(about); //mhelp
edic.addActionListener(this);
cdic.addActionListener(this);
quit.addActionListener(this);
addedit.addActionListener(this);
modedit.addActionListener(this);
deledit.addActionListener(this);
hhelp.addActionListener(this);
about.addActionListener(this);
englishtext = new JTextField(16);
englishtext.setFont(new Font("",15,15));
chinesetext = new JTextArea(8,15);
chinesetext.setEditable(false);
chinesetext.setFont(new Font("",25,25));
b1 = new JButton("查询");
b2 = new JButton("添加");
b3 = new JButton("修改");
b4 = new JButton("刪除");
b5 = new JButton("发音");
JPanel p1 = new JPanel(),p2 = new JPanel();//添加面板
label = new JLabel("输入要查询的英语单词:");
label.setFont(new Font("隶书",20,20));
p1.add(label);
p1.add(englishtext);p1.add(b1);p1.add(b5);
p2.add(b2);p2.add(b3);p2.add(b4);
this.add(p1,"North");
this.add(p2,"South");
this.add(new JScrollPane(chinesetext),"Center");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
englishtext.addActionListener(this);
back_data.addActionListener(this);
filedialog_save = new FileDialog(this,"保存文件对话框",FileDialog.SAVE);
filedialog_save.addWindowListener(new WindowAdapter() //对话框增加适配器
{
public void windowClosing(WindowEvent e)
{
(见教材)
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
}
public void actionPerformed(ActionEvent e) //实现按钮监听
{
if(e.getSource()==b1||e.getSource()==englishtext)
{
chinesetext.setText("");
if(englishtext.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"查询对象不能为空!","警告",
JOptionPane.WARNING_MESSAGE);
}
else{
try{ (见教材) }
catch(SQLException ee){ }
}
}
else if(e.getSource()==b2||e.getSource()==addedit)
{
AddWin add = new AddWin(); //AddWin是添加窗口的类
}
else if(e.getSource()==b3||e.getSource()==modedit)
{
ModifyWin modi = new ModifyWin(); //ModifyWin是修改窗口的类
}
else if(e.getSource()==b4||e.getSource()==deledit)
{
DelWin del = new DelWin(); //DelWin是删除窗口的类
}
else if(e.getSource()==b5)//发音
{
if(englishtext.getText()!=null)
{
try
{
InputStream is=getClass().getResource("sound//"+
englishtext.getText().trim()+".wav").openStream();
AudioPlayer.player.start(is);
}
catch(IOException e1){ }
}
}
else if(e.getSource()==edic) //英汉
{
label.setText("输入要查询的英语单词:");
b1.setVisible(true);
b5.setVisible(true);
}
else if(e.getSource()==cdic) //汉英
{
label.setText("输入要查询的汉语意思:");
b1.setVisible(true);
b5.setVisible(true);
}
else if(e.getSource()==back_data) //备份
{
File fromfile = new File("english\\english.mdb");
FileInputStream fis = null;
filedialog_save.setVisible(true);
try{
fis = new FileInputStream(fromfile);
int bytesRead; //定义变量来存储输入流中读取出来的文件
byte[] buf = new byte[4*1024]; //4K buffer
File tofile = new File(filedialog_save.getDirectory(),filedialog_save.getFile());
FileOutputStream fos = new FileOutputStream(tofile);
while((bytesRead = fis.read(buf))!=-1)
{
fos.write(buf,0,bytesRead);
}
fos.flush();
fos.close();
fis.close();
}
catch(IOException e2){ }
}
else if(e.getSource()==quit) //退出
{
System.exit(0);
}
else if(e.getSource()==about) //关于
{
final String AboutMsg = "A Electrical Dictionary 1.0 \n \n"
+"An application written to show off the function of dictionary.\n \n"
+"Written By Edith.\n \n"
+"Copyright (c) 2005 by Edith.All rights Reserved.";
JOptionPane.showMessageDialog(topFrame,AboutMsg);
}
else if(e.getSource()==hhelp) //帮助
{
HelpFrame help = new HelpFrame();
help.setVisible(true);
}
}
public void Listwords() throws SQLException //实现查询功能
{
String cname,ename;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
//建立桥接器
Connection Ex1Con = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=english\\english.mdb","","");
Statement Ex1Stmt = Ex1Con.createStatement();
if(label.getText().equals("输入要查询的英语单词:"))
{
ResultSet rs = Ex1Stmt.executeQuery("SELECT * FROM 表1");
while( rs.next())
{
ename = rs.getString("单词");
cname = rs.getString("解释");
if(ename.equals(englishtext.getText()))
{
chinesetext.append(cname+'\n');
}
}
Ex1Con.close(); //关闭数据库
if(chinesetext.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"查无此单词!","警告",
JOptionPane.WARNING_MESSAGE);
}
}
else if(label.getText().equals("输入要查询的汉语意思:"))
{
ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM 表1 WHERE 解释 LIKE '%"+englishtext.getText()+"%'");
while(rs.next())
{
ename = rs.getString("单词");
cname = rs.getString("解释");
chinesetext.append(ename+'\n');
}
Ex1Con.close(); //关闭数据库
if(chinesetext.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"查无此单词!","警告",
JOptionPane.WARNING_MESSAGE);
}
}
} // end of Liststudent()
}
public class Dic
{
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
DataWindow window = new DataWindow();
window.validate();
// AddWin add = new AddWin();
}
public void add()
{
}
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
评论0