📄 zidian.java
字号:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.sql.*;
import java.io.*;
import javax.swing.JOptionPane;
public class zidian
{
public static void main(String args[])
{
windows windows=new windows();
}
}
class windows extends JFrame implements ActionListener
{
FileDialog f1,f2;//创建两个文件对话框
BufferedReader in;//读入缓冲
FileReader file_reader;//写出文文件对象
BufferedWriter out;//写出缓冲
FileWriter to;//读出文件对象
JMenuBar bar;
JMenu m1,m2;
JMenuItem i1,i2,i3,i4;
JTextField text1;
TextArea tex1;
JButton button1,button2,button3;
JLabel label1,label2;
Connection con=null;
Statement st=null;
windows()
{
super("小字典");
FlowLayout f=new FlowLayout();//设计布局格式
f.setVgap(6);//设置垂直间距
f.setAlignment(FlowLayout.CENTER);
setLayout(f);
setBounds(500,150,250,280);
setVisible(true);//组件的可见性
setResizable(false);//设置窗口是否可以调整大小
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }//建立jdbc-odbc桥接器
catch(ClassNotFoundException e){ }
try{ con=DriverManager.getConnection("jdbc:odbc:fsb","c","c");//连接到数据库
st=con.createStatement();}// //向数据库发送语句
catch(SQLException ee){ }
text1=new JTextField(20);
tex1=new TextArea(5,30);
label1=new JLabel("请输入单词:");
label2=new JLabel(" ");
button1=new JButton("添加");
button2=new JButton("查询");
button3=new JButton("清屏");
bar=new JMenuBar();//创建菜单
bar.setBackground(Color.orange);
m1=new JMenu("文件");
m2=new JMenu("查看");
i1=new JMenuItem("打开");
i1.setBackground(Color.orange);
i2=new JMenuItem("保存");
i2.setBackground(Color.orange);
i3=new JMenuItem("信息");
i3.setBackground(Color.orange);
i4=new JMenuItem("报告");
i4.setBackground(Color.orange);
m1.addSeparator();
m2.addSeparator();//为菜单项添加线
m1.add(i1);
m1.add(i2);
m2.add(i3);
m2.add(i4);
bar.add(m1);
bar.add(m2);
setJMenuBar(bar);
add(label1);
add(label2);
add(text1);
add(button1);
add(button2);
add(button3);
add(tex1);
Label label3=new Label("050341101 陈 朝 银 制 作");
label3.setBackground(Color.orange);
add(label3);
button1.addActionListener(this);//添加监听器
button2.addActionListener(this);
button3.addActionListener(this);
i1.addActionListener(this);
i2.addActionListener(this);
i3.addActionListener(this);
i4.addActionListener(this);
validate();//组件正确的显示出来
f1=new FileDialog(this,"打开对话框",FileDialog.LOAD);//文件打开对话框
f2=new FileDialog(this,"保存对话框",FileDialog.SAVE);//文件保存对话框
f1.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
f1.setVisible(false);
}
});//适配器
f2.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
f2.setVisible(false);
}
});
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e2)
{
System.exit(0);}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
//查询单词是否存在于数据库中
try
{
String cname,ename;
String s=text1.getText();
con=DriverManager.getConnection("jdbc:odbc:fsb","c","c");
ResultSet rs=st.executeQuery("SELECT * FROM biao ");
boolean boo=false;
int b=2;
while ((boo=rs.next())==true)
{
ename=rs.getString("xh");
cname=rs.getString("xm");
if(ename.equals(text1.getText())==true||cname.equals(tex1.getText())==true)
{
JOptionPane.showMessageDialog(this,"对不起,你添加的单词已存在!",
"确认对话框",JOptionPane.WARNING_MESSAGE);
text1.setText(text1.getText());
tex1.setText(tex1.getText());
b=1;
break;
}
}
con.close();//关闭数据库的连接
if(b!=1)//判断是否要添加
{
int n=JOptionPane.showConfirmDialog(this,"确认要添加吗?",
"确认对话框",JOptionPane.OK_CANCEL_OPTION);
if(n==JOptionPane.OK_OPTION)//确认要添加单词
{
try{
tianjia();
}
catch(SQLException ee){}
}
else if(n==JOptionPane.CANCEL_OPTION)//取消添、加单词
{
text1.setText(text1.getText());
tex1.setText(tex1.getText());
}
}
}
catch(SQLException ee){}
}
else if(e.getSource()==button2)//查询
{
try{
chaxun();
}
catch(SQLException ee){ }
}
else if(e.getSource()==button3)
{
text1.setText(null);
tex1.setText(null);
}
//打开文件
else if(e.getSource()==i1)
{
f1.setVisible(true);
tex1.setText(null);
String s;
if(f1.getFile()!=null)
{
try{
File file=new File(f1.getDirectory(),f1.getFile());
file_reader=new FileReader(file);
in=new BufferedReader(file_reader);
while((s=in.readLine())!=null)
tex1.append(s);
in.close();
file_reader.close();
}
catch(IOException ee){}
}
}
//保存文件
else if(e.getSource()==i2)
{
f2.setVisible(true);
if(f2.getFile()!=null)
{
try
{
File file=new File(f2.getDirectory(),f2.getFile());
to=new FileWriter(file);
out=new BufferedWriter(to);
out.write(text1.getText(),0,(text1.getText()).length());
System.out.println('\n');
out.write(tex1.getText(),0,(tex1.getText()).length());
out.flush();
out.close();
to.close();
}
catch(IOException ee){}
}
}
else if(e.getSource()==i3)
{
try{
Runtime ce=Runtime.getRuntime();
ce.exec("NOTEPAD.EXE information.txt");//用记事本打开文件
}
catch(Exception e3){ e3.printStackTrace();}
}
else if(e.getSource()==i4)
{
try{
Runtime ce=Runtime.getRuntime();
ce.exec("C:/Program Files/Microsoft Office/OFFICE11/WINWORD.EXE baogao.doc");//用记事本打开文件
}
catch(Exception e3){ e3.printStackTrace();}
}
}
//查询方法的实现
public void chaxun() throws SQLException
{
String cname,ename;
String s=text1.getText();
con=DriverManager.getConnection("jdbc:odbc:fsb","c","c");
ResultSet rs=st.executeQuery("SELECT * FROM biao ");//向数据库中发出查询的指令
boolean bool=false;
while((bool=rs.next())==true)
{
ename=rs.getString("xh");
cname=rs.getString("xm");
//判断数据库中是否存在该单词
if(ename.equals(text1.getText())||cname.equals(text1.getText()))
{
text1.setText(ename);
tex1.setText(cname);
break;
}
}
con.close();
if(bool==false)
{
tex1.setText("没有该单词");
}
}
//添加单方法的实现
public void tianjia() throws SQLException
{
String st2_1=text1.getText().trim();
String st2_2=tex1.getText().trim();
con=DriverManager.getConnection("jdbc:odbc:fsb","c","c");
st.executeUpdate("INSERT INTO biao VALUES ("+"'"+st2_1+"'"+","+"'"+st2_2+"'"+")");
//向数据库添加新的单词
con.close();
}//更新数据库
}
/*public class zidian
{
public static void main(String args[])
{
windows windows=new windows();
}
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -