java实现课堂随机点名小程序

时间:2024-01-01 18:59:33

通过jdbc连接数据库实现读取学生花名册进行随机点名!

  ~jdbc连接mysql数据库  ||  注释部分代码可通过读取.txt文档实现显示学生信息

   ~通过点击开始按钮实现界面中间标签不断更新学生信息,当按下停止时显示唯一学生信息,并调用random对象函数实现随机抽取数据库学生信息

    ~界面功能简单,主要实现事件监听和线程实现与销毁。

java实现课堂随机点名小程序

java实现课堂随机点名小程序

//Menu.java

package a1;

import javax.swing.JFrame;

public class Menu {
public static void main(String[] args) { //主程序
randoms n = new randoms();
FromEx f=new FromEx();
n.AListener(f);
n.setResizable(false);
n.setTitle("学号点名小程序");
n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
n.setBounds(700, 200, 100, 200);
n.setSize(400, 300);
n.setVisible(true);
}
}

//randoms.java

package a1;
import java.awt.*;

import javax.swing.*;
import javax.swing.JFrame;

public class randoms extends JFrame{

JLabel num;//显示标题
JButton start,stop; // 开始、停止按钮
JLabel rs; //显示结果
JPanel jp1,jp2;
ExListener listener;
static boolean isSotp = true; //判断当前状态
public randoms() {
init();
}
void init(){

num = new JLabel("<<玩的就是心跳>>");
num.setFont(new Font("黑体", 2, 40));
start = new JButton("开始");
stop = new JButton("停止");
rs = new JLabel("0");

jp1 = new JPanel();
jp2 = new JPanel();
this.add(num,BorderLayout.NORTH);
jp1.add(rs);
this.add(jp1,BorderLayout.CENTER);
FlowLayout layout = new FlowLayout();//面板布局
layout.setAlignment(FlowLayout.CENTER);
jp1.setLayout(layout);
jp2.setLayout(layout);
jp2.add(start);
jp2.add(stop);
rs.setFont(new Font("宋体", 1, 30));//设置结果样式
rs.setForeground(Color.RED);
this.add(jp2,BorderLayout.SOUTH);
}
void AListener(ExListener listener){
this.listener=listener;
listener.set(start, stop, rs, isSotp);
start.addActionListener(listener); //添加监听
stop.addActionListener(listener);//添加监听

}
}

//Exlistener.java

package a1;

import java.awt.event.ActionListener;

import javax.swing.*;

public interface ExListener extends ActionListener{
public void set(JButton J1,JButton J2,JLabel rs,boolean isSotp);
}

//ExThead.java  线程的实现

package a1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import javax.swing.*;

public class ExThread extends Thread{
private JLabel rs;
boolean isSotp;
int n;
String a="";
//ArrayList<String> array=new ArrayList<String>();
Vector vector=new Vector();
int getn;
/*ExThread(JLabel rs,int n,boolean a){
this.rs = rs;
this.n = n;
isSotp=a;
}*/
void setbool(boolean a){isSotp=a;};
void setint(int a){n=a;};

ExThread(JLabel rs) throws IOException{

this.rs = rs;
v(vector);
//m(array);
/*        //注释部分读取.txt文档获取花名册信息
BufferedReader m=new BufferedReader(new FileReader("a.txt"));
while ((a = m.readLine()) != null) {
array.add(a);
}
getn=array.size();*/
}
public void v(Vector a){
a=vector;
try{
Class.forName("com.mysql.jdbc.Driver");//加载 MySQL 驱动程序
//建立与数据库的连接
String url = "jdbc:mysql://localhost:3306/conarry";

Connection cn = DriverManager.getConnection(url,"root","guoxiaotong");
Statement cmd = cn.createStatement();//创建 Statement 对象
System.out.println("123");
String sql="select * from student";
ResultSet r=cmd.executeQuery(sql);

while(r.next()){
String n1=r.getString(1);//获取数据库 第一行第一列
String n2=r.getString(2); //获取数据库 第一行第二列
String n3=n1+" "+n2; //连接学号和姓名
a.add(n3);

}
getn=a.size();
} catch (Exception e) {
e.printStackTrace();
}

}
/*public void m(ArrayList<String> array){
this.array=array;
try{
Class.forName("com.mysql.jdbc.Driver");//加载 MySQL 驱动程序
//建立与数据库的连接
String url = "jdbc:mysql://localhost:3306/conarry";

Connection cn = DriverManager.getConnection(url,"root","guoxiaotong");
Statement cmd = cn.createStatement();//创建 Statement 对象
System.out.println("123");
String sql="select * from student";
ResultSet r=cmd.executeQuery(sql);

while(r.next()){
String n1=r.getString(1);
String n2=r.getString(2);
String n3=n1+" "+n2;
array.add(n3);

}
getn=array.size();
} catch (Exception e) {
e.printStackTrace();
}

}*/
@Override
public void run() {
while(isSotp){
try {
Thread.sleep(500);

rs.setText((String) vector.get(getnumber(getn)));
System.out.println("1232");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static int getnumber(int n){ //根据你输入的总人数返回随机在人数范围内的一个学号
Random r = new Random();
int r1 = r.nextInt(n);
return r1;
}
}

//FromEx.java    事件监听接口实现 重写两个方法

package a1;

import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FromEx implements ExListener {
JButton start,stop;
JLabel rs;

static boolean isSotp;
ExThread t;
public void set(JButton J1,JButton J2,JLabel rs,boolean isSotp){
start=J1;
stop=J2;
this.rs=rs;
this.isSotp=isSotp;

try {
t= new ExThread(rs);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){

Object o = e.getSource();//获取触发事件对象实现监听映射。
if(o == start)  
{
//String number = num.getText();
try {
t= new ExThread(rs);
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
try{
//int n = Integer.parseInt(number);
isSotp = true;
//t.setint(n);
t.setbool(isSotp);
t.start();
}catch(NumberFormatException e2){
System.err.println("格式错误");

}
}else if(o == stop){
isSotp = false;
t.setbool(isSotp);
t.start();
System.out.println("this is stop");

}
}
}