?? web158_com.java
字號:
//選擇
//判斷選項//自己定義類
protected void getSelect(String str)
{
if(str.equals("A"))
{
examPanel.A.setSelected(true);
examPanel.B.setSelected(false);
examPanel.C.setSelected(false);
examPanel.D.setSelected(false);
}
if(str.equals("B"))
{
examPanel.A.setSelected(false);
examPanel.B.setSelected(true);
examPanel.C.setSelected(false);
examPanel.D.setSelected(false);
}
if(str.equals("C"))
{
examPanel.A.setSelected(false);
examPanel.B.setSelected(false);
examPanel.C.setSelected(true);
examPanel.D.setSelected(false);
}
if(str.equals("D"))
{
examPanel.A.setSelected(false);
examPanel.B.setSelected(false);
examPanel.C.setSelected(false);
examPanel.D.setSelected(true);
}
}//end of getSelect()
void initQuestion(String question,String a,String b,String c,String d,String answer)
{
examPanel.txtinfo.setText(question);
examPanel.txt_a.setText(a);
examPanel.txt_b.setText(b);
examPanel.txt_c.setText(c);
examPanel.txt_d.setText(d);
//是否已經答過
if(!answer.equals("web158.com"))
{
getSelect(answer);
}
//第一次答題,把原來答案清空
else
{
examPanel.A.setSelected(false);
examPanel.B.setSelected(false);
examPanel.C.setSelected(false);
examPanel.D.setSelected(false);
}
}//end of initQuestion
void getQuestion(int id,String flag)
{
//是否存在小于1的ID
if(id<1)
{
JOptionPane.showMessageDialog(null, "請選擇下一題,上一題不存在");
return;
}
//如果是操作題目,沒有上一條
System.out.println(totalchoose);
if(flag.equals("operate")&&id<=totalchoose)
{
JOptionPane.showMessageDialog(null, "請選擇下一題,上一題不存在");
return;
}
//開始數據庫操作
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String dbUrl="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=web158_exam/exam.dll";
Statement stmt=null;
Connection conn=null;
//加載驅動
//**************************
try
{
Class.forName(driver);
}
catch(java.lang.ClassNotFoundException nofound)
{
JOptionPane.showMessageDialog(null, "讀取試卷時數據驅動錯誤");
System.err.println(nofound.getMessage());
return ;
}
//開始添加試題
//**********************************
try{
//取選擇題目
if(flag.equals("choose"))
{
String sql="select * from exam where id="+id+"";
conn=DriverManager.getConnection(dbUrl,"web158.com","web158.com");
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
//如果題庫中存在題
if(rs.next())
{
String question=rs.getString("question");
String a=rs.getString("a");
String b=rs.getString("b");
String c=rs.getString("c");
String d=rs.getString("d");
int nowid=rs.getInt("id");
String stua=rs.getString("stuanswer");
//初始化試題
initQuestion(id+"、"+question,a,b,c,d,stua);
//寫上當前題號
nowexamid=nowid;
}
else
{
System.out.println(nowexamid);
totalchoose=nowexamid;//寫清選擇題目總數
int n=JOptionPane.showConfirmDialog(this, "選擇題已經答完,確定開始答操作題嗎?","確認",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
//開始操作題
initOperate();
//提交按鈕有效
web158_com_submit.setEnabled(true);
return ;
}
return ;
}
//關閉連接
stmt.close();
conn.close();
}
if(flag.equals("operate"))
{
String sql="select * from operate where id="+id+"";
conn=DriverManager.getConnection(dbUrl,"web158.com","web158.com");
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
//如果題庫中存在題
if(rs.next())
{
int nowid=rs.getInt("id");
String question=rs.getString("question");
//初始化試題
initQuestion_operate(id+"、"+question);
//寫上當前題號
nowexamid=nowid;
}
else
{
JOptionPane.showMessageDialog(null, "題目已經全部答完,如果需要更改請點擊上一題,不需要更改請提交試卷");
return ;
}
//關閉連接
stmt.close();
conn.close();
}
}
catch(Exception ef)
{
JOptionPane.showMessageDialog(null, "你的試卷存在問題,請聯系監考人員");
System.out.println(ef.getMessage());
return ;
}
finally
{
try
{
stmt.close();
conn.close();
}catch(Exception ef)
{}
}
}
//考試面板
void getExamPanel()
{
initAccess("update exam set stuanswer='web158.com'");
getQuestion(nowexamid,"choose");
//web158_info();
//timer.start();
//先添加按鈕事件
examPanel.A.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
getSelect("A");
}
});
examPanel.B.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
getSelect("B");
}
});
examPanel.C.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
getSelect("C");
}
});
examPanel.D.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
getSelect("D");
}
});
//添加上一題下一題目按鈕
examPanel.button_last.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(isSelect()==false)
{
JOptionPane.showMessageDialog(null, "請選擇答案");
return;
}
String answer=getNowAnswer();
initAccess("update exam set stuanswer='"+answer+"' where id="+nowexamid+"");
//JOptionPane.showMessageDialog(null, answer);
getQuestion(nowexamid-1,"choose");
}
});
//下一題目
examPanel.button_next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//是不是沒有選擇
if(isSelect()==false)
{
JOptionPane.showMessageDialog(null, "請選擇答案");
return;
}
else
{
String answer=getNowAnswer();
initAccess("update exam set stuanswer='"+answer+"' where id="+nowexamid+"");
//JOptionPane.showMessageDialog(null, answer);
getQuestion(nowexamid+1,"choose");
}
}
});
//添加
web158_com_Cpanel.removeAll();
web158_com_Cpanel.add(examPanel);
web158_com_Cpanel.repaint();
web158_com_Cpanel.validate();
}//end of getExamPanel()
/**************************************************************************************
*
*操作題目處理模塊
*/
void initQuestion_operate(String question)
{
operatePanel.txtinfo.setText(question);
}
//添加操作題Panel
void initOperate()
{
//提示信息
txtinfo.setText("操作題答題要求:\n\n把所有操作題作完后,請在D盤myexam文件夾中新建文件夾exam,把所有試卷答案文件存到exam文件夾后打包成為exam.rar,然后提交試卷!");
getQuestion(nowexamid+1,"operate");
//添加上一題下一題目按鈕
operatePanel.button_last.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
getQuestion(nowexamid-1,"operate");
}
});
//下一題目
operatePanel.button_next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
getQuestion(nowexamid+1,"operate");
}
});
//添加
web158_com_Cpanel.removeAll();
web158_com_Cpanel.add(operatePanel);
web158_com_Cpanel.repaint();
web158_com_Cpanel.validate();
}
//更新答案
void initAccess(String sql)
{
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String dbUrl="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=web158_exam/exam.dll";
Statement stmt=null;
Connection conn=null;
//加載驅動
//**************************
try
{
Class.forName(driver);
}
catch(java.lang.ClassNotFoundException nofound)
{
JOptionPane.showMessageDialog(null, "讀取試卷時數據驅動錯誤");
System.err.println(nofound.getMessage());
return ;
}
//開始添加試題
//**********************************
try{
conn=DriverManager.getConnection(dbUrl,"web158.com","web158.com");
stmt=conn.createStatement();
stmt.executeUpdate(sql);
//關閉連接
stmt.close();
conn.close();
}
catch(Exception ef)
{
JOptionPane.showMessageDialog(null, "初始化選擇題更新時失敗");
System.out.println(ef.getMessage());
return ;
}
finally
{
try
{
stmt.close();
conn.close();
}catch(Exception ef)
{}
}
}
//提交試卷
public void sendAnswer()
{
//需要返回的字
//String str="";
File f=new File("D:/myexam/exam.rar");
if(!f.exists())
{
JOptionPane.showMessageDialog(null, "您的操作題答案文件不存在,請檢查\n請查看"+f);
return;
}
if(createAnswer(getChoose()))
{
JOptionPane.showMessageDialog(null, "多人提交可能速度很慢,請耐心等待");
//開啟發送試卷信息
try
{
System.out.println("正在與服務器建立連接...");
socket=new Socket(serverip,sendport);
System.out.println("連接已經建立");
out = new PrintWriter(socket.getOutputStream(),true);
out.println(stuid+"¤"+stuname+"¤"+score);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(in.readLine()); //按行輸入
String order=in.readLine();
System.out.print(order);
//如果是成功剛開始發送答案
if(order.equals("oksend"))
{
try
{
//System.out.print("你發送的請求我收到,我現在給你發我的答案");
Thread.sleep(100);
nowSendAnswer("web158_exam/answer.dll");
Thread.sleep(100);
nowSendAnswer("D:/myexam/exam.rar");
//刪除文件
delExt();
JOptionPane.showMessageDialog(null, "試卷提交成功,請關閉系統退出考場,系統將自動在3分鐘后退出");
//初始退出面板
//頂上的空地
JLabel temp=new JLabel();
temp.setPreferredSize(new Dimension(510,120));
examed.add(temp);
examed_label.setText("考試完畢,請退出系統離開考場!");
examed_label.setFont(new Font("宋體",Font.BOLD,16));
examed.setPreferredSize(new Dimension(510,445));
examed.setOpaque(false);
examed.add(examed_label);
//更改面板
web158_com_Cpanel.removeAll();
web158_com_Cpanel.add(examed);
web158_com_Cpanel.repaint();
web158_com_Cpanel.validate();
//不許重提交
web158_com_submit.setEnabled(false);
//關閉開始
timer.stop();
exitTimer.start();
}catch(Exception ef)
{
System.out.println(ef.getMessage());
}
}
else
{
JOptionPane.showMessageDialog(null, "提交試卷出錯,請聯系監考人員手動提交");
}
out.close();
in.close();
socket.close();
//str="試題提取成功";
//return str;
}
catch(IOException e)
{
System.out.println("服務器沒有開啟"+e.getMessage()+serverip+":"+sendport);
JOptionPane.showMessageDialog(null, "提交試題失敗,與服務器:"+serverip+":"+sendport+"建立連接失敗...");
// return "提交試題失敗,與服務器:"+serverip+":"+sendport+"建立連接失敗...";
//return e.getMessage();
}
finally
{
try
{
socket.close();
out.close();
in.close();
}
catch(Exception ef)
{}
}
}
//return str;
}
//生成考試題文件
boolean createAnswer(String question)
{
try
{
//創建文件對象
File f=new File("web158_exam/answer.dll");
//如果沒有答案文件新建
if(!f.exists())
{
f.createNewFile();
}
FileWriter writeto=new FileWriter(f);
BufferedWriter out=new BufferedWriter(writeto);
out.write(question,0,question.length());
out.flush();
out.close();
writeto.close();
return true;
}
catch(IOException e)
{
System.out.print(e.getMessage());
return false;
}
}
//向服務器發送答案
public void nowSendAnswer(String f)
{
Socket clientSocket;
DataInputStream in;
DataOutputStream out;
try {
clientSocket = new Socket(serverip, receiveport);
File file = new File(f);
byte[] buffer = new byte[1024];
int length = 0;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -