学生点名系统

刚学java时做的一个小程序,写的挺乱的,到处乱创建对象。自己用来拿着玩还可以。


数据库:

1、JDBC连接数据库,用的Sql server数据库

2、实现查询

3、实现更新

4、stu表sname.sno,sok(初始为0,未到课时设置为1,退出系统时归0)


GUI:

1、Jframe主界面

2、JDialog查找界面:确认对话框JOptionPane

3、JDialog点名界面

4、JDialog未到名单界面


监听器:

1、主界面:查找、点名、名单、退出

2、点名界面:提交、到课、未到、last、next

学生点名系统_第1张图片

学生点名系统_第2张图片


学生点名系统_第3张图片

学生点名系统_第4张图片

学生点名系统_第5张图片


主界面:

package Guishow;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Database.MyDataBase;

public class MainJframe extends JFrame {

	/**
	* 
	*/
	private static final long serialVersionUID = 1L;

	private MyDataBase md = new MyDataBase();

	private NameJDialog njd = new NameJDialog();

	private JPanel mainjp;
	private JButton mainjb1;
	private JButton mainjb2;
	private JButton mainjb3;
	private JButton mainjb4;

	public MainJframe() {
		mainjb1 = new JButton("查找");
		mainjb2 = new JButton("点名");
		mainjb3 = new JButton("未到名单");
		mainjb4 = new JButton("退出");
		mainjp = new JPanel();
		mainjp.setLayout(new GridLayout(0, 2));
		mainjp.add(mainjb1);
		mainjp.add(mainjb2);
		mainjp.add(mainjb3);
		mainjp.add(mainjb4);
		add(mainjp, new BorderLayout().NORTH);
		setTitle("点名系统");
		ImageIcon img = new ImageIcon("image/聊城大学.jpg");
		JLabel jl = new JLabel(img);
		add(jl, new BorderLayout().CENTER);

		setSize(300, 370);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocation(400, 100);
		setResizable(false);
		setVisible(true);
		AddMainAction();
	}

