import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.sql.*;
import java.io.*;
import javax.swing.*;

public class DataWindow extends JFrame implements ActionListener {
	JFrame topFrame;
	JDesktopPane desktop;
	FileDialog filesave;
	JTextField englishtext;
	JTextArea chinesetext;
	JButton b1,b2,b3,b4,b5;
	JLabel lable;
	JMenuBar mbar;
	JMenu mfile, medit,mhelp,mexit;
	JMenuItem edic,cdic,quit,add,mode,delete,hhelp,about, exit;
	JRadioButton Rb1,Rb2;
	ButtonGroup bg;
	
	DataWindow()
	{
		super("Ӣʵ");
		//this.setBounds(250,250,600,400);
		setSize(650,400);
		setVisible(true);
		setLocationRelativeTo(null);
		getContentPane().add(new JScrollPane(chinesetext));
		//getContentPane().setBackground(Color.blue);
		//chinesetext.setBackground(Color.BLACK);
	
		mbar=new JMenuBar();
		setJMenuBar(mbar);
        mbar.setOpaque(true);
        mfile=new JMenu("ļ");    //˵
        medit=new JMenu("༭");
        mhelp=new JMenu("");
        mexit=new JMenu("˳");
        mbar.add(mfile);
        mbar.add(medit);
        mbar.add(mhelp);
        mbar.add(mexit);
      
        edic=new JMenuItem("Ӣʵ");    //ļ˵
        cdic=new JMenuItem("Ӣʵ");
  
         quit=new JMenuItem("˳");
         add=new JMenuItem("Ӵʻ");   //༭˵
         mode=new JMenuItem("޸Ĵʻ");
       delete=new JMenuItem("ɾʻ");
       hhelp=new JMenuItem("");     //˵
       about=new JMenuItem("");
       exit=new JMenuItem("˳");
       
       mfile.add(edic);
       mfile.add(cdic);

       mfile.addSeparator();
       mfile.add(quit);
       medit.add(add);
       medit.add(mode);
       medit.add(delete);
       mhelp.add(hhelp);
       mhelp.add(about);
       mexit.add(exit);
       edic.addActionListener(this);
       cdic.addActionListener(this);
       quit.addActionListener(this);
       add.addActionListener(this);
       mode.addActionListener(this);
       delete.addActionListener(this);
       hhelp.addActionListener(this);
       about.addActionListener(this);
      exit.addActionListener(this);
       englishtext=new JTextField(16);
       englishtext.setFont(new Font("",15,15));
       chinesetext=new JTextArea(8,15);
       chinesetext.setEditable(false);
       chinesetext.setFont(new Font("",28,28));
       b1=new JButton("ѯ");
       b2=new JButton("");
       b3=new JButton("޸");
       b4=new JButton("ɾ");
     JPanel p1=new JPanel();
     JPanel p2=new JPanel();
     lable=new JLabel("ѯӢﵥʣ");
     Rb1=new JRadioButton("Ӣ뺺");
     Rb2=new JRadioButton("Ӣ");
     bg=new ButtonGroup();
     bg.add(Rb1);
     bg.add(Rb2);
     
     lable.setFont(new Font("",20,20));
     p1.add(lable);
     p1.add(englishtext);
     p1.add(b1);
     p1.add(Rb1,"North");
     p1.add(Rb2,"South");
     
     p2.add(b2);
     p2.add(b3);
     p2.add(b4);
    // setResizable(false);
     getContentPane().add(p1,"North");
     getContentPane().add(p2,"South");
   getContentPane().add(new JScrollPane(chinesetext),"Center");
     b1.addActionListener(this);
     b2.addActionListener(this);
     b3.addActionListener(this);
     b4.addActionListener(this);
     englishtext.addActionListener(this);
     
     
     
       
       
	}
	//ʵְťļ¼
	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
			{
				Listwords();
			}
			catch(SQLException ee)
			{
				JOptionPane.showMessageDialog(this, "صʧܣ","",JOptionPane.WARNING_MESSAGE);
				
			}
		}
	}
	else if(e.getSource()==quit||e.getSource()==exit)       //˳ť
	{
		if(JOptionPane.showConfirmDialog(null,"Ƿ˳")==JOptionPane.OK_OPTION)   
	    {
			System.exit(0);
		}
	}
	else if(e.getSource()==about)
	{
		JOptionPane.showMessageDialog(topFrame, "ӭʹӢʵ");
		
	}
	else if(e.getSource()==hhelp)
	{
		JOptionPane.showMessageDialog(topFrame,"ϸ¼վȡллʹã");
	}
	else if(e.getSource()==edic)
	{
		lable.setText("ѯӢﵥʣ");
		lable.setForeground(Color.black);
	
		b1.setVisible(true);
	}
	else if(e.getSource()==cdic)
	{
		lable.setText("ѯĺﵥʣ");
		lable.setForeground(Color.red);
		b1.setVisible(true);
	}
	
		
	
	else if(e.getSource()==b2||e.getSource()==add)
	{ add addword=new add();
	addword.validate();
	}
	else if(e.getSource()==b3||e.getSource()==mode)
	{
		mode modeword=new mode();
		modeword.validate();
	}
	else if(e.getSource()==b4||e.getSource()==delete)
	{
		delet deleteword=new delet();
		deleteword.validate();
	}
}
	public void Listwords() throws SQLException
	{
		String cname,ename;
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		   }
			catch(ClassNotFoundException e)
			{
			System.out.println("ForName:"+e.getMessage());
			}
			String conURL ="jdbc:odbc:TSDIC";
			Connection con=DriverManager.getConnection(conURL);
			Statement st=con.createStatement();
			if(lable.getText().equals("ѯӢﵥʣ"))
			{
			ResultSet re=st.executeQuery("select * from Words");
			while(re.next())
			{
				ename=re.getString("");
				cname=re.getString("");
				if(ename.equals(englishtext.getText()))
				{
					chinesetext.append(cname+'\n');
				}
			}
			con.close();
			if(chinesetext.getText().equals(""))
			{
				JOptionPane.showMessageDialog(this, "޴˵!","",JOptionPane.WARNING_MESSAGE);
			}
			
			}
			else if(lable.getText().equals("ѯĺﵥʣ"))
			{
				String ss="select * from Words where  like '%"+englishtext.getText()+"%'";
				ResultSet re=st.executeQuery(ss);
				
				while(re.next())
				{
					ename=re.getString("");
					cname=re.getString("");
					
					
						chinesetext.append(ename+"   "+cname+'\n');
					
				}
				con.close();
				if(chinesetext.getText().equals(""))
				{
					JOptionPane.showMessageDialog(this, "޴˵!","",JOptionPane.WARNING_MESSAGE);
				}
				
			}
	}
}


