java语言基础代码字典 java字典用法( 二 )


}
});
//备份词库
JMenuItem BackupItem = new JMenuItem("备份词库");
BackupItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ce) {
System.out.println(ce);
}
url = "jdbc:odbc:test";
String fileName;
chooser = new JFileChooser();
frame = new JFrame();
chooser.setDialogTitle("备份词库为");
try {
flag = chooser.showSaveDialog(frame);
} catch (HeadlessException h) {
System.out.println("Save File Dialog ERROR!");
}
if (flag == JFileChooser.APPROVE_OPTION) {
f = chooser.getSelectedFile();
fileName = chooser.getName(f);
fileName = chooser.getSelectedFile().getPath();
try {
File saveFile = new File(fileName);
FileWriter fw = new FileWriter(saveFile);
try
{
con = DriverManager.getConnection(url);
s = con.createStatement();
rs = s.executeQuery("SELECT * from 字典 ");
while (rs.next()) {
String a = rs.getString("单词");
String b = rs.getString("词语解释");
text2.append(a+b+"\n");
}
rs.close();
} catch (Exception ea) {
text2.setText("查询数据失败");
}
fw.write(text2.getText());
fw.close();
} catch (IOException ec) {
text2.setText(fileName);
}
text2.setText("词库备份成功!");
}
}
});
fileMenu.add(ECItem);
fileMenu.add(CEItem);
fileMenu.add(BackupItem);
fileMenu.addSeparator();
//退出监听器
fileMenu.add(new
AbstractAction("退出")
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
JMenu editMenu = new JMenu("编辑");
JMenuItem addItem = new JMenuItem("添加词汇");
addItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
//菜单栏增加监听器
addItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("没有输入内容!请重新增加?。。?);
else
{
String s=text1.getText().trim();
String s1=text2.getText().trim();
text2.setText(new add().jadd(text1.getText().trim(),text2.getText().trim()));
text2.append(s+"\n"+s1+";"+""+"//"+"该内容已增加?。。?);
}
}
});
//菜单栏修改监听器
JMenuItem updateItem = new JMenuItem("修改词汇");
updateItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.CTRL_MASK));
updateItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("没输入所要修改的内容!请重新输入?。。。?);
else{
String s=text1.getText().trim();
text2.setText(new update().jupdate(text1.getText().trim(),
text2.getText().trim()));
text2.append(s+""+"//"+"该内容已修改?。。?);
}
}
});
//菜单栏删除监听器
JMenuItem deleteItem = new JMenuItem("删除词汇");
deleteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
deleteItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("没输入所要删除的内容!请重新输入?。。。?);
else{
String s=text1.getText().trim();
text2.setText(new delete().jdelete(text1.getText().trim()));
text2.append(s+""+"//"+"该内容已删除?。。?);
}
}
});
editMenu.add(addItem);
editMenu.add(updateItem);
editMenu.add(deleteItem);

推荐阅读