import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.awt.Point;
import java.lang.*;
import java.math.*;
import java.sql.*;
import java.io.*;
import java.io.File;
import java.net.URL;
import java.applet.*;
class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e) //正在关闭窗口时
{
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
System.exit(0);
}
}
class MyButtonListener4 implements ActionListener //排行榜对话框“确认”和出错“确定”按钮事件响应
{
public void actionPerformed(ActionEvent e)
{
Dialog d = (Dialog)((Button)e.getSource()).getParent();
d.setVisible(false);
d.dispose();
}
}
class MyWindowListener5 extends WindowAdapter //关闭子窗口事件响应
{
public void windowClosing(WindowEvent e)
{
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
}
}
class ErrorDialog extends Dialog //出错对话框
{
private String str;
public ErrorDialog(Dialog owner,String s)
{
super(owner);
str=s;
}
public void init()
{
setTitle("出错");
setModal(true);
setBounds(500,200,400,150);
setResizable(false);
Button b=new Button("确定");
b.setFont(new Font("隶书",Font.BOLD,20));
b.setBounds(170,100,60,30);
setLayout(null);
add(b);
b.addActionListener(new MyButtonListener4());
addWindowListener(new MyWindowListener5());
setVisible(true);
}
public void paint(Graphics g)
{
g.setFont(new Font("楷体",Font.BOLD,20));
g.drawString(str,20,60);
}
}
class PaiHangDialog extends Dialog //排行榜对话框
{
private Label l[];
public PaiHangDialog(Dialog owner)
{
super(owner);
setTitle("排行榜");
setModal(true);
setBounds(500, 200, 200, 400);
setResizable(false);
Button b = new Button("关闭");
b.setFont(new Font("隶书", Font.BOLD, 10));
b.setBounds(100, 360, 60, 30);
setLayout(null);
add(b);
l=new Label[5];
for(int i=0;i<5;i++)
{
l[i]=new Label(" ");
add(l[i]);
l[i].setBounds(25,50+i*45,150,30);
l[i].setBackground(Color.yellow);
l[i].setForeground(Color.red);
}
b.addActionListener(new MyButtonListener4());
addWindowListener(new MyWindowListener5());
}
public void SetText(String s[])
{
for(int i=0;i<5;i++)
{
l[i].setText(s[i]);
}
setVisible(true);
}
}
class ChengJiDialog extends Dialog //记录成绩对话框
{
private PaiHang ph=new PaiHang();
private Label l1;
private Label l2;
private TextField t=new TextField();
private Button b = new Button("确定");
public ChengJiDialog(Dialog owner)
{
super(owner);
setTitle("成功");
setModal(true);
setBounds(500, 200, 400, 300);
setResizable(false);
setLayout(null);
add(b);
b.setFont(new Font("隶书", Font.BOLD, 20));
b.setBounds(170, 260, 60, 30);
l1=new Label("成功!");
add(l1);
l1.setFont(new Font("隶书", Font.BOLD, 60));
l1.setBounds(100,40,180,80);
l1.setBackground(Color.yellow);
l1.setForeground(Color.red);
l2=new Label("请输入名子:");
add(l2);
l2.setFont(new Font("隶书", Font.BOLD, 20));
l2.setBounds(20,155,120,60);
l2.setForeground(Color.black);
add(t);
t.setBounds(150,170,150,30);
b.addActionListener(new MyButtonListener6());
addWindowListener(new MyWindowListener5());
setVisible(true);
}
class MyButtonListener6 implements ActionListener //成绩记录对话框“确定”按钮事件响应
{
private String str="";
private long l=0;
public void actionPerformed(ActionEvent e)
{ str=t.getText();
l=StopWatch.Gete()-StopWatch.Gets();
ph.adddate(str,l);
Dialog d = (Dialog)((Button)e.getSource()).getParent();
d.setVisible(false);
d.dispose();
}
}
}
class MyMenuListener1 implements ActionListener //窗体关闭按钮监听器
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class StopWatch extends Canvas implements Runnable//计时器类
{
private static long startTime = 0;
private static long endTime = 0;
private static boolean bStart = false;
private String s;
private Dialog d;
public StopWatch()
{
setSize(150, 80);
}
public static void Set(long s, long e, boolean b)
{
startTime = s;
endTime = e;
bStart = b;
}
public static long Gets()
{
return startTime;
}
public static long Gete()
{
return endTime;
}
public void paint(Graphics g)
{
Font font1 = new Font("隶书", Font.BOLD, 50);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
java.util.Date elapsedTime = null;
try
{
elapsedTime = sdf.parse("00:00:00");
}
catch (Exception e) { }
elapsedTime.setTime(endTime - startTime + elapsedTime.getTime());
String display = sdf.format(elapsedTime);
s = display;
g.setFont(font1);
g.translate(0, 30);
g.setColor(Color.BLACK);
g.drawString(display, 10, 20);
}
public void run()
{
while (bStart)
{
try
{
Thread.sleep(500);
endTime = System.currentTimeMillis();
repaint();
}
catch (Exception e) { new ErrorDialog(d,"计时器出现错误!").init(); }
}
}
}
class PaiHang //排行榜类
{
private Dialog d;
private PaiHangDialog ph = new PaiHangDialog(d);
public void PaiHang()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/*
Access没有JDBC本地驱动,需要使用JDBC-ODBC桥,通过ODBC驱动程序来访问Access数据库
*/
}
catch (Exception e)
{
new ErrorDialog(d,"连接数据库失败!").init();
}
}
public void adddate(String str,long l)//添加成绩记录
{
File f = new File("Test.mdb");
if (f.exists())
{
String connStr = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)}; DBQ=" + f.getAbsolutePath();
try
{
Connection conn = DriverManager.getConnection(connStr, "", "");
String sql = "insert into accountList(name,time) values(?,?)"; //“?”表示待定参数
PreparedStatement ps = conn.prepareStatement(sql);
ps.clearBatch(); //清空批处理命令
ps.setString(2, str);
ps.setLong(3, l);
ps.addBatch(); //将一组参数添加到此PreparedStatement对象的批处理命令中
ps.executeBatch(); //执行批处理命令
ps.close();
conn.close();
}
catch (Exception e)
{
new ErrorDialog(d,"添加数据失败!").init();
}
}
}
public void print() //显示前5条记录
{
File f = new File("Test.mdb");
String str[]=new String[5];
int count=0;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
java.util.Date elapsedTime = null;
try
{
elapsedTime = sdf.parse("00:00:00");
}
catch (Exception e) {new ErrorDialog(d,"时间出错!").init(); }
if (f.exists())
{
String connStr = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)}; DBQ=" + f.getAbsolutePath();
try
{
Connection conn = DriverManager.getConnection(connStr, "", "");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("select * from accountList order by time desc" );
while (rs.next())
{
String s1 = Integer.toString(count+1) ;
String s2 = rs.getString(2) ;
elapsedTime.setTime(rs.getLong(3)+ elapsedTime.getTime());
String s3 = sdf.format(elapsedTime);
str[count] =s1+ " " + s2+" "+s3;
count++;
if(count>4)
break;
}
ph.SetText(str);
rs.close();
s.close();
conn.close();
}
catch (Exception e)
{
new ErrorDialog(d,"显示排行榜出错!").init();
}
}
}
}
class MoBan extends Panel //魔板类
{
private static int num = 3;
private st
- 1
- 2
前往页