java窗口化背英语单词练习(连接了数据库)

1、连接了SQL 2016数据库(jdbc)
2、采用窗口化方式
3、随机出现存储的单词
java窗口化背英语单词练习(连接了数据库)_第1张图片
java窗口化背英语单词练习(连接了数据库)_第2张图片
java窗口化背英语单词练习(连接了数据库)_第3张图片
java窗口化背英语单词练习(连接了数据库)_第4张图片
java窗口化背英语单词练习(连接了数据库)_第5张图片
/*
数据库连接
*/

package Simple_words;
import java.sql.*;
public class conSQL {
    public final static String url="jdbc:sqlserver://localhost:1433;databaseName=Studying";
//databaseName=你的数据库名称
    public final static String user="sa";//登录名
    public final static String password="sa";//登录密码

    public static Connection connection=null;
    public static PreparedStatement ps=null;
    public static ResultSet res=null;
    /*
     * 连接数据库*/
    public static void ConnectionSql(){
        //加载驱动
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("加载驱动成功!");
        }catch (Exception e1){
            e1.printStackTrace();
            System.out.println("加载驱动失败!");
        }
        //连接数据库
        try{
            connection= DriverManager.getConnection(url,user,password);
            System.out.println("连接数据库成功!");
        }catch (Exception e2){
            e2.printStackTrace();
            System.out.println("连接数据库失败!");
        }

    }

    /*
     * 查询操作(练习)*/
    public static int Checkaccount(String sql,String words[],String means[]){
        int len=0;
        try{
            ps=connection.prepareStatement(sql);
            res=ps.executeQuery();
            while(res.next()){
                words[len]=res.getString(1);
                means[len]=res.getString(2);
                len++;
                /*if(len==1)
                    return len;*/
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return len;
    }
    /*
     * 查询操作(管理)*/
    public static String Checkadm(String sql){
        String mean="";
        try{
            ps=connection.prepareStatement(sql);
            res=ps.executeQuery();
            while(res.next()){
                mean=res.getString(1);
                break;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return mean;
    }
    /*
     * 插入操作*/
    public static boolean Insertdata(String sql){
        try {
            ps=connection.prepareStatement(sql);
            if(ps.executeUpdate()>0){
               return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}

//主函数

package Simple_words;

public class words {
    public static void main(String[] args) {
        conSQL.ConnectionSql();//调用数据库连接
        new operation();
    }
}

//选择操作方式
package Simple_words;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class operation extends JFrame implements ActionListener {
    JButton admWord;//管理单词
    JButton practiceWord;//练习单词

    public operation(){
        //需要设置两个按钮和一个窗口
        //设置窗口
        setBounds(750,50,400,800);
        setTitle("主界面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);/
        setLocationRelativeTo(null);
        this.setVisible(true);
        init();//初始化
    }

    public void init(){
        setLayout(null);
        //设置两个按钮
        admWord = new JButton("管理单词");
        practiceWord = new JButton("练习单词");

		//设置按钮样式
        admWord.setBounds(100,200,200,100);
        admWord.setBackground(Color.YELLOW);
        admWord.setFont(new Font("华为楷体",1,30));
        admWord.addActionListener(this);

        practiceWord.setBounds(100,500,200,100);
        practiceWord.setBackground(Color.YELLOW);
        practiceWord.setFont(new Font("华为楷体",1,30));
        practiceWord.addActionListener(this);
		//加人布局
        add(admWord);
        add(practiceWord);
    }

    @Override
	    public void actionPerformed(ActionEvent e) {
	        JButton btn = (JButton)e.getSource();
			//判断点击的按钮
	        if(btn==admWord){
	            dispose();
	            new adm();//管理单词
	        }
	        else
	        {
	            dispose();
	            new select();//练习单词
	        }
	    }
    }

//管理窗口

package Simple_words;	
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class adm extends JFrame implements ActionListener {
	//设置按钮
    JButton findWord;//查询单词
    JButton addWord;//添加单词

	//设置输入框
    JTextField fw;//查询
    JTextField aw;//单词
    JTextField ac;//词意

    //设置提示
    JLabel jl1;
    JLabel jl2;
    JLabel jl3;

    public adm(){
        //需要设置两个按钮和一个窗口
        //设置窗口
        setBounds(750,50,800,800);	
        setTitle("管理单词界面");	
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );	
        setResizable(false);
        this.setVisible(true);	
        init();	
    }

    public void init(){	
        setLayout(null);
        //按钮样式
        findWord = new JButton("查询单词");
        findWord.setBounds(550,200,200,75);
        findWord.setBackground(Color.YELLOW);
        findWord.setFont(new Font("华为楷体",1,20));
        findWord.addActionListener(this);
        add(findWord);

        addWord = new JButton("添加单词");
        addWord.setBounds(550,350,200,75);
        addWord.setBackground(Color.YELLOW);
        addWord.setFont(new Font("华为楷体",1,20));
        addWord.addActionListener(this);
        add(addWord);


        //提示样式
        jl1 = new JLabel("根据单词:");
        jl1.setFont(new Font(("华为楷体"),1,20));
        jl1.setBounds(50,200,150,75);
        this.add(jl1);

        jl2 = new JLabel("输入单词:");
        jl2.setFont(new Font(("华为楷体"),1,20));
        jl2.setBounds(50,300,150,75);
        this.add(jl2);

        jl3 = new JLabel("输入词意:");
        jl3.setFont(new Font(("华为楷体"),1,20));
        jl3.setBounds(50,400,150,75);
        this.add(jl3);

        //输入框样式
        fw = new JTextField(20);
        fw.setBounds(230,200,275,75);
        fw.setFont(new Font("华为楷体",1,20));
        this.add(fw);

        aw = new JTextField(20);
        aw.setBounds(230,300,275,75);
        aw.setFont(new Font("华为楷体",1,20));
        this.add(aw);

        ac = new JTextField(20);
        ac.setBounds(230,400,275,75);
        ac.setFont(new Font("华为楷体",1,20));
        this.add(ac);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton)e.getSource();
        String mean ="";
        String sql = "";
        if(btn==findWord){//查询
            if(fw.getText().equals("")){//如果没有输入需要查询的单词
            //弹出框提示
                JOptionPane.showConfirmDialog(null,"请输入需要查询的单词!",null,JOptionPane.OK_OPTION);
            }
            else {
            //数据库查询操作
                sql = "select mean from English_Studying where word='"+fw.getText()+"'";
                mean=conSQL.Checkadm(sql);
                if(mean.equals("")){
                //弹出
                    JOptionPane.showConfirmDialog(null,"没有找到此单词!",null,JOptionPane.OK_OPTION);
                }
                else{
                    JOptionPane.showConfirmDialog(null,"词意为:"+mean,null,JOptionPane.OK_OPTION);
                }
            }
        }
        else //添加
        {
        //数据库插入操作
            sql = "insert into English_Studying values('"+aw.getText()+"','"+ac.getText()+"')";
            if(conSQL.Insertdata(sql)){
                JOptionPane.showConfirmDialog(null,"插入成功!",null,JOptionPane.OK_OPTION);
            }
            else{
                JOptionPane.showConfirmDialog(null,"插入失败",null,JOptionPane.OK_OPTION);
            }
        }
    }
}

//练习窗口(窗口都是类似)

package Simple_words;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class select extends JFrame implements ActionListener {

    JButton words;//单词按钮
    JButton means;//词意按钮

    public select(){
        //需要设置两个按钮和一个窗口
        setBounds(750,50,400,800);


        setTitle("选择练习方式");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setResizable(false);

        setLocationRelativeTo(null);

        this.setVisible(true);

        init();



    }

    public void init(){

        setLayout(null);


        words = new JButton("单词练习");

        means = new JButton("词意练习");

        words.setBounds(100,200,200,100);
        words.setBackground(Color.YELLOW);
        words.setFont(new Font("华为楷体",1,30));
        words.addActionListener(this);

        means.setBounds(100,500,200,100);
        means.setBackground(Color.YELLOW);
        means.setFont(new Font("华为楷体",1,30));
        means.addActionListener(this);


        add(words);

        add(means);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton)e.getSource();
        JFrame frame = new JFrame();

        frame.setBounds(10, 10, 800, 800);

        frame.setResizable(false);

        //关闭子窗口,父窗口不会关闭
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);


        if(btn == words){//选择单词

            frame.add(new study_English("单词练习"));

            frame.setVisible(true);//出现窗口
        }
        else{//选择词意


            frame.add(new study_English("词意练习"));

            frame.setVisible(true);//出现窗口

        }

    }

}

//练习单词操作窗口

package Simple_words;	
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;


public class study_English extends JPanel implements  ActionListener , KeyListener {

    String name;
    String name1,name2;//前面的放上面,后面的放下面

    String words[] ;//存储单词
    String means[] ;//存储意思
    int len = 0;//长度

    //把出现的单词存储起来
    String oldwords[] ;
    String oldmeans[] ;
    int oldlen=0;

    //随机出现的单词和意思
    String word = "";
    String mean = "";

    //存入错误单词和个数
    String errorwords[];
    String errormeans[];
    int errorwordnum = 0;

    //正确单词个数
    String correctwords[];
    String correctmeans[];
    int correctwordnum = 0;

    JButton btn1,btn2,btn3;//重新开始,提示,确定

    JTextField sr ;//输入框

    public study_English(String name){

		//根据需要练习的方式进行不同的显示
        this.name = name;
        if(name=="单词练习"){
            name1="单词:";
            name2="词意:";
        }
        else{
            name2="单词:";
            name1="词意:";
        }


        this.addKeyListener(this);
        //获取焦点
        this.setFocusable(true);
        	
        init();//初始化
        repeat();
    }


	//重置数据(重新开始的时候)
    public void repeat(){
        words = new String[2048];//存储单词
        means = new String[2048];//存储意思
        len = 0;//长度

        //把出现的单词存储起来
        oldwords = new String[2048];
        oldmeans = new String[2048];
        oldlen=0;

        //随机出现的单词和意思
        word = "";
        mean = "";

        //存入错误单词和个数
        errorwords=new String[2048];
        errormeans=new String[2048];
        errorwordnum = 0;

        //正确单词
        correctwords=new String[2048];
        correctmeans=new String[2048];
        correctwordnum = 0;

        //取出存在数据库中的单词
        String sql;
        sql = "select * from English_Studying";
        len=conSQL.Checkaccount(sql,words,means);//返回的是单词的个数
        sr.setEditable(true);//输入框可输入
        Roll_word();//随机单词
    }

    public void init(){

        setLayout(null);
		//按钮样式
        btn1=new JButton("重新开始");
        btn1.setBounds(300,650,200,75);
        btn1.setBackground(Color.PINK);
        btn1.setFont(new Font("华为楷体",1,30));
        btn1.addActionListener(this);
        this.add(btn1);

        btn2=new JButton("提示");
        btn2.setBounds(600,150,100,150);
        btn2.setBackground(Color.RED);
        btn2.setFont(new Font("华为楷体",1,30));
        btn2.addActionListener(this);
        this.add(btn2);

        btn3=new JButton("确定");
        btn3.setBounds(300,520,200,50);
        btn3.setBackground(Color.GREEN);
        btn3.setFont(new Font("华为楷体",1,30));
        btn3.addActionListener(this);
        this.add(btn3);

		//输入框样式
        sr = new JTextField(20);
        sr.setFont(new Font("华为楷体",1,30));
        sr.setBounds(210,430,500,50);
        this.add(sr);
        sr.addKeyListener(new KeyListener() {
                              @Override
                              public void keyTyped(KeyEvent e) {

                              }

                              public void keyPressed(KeyEvent e) {
                              //在输入框输入完单词\词意时直接敲击键盘回车进行判断
                                  if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                                    judge();//判断单词对错
                                    repaint();//重绘
                                  }
                              }
                                @Override
                                public void keyReleased(KeyEvent e) {

                                }
        });

    }


    public void paintComponent(Graphics g){
        super.paintComponent(g);//覆盖之前的

        //设置标题
        g.setFont(new Font("华文楷体", 1,50));

        g.setColor(Color.BLACK);

        g.drawString(name,300,50);

        g.drawRect(50,100,700,500);

        g.drawLine(50,350,750,350);

        //设置位置
        g.setFont(new Font("华文楷体", 1,30));
        g.setColor(Color.BLUE);


        //设置单词位置
        g.drawString(name1,100,160);
        g.drawString(name2,100,460);
        if(name1=="单词:"){
            g.drawString(word,200,160);
        }
        else{
            g.drawString(mean,200,160);
        }

        //剩余单词个数
        g.drawString("剩余单词个数:"+String.valueOf(len - oldlen),100,220);
        //正确单词个数
        g.drawString("正确单词个数:"+String.valueOf(correctwordnum),100,260);
        //错误单词个数
        g.drawString("错误单词个数:"+String.valueOf(errorwordnum),100,300);
    }
    /*
    *随机出现单词 */
    public void Roll_word(){
        //初始随机数
        Random random = new Random();
        int rollnum = 0 ;
        boolean repet=false;
        //随机数
        while(true) {
            rollnum = random.nextInt(len);//随机是数据库中输入单词的个数
            word=words[rollnum];
            mean=means[rollnum];

            for (int i = 0; i < oldlen; i++) {
                if(word==oldwords[i]&&mean==oldmeans[i]){
                    repet=true;
                    break;
                }
            }
            if(!repet) {
                //出现的存入
                oldwords[oldlen]=word;
                oldmeans[oldlen]=mean;
                oldlen++;
                break;
            }
            repet=false;//随机的单词不对的话需要重新判断
        }


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        //根据点击的按钮进行判断
        if (button == btn1) {//重新开始
            repeat();
        } else if (button == btn2) {//提示
            if (name1 == "单词:")
                JOptionPane.showConfirmDialog(null, name2 + mean, "提示", JOptionPane.INFORMATION_MESSAGE);
            else
                JOptionPane.showConfirmDialog(null, name2 + word, "提示", JOptionPane.INFORMATION_MESSAGE);

            //点击提示默认为错误
            //如果错误中不存在,存入
            if (errorwordnum==0||errorwords[errorwordnum - 1] != word) {
                errorwords[errorwordnum] = word;
                errormeans[errorwordnum] = mean;
                errorwordnum++;
            }
        } else {//确定
            judge();
        }
        repaint();//重绘
    }


    public void judge(){//进行判断单词的对错
        if(sr.getText().equals("")){
            System.out.println(sr.getText()+1);
            JOptionPane.showConfirmDialog(null, "输入不能为空!", null, JOptionPane.INFORMATION_MESSAGE);
        }
       else if (sr.getText().equals(mean) && name2.equals("词意:") || sr.getText().equals(word) && name2.equals("单词:")) {
            JOptionPane.showConfirmDialog(null, "正确!", "正确", JOptionPane.INFORMATION_MESSAGE);

            //如果没有存入错误单词里面--->输入正确
            if (errorwordnum==0||errorwords[errorwordnum - 1] != word) {
                correctwords[correctwordnum] = word;
                correctmeans[correctwordnum] = mean;
                correctwordnum++;
            }
            //清空输入框中的字符
            sr.setText("");
            if(oldlen0){
            //有错误,显示错误单词
            new show_words(errorwords,errormeans,errorwordnum);
        }
        else if(oldlen==len&&errorwordnum==0){
            //全对
            JOptionPane.showConfirmDialog(null,"牛批!全对!",null,JOptionPane.OK_CANCEL_OPTION);
            //键盘无法输入
            sr.setEditable(false);
        }
    }
    @Override
    /*
    * 点击回车和确定键效果一样*/
    public void keyTyped(KeyEvent e) {

    }
    @Override
    public void keyPressed(KeyEvent e) {
      /*  if (e.getKeyChar() == KeyEvent.VK_ENTER) {

        }*/
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

//显示错误单词----以表格形式显示

package Simple_words;

import javax.swing.*;
import java.awt.*;

public class show_words extends JFrame {

    public show_words(String words[],String means[],int len){
        JTable table;
        String  data[][] = new String[2048][2];//单词
        String name[]={"单词","词意"};//列表名
        for (int i = 0; i < len; i++) {
            data[i][0]=words[i];
            data[i][1]=means[i];
        }

        table = new JTable(data,name);
        Container con= getContentPane();
        getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);

       setSize(400,800);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setTitle("错误单词列表");
        setVisible(true);
    }
}

你可能感兴趣的:(java窗口化背英语单词练习(连接了数据库))