	private void AddMainAction() {

		mainjb4.addActionListener(new ActionListener() {// 退出,将数据库sok设置为0

			@Override
			public void actionPerformed(ActionEvent arg0) {
				md.Setdatabase("update stu set sok=0");
				System.exit(0);
			}
		});

		mainjb3.addActionListener(new ActionListener() {// 未到名单,创建未到类的对象,将名单界面设置为显示

			@Override
			public void actionPerformed(ActionEvent e) {
				new WeiNameJDialog().setVisible(true);
				;
			}
		});

		mainjb2.addActionListener(new ActionListener() {// 点名,将点名界面设置为可见
			@Override
			public void actionPerformed(ActionEvent arg0) {
				njd.setVisible(true);
			}
		});

		mainjb1.addActionListener(new ActionListener() {// 查找,运用对话框

			@Override
			public void actionPerformed(ActionEvent e) {
				String findno;
				int selection;
				findno = JOptionPane.showInputDialog("请输入学号:");
				selection = JOptionPane.showConfirmDialog(null, "学号:" + findno + "?", "请确认", JOptionPane.YES_NO_OPTION);
				if (selection == JOptionPane.YES_OPTION) {
					try {
						ResultSet res = md.FindDatabase("select *from stu");
						while (res.next()) {
							String sno1 = res.getString("sno").trim();
							if (findno.equals(sno1)) {
								njd.Findstudent(sno1);
								break;
							}
						}
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
	}
}


点名界面:

package Guishow;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import Database.MyDataBase;

public class NameJDialog extends JDialog {
	private static final long serialVersionUID = 1L;

	private MyDataBase nd = new MyDataBase();
	private ResultSet res1 = nd.FindDatabase("select *from stu");

	private JLabel imagjl;
	private JPanel namejp;
private Icon []icons={........................};
	ArrayList imName = new ArrayList();
	ArrayList imNo = new ArrayList();
	private JButton nextjb = new JButton("Next");
	private JButton lastjb = new JButton("Last");
	private JButton tjb = new JButton("到课");
	private JButton fjb = new JButton("未到");
	private JButton okjb = new JButton("提交");
	private int index = 0;
	private int len = icons.length;

	public NameJDialog() {
		try {
			while (res1.next()) {
				String name1 = res1.getString("sname");
				String no1 = res1.getString("sno");
				imNo.add(no1);
				imName.add(name1);
			}
			res1.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		imagjl = new JLabel(imName.get(index), icons[index], SwingConstants.CENTER);
		imagjl.setVerticalTextPosition(SwingConstants.TOP);
		imagjl.setHorizontalTextPosition(SwingConstants.CENTER);
		add(imagjl, new BorderLayout().CENTER);

		namejp = new JPanel();
		namejp.setLayout(new GridLayout(0, 2));
		namejp.add(tjb);
		namejp.add(fjb);
		namejp.add(lastjb);
		namejp.add(nextjb);

		add(okjb, new BorderLayout().NORTH);
		add(namejp, new BorderLayout().SOUTH);

		setTitle("点名");
		setSize(220, 300);
		setLocation(500, 200);
		setVisible(false);
		AddNameAction();
	}

	private void AddNameAction() {

		okjb.addActionListener(new ActionListener() {// 提交,将点名界面设置为不可见

			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		nextjb.addActionListener(new ActionListener() {// next

			@Override
			public void actionPerformed(ActionEvent e) {
				index++;
				imagjl.setText(imName.get(index));
				imagjl.setIcon(icons[index]);
				if (index == len - 1) {
					nextjb.setEnabled(false);
				}
				if (index == 1) {
					lastjb.setEnabled(true);
				}
			}
		});

		lastjb.addActionListener(new ActionListener() {// last

			@Override
			public void actionPerformed(ActionEvent e) {
				index--;
				imagjl.setText(imName.get(index));
				imagjl.setIcon(icons[index]);
				if (index == 0) {
					lastjb.setEnabled(false);
				}
				if (index == len - 2) {
					nextjb.setEnabled(true);
				}
			}
		});

		fjb.addActionListener(new ActionListener() {// 未到,将数据库sok设置为1

			@Override
			public void actionPerformed(ActionEvent e) {
				String s = "update stu set sok=1 where sname=" + "'" + imName.get(index).trim() + "'";
				nd.Setdatabase(s);
			}
		});
	}

	public void Findstudent(String sno1) { // 根据学号查找学生信息,接受查找按钮传过来的学号
		for (int i = 0; i < imNo.size(); i++) {
			if (sno1.equals(imNo.get(i).trim())) {
				new FindJDialog(imName.get(i), imNo.get(i), icons[i]).setVisible(true);
				break;
			}
		}
	}
}



查找界面:

package Guishow;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class FindJDialog extends JDialog {
	/**
	* 
	*/
	private static final long serialVersionUID = 1L;
	private JLabel jname;// 存放姓名
	private JLabel jno;// 存放学号
	private JPanel jptop;// 存放姓名、学号标签
	private JLabel jlicon;// 存放图片

	public FindJDialog(String name, String no, Icon icon) {
		jname = new JLabel(name, SwingConstants.CENTER);
		jno = new JLabel(no, SwingConstants.CENTER);
		jptop = new JPanel();
		jptop.setLayout(new GridLayout(0, 1));
		jptop.add(jname);
		jptop.add(jno);
		add(jptop, new BorderLayout().NORTH);
		jlicon = new JLabel(icon);
		add(jlicon, new BorderLayout().CENTER);

		setTitle("查找");
		setLocation(500, 200);
		setSize(200, 250);
		setVisible(false);
	}
}



未到界面:

package Guishow;

import java.awt.GridLayout;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import Database.MyDataBase;

public class WeiNameJDialog extends JDialog {
	/**
	* 
	*/
	private static final long serialVersionUID = 1L;
	private JTextArea jta = new JTextArea("       姓名" + "\t        学号\n");
	private JScrollPane jsp = new JScrollPane(jta);// 设置滚动条
	private MyDataBase mdb = new MyDataBase();
	private ResultSet res = mdb.FindDatabase("select *from stu");

	public WeiNameJDialog() {
		setLayout(new GridLayout(0, 1));
		try {
			while (res.next()) {
				String name = res.getString("sname").trim();
				String sno = res.getString("sno");
				int n = res.getInt("sok");
				if (n == 1) {
					if (name.length() == 3)
						jta.setText(jta.getText() + "       " + name + "\t" + sno + "\n");
					else
						jta.setText(jta.getText() + "       " + name + "\t" + sno + "\n");

				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		add(jsp);
		setTitle("未到名单");
		setSize(200, 300);
		setLocation(600, 200);
		setVisible(false);
	}
}

连接数据库:

package Database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MyDataBase {
	Connection con;
	Statement sta;
	ResultSet res;

	public MyDataBase() {// 连接本地数据库
		try {
			con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=student", "fdw", "960926");
			sta = con.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public ResultSet FindDatabase(String s) {// 数据库查询
		try {
			res = sta.executeQuery(s);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}

	public void Setdatabase(String s) {// 数据库更新
		try {
			sta.executeUpdate(s);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

开始:

package Main;

import Guishow.MainJframe;

public class StartMain {

	public static void main(String[] args) {
		new MainJframe();
	}
}

你可能感兴趣的:(Java)