⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scoreframe.java

📁 上机考试答卷在软盘上
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class ScoreFrame extends JFrame{

     public static void main(String[] args){
        ScoreFrame sf=new ScoreFrame(args[0]);
        sf.setVisible(true);
     }
     
     //声明工作界面上的各种文本域和标签     
     JLabel idLabel,sumLabel,scoreLabel,docLabel;
     JTextField idField,sumField,docField,plateNumField;
     //提示窗口,当使用者有不当操作时给予出错提示
     JTextField prompt=new JTextField(40);
     //声明一个文本域数组保存各单项成绩
     JTextField[] score=new JTextField[8];
     //声明工作界面上的按纽
     JButton newB,sumB,saveB,loadB;
     
     JPanel loadPanel=new JPanel();//用于安排载入按钮
     JPanel idPanel=new JPanel();//安排学号和总分的面板
     JPanel scorePanel=new JPanel();//安排各项得分的面板
     JPanel actionPanel=new JPanel();//安排文件名及各种按钮的面板
     JPanel promptPanel=new JPanel();//提示面板
     
     JPanel judgePanel=new JPanel();//判卷面板用于安排以上四个面板,然后和答题文件面板一起安排到内容板
      
     int state=0;/*状态参量,0表示等待判新卷状态,1表示成绩录入状态,2表示计算过
               总分后的待存状态*/
     
     PrintWriter pw;//声明输出流类对象把学生成绩写入文件保存
     
     JTextArea paperShow;//答题文件显示窗口
     JScrollPane sp;
     
     //答题文件地址及文件选择器
     JFileChooser f;
     File file;
     
     ScoreFrame(String s){
       super("判上机卷");
       
       file=new File("d:\\test.txt");//初始化答题文件保存地址  
       f=new JFileChooser();//创建文件选择器对象
       
       /*获取用户使用信息和需求,如果用户想继续判卷则选择继续判卷,系统
         将从保存的上次判卷信息文件中读入上次结束时的盘号;若是初次使用
         者,则要求输入初始盘号。此过程中如果用户出错系统将给出提示,并
         要求重新选择*/
       int plateNum=-1;
       while(plateNum==-1){
         Object[] obj={"初次使用者","继续判卷"};
         Object selectedValue=JOptionPane.showInputDialog(null,"请您选择使用情况:","使用情况选择",JOptionPane.INFORMATION_MESSAGE,null,obj,obj[0]);       
         if(selectedValue.toString().equals("初次使用者")) {
       	     String str=JOptionPane.showInputDialog("请输入你的初始盘号:");
       	     try{
       	        plateNum=Integer.valueOf(str).intValue()-1;
       	     }catch(NumberFormatException nfe){
       	     	JOptionPane.showMessageDialog(null,"请输入正确格式的初始盘号!"); 
       	     }	  
       	 }
         if(selectedValue.toString().equals("继续判卷")){
       	     String str="";
             try{
             	FileReader d1=new FileReader("plateNum.txt");
       	        BufferedReader br1=new BufferedReader(d1);
       	        str=br1.readLine();
       	        br1.close();	           
             }catch(FileNotFoundException e){JOptionPane.showMessageDialog(null,"您还没有使用过本系统,请选择初次使用者进行操作!");}
              catch(IOException e){}
             try{
       	        plateNum=Integer.valueOf(str).intValue();
       	     }catch(NumberFormatException e){}
         }     
       }
       //初始化载入按钮,用于对答题进行定位
       loadB=new JButton("载入");
       loadPanel.add(loadB);
       loadB.addActionListener(new LoadL());
       
       //初始化答题文件显示窗口
       paperShow=new JTextArea(10,20);
       paperShow.setEditable(false);
       paperShow.setBackground(Color.pink);
       paperShow.setLineWrap(true);
       sp=new JScrollPane(paperShow);

       //初始化准考证和总分面板
       idLabel=new JLabel("准考证号:");
       idField=new JTextField(15);
       idField.setFont(new Font("Serif",Font.BOLD,15));
       idField.setEditable(false);   idField.setBackground(Color.white);       sumLabel=new JLabel("总分:");
       sumField=new JTextField(15);
       sumField.setEditable(false);  sumField.setBackground(Color.white);
       
       idPanel.add(idLabel);    idPanel.add(idField);
       idPanel.add(sumLabel);   idPanel.add(sumField);

       //初始化各项得分面板
       scoreLabel=new JLabel("输入各项得分:");
       scorePanel.add(scoreLabel);
       for(int i=0;i<8;i++){
           score[i]=new JTextField(4);
           score[i].setEditable(false);
           score[i].setBackground(Color.white);
           scorePanel.add(score[i]);
        }

       //初始化按钮面板
       docLabel=new JLabel("文件名:");    actionPanel.add(docLabel); 
       docField=new JTextField(9);   
       docField.setEditable(false);  docField.setBackground(Color.white);
       docField.setText(s);          actionPanel.add(docField); 
       plateNumField=new JTextField(5);     
       plateNumField.setText(String.valueOf(plateNum));  
       plateNumField.setBackground(Color.white);
       actionPanel.add(plateNumField);
       newB=new JButton("判新卷");    actionPanel.add(newB);     
            newB.addActionListener(new NewL());
       sumB=new JButton("计算总分");  actionPanel.add(sumB);
            sumB.addActionListener(new SumL());
       saveB=new JButton("本卷判毕"); actionPanel.add(saveB);
            saveB.addActionListener(new SaveL());

      //初始化提示面板
      prompt.setEditable(false); 
      prompt.setBackground(Color.white);            
      prompt.setForeground(Color.red); 
      prompt.setFont(new Font("Serif",0,14)); 
      prompt.setText("请点击判新卷按钮开始判卷!");                       
      promptPanel.add(prompt);
      
      //安排判卷面板
      judgePanel.setLayout(new GridLayout(5,1));
      judgePanel.add(loadPanel);
      judgePanel.add(idPanel);  
      judgePanel.add(scorePanel);
      judgePanel.add(actionPanel);
      judgePanel.add(promptPanel);
      
      //安排主界面
      Container contentPane=getContentPane();
      contentPane.setLayout(new GridLayout(2,1));
      contentPane.add(sp);
      contentPane.add(judgePanel);
      
      //创建输出流对象
      try{
       FileOutputStream fis=new FileOutputStream(s,true);
       pw=new PrintWriter(fis);
      }catch(FileNotFoundException e1){prompt.setText("找不到指定文件!");}       
      
      addWindowListener(new WL());//安装窗口事件监听器
      
      setSize(500,400);//设置窗口大小
    }
    
    //清空各项得分文本域,总分窗口和答题文件显示窗口
    void clear(){
    	paperShow.setText("");
    	sumField.setText("");
    	prompt.setText("");
    	for(int i=0;i<8;i++){
    	    score[i].setText("");
    	}
    } 	    	
    
    /*保存成绩方法,将学生的盘号,学号和总分保存到指定文件。成功保存后
    系统状态变为等待判新卷状态*/
    void save(){
           String jl=plateNumField.getText()+" "+idField.getText()+"  "+sumField.getText();
           pw.println(jl);     
    }
    
    //类LoadL用于监听‘载入‘按钮,对答题文件进行定位
    class LoadL implements ActionListener{
       public void actionPerformed(ActionEvent e){
         int v=f.showOpenDialog(ScoreFrame.this);
         if(v==JFileChooser.APPROVE_OPTION){
            file=f.getSelectedFile();
         }
       }
    }
    
    //类NewL用于监听‘判新卷’按钮的动作事件,作为类ScoreFrame的内部类
    class NewL implements ActionListener{
       public void actionPerformed(ActionEvent e){  	 
           //如果系统处于初始状态,则读入下一个同学的学号和答题文件	
        if(state==0){ 
           //清空各文本域
           clear(); 	
           //读入下一个同学的学号,并显示到学号域中
           try{
              FileInputStream fis=new FileInputStream("d:\\zkz\\test20.txt");
              InputStreamReader isr=new InputStreamReader(fis);
              BufferedReader br=new BufferedReader(isr);
              String s=(String)br.readLine();
              idField.setText(s);
              br.close();
           }catch(FileNotFoundException ioe1){prompt.setText("不能找到指定文件");}
            catch(IOException ioe2){prompt.setText("全部卷已经判毕!");}
           /*读入下一个同学的答题文件并显示到答题文件窗口中
           系统默认每位同学的答题文件都存在d:\test.txt,当然具体操作时
           可以把保存地址换一下,例如a:\test.txt,点击判新卷前通过载入
           按钮选择答题文件*/
           try{
              BufferedReader cs=new BufferedReader(new FileReader(file));
              String s=cs.readLine();
              while(s!=null){
              	 paperShow.append(s+"\n"); 
              	 s=cs.readLine();
              }
              cs.close();  
           }catch(IOException ioe2){prompt.setText("无法读入答题文件!");} 
           //盘号加一
           int plateNum=Integer.valueOf(plateNumField.getText()).intValue();
           String number=String.valueOf(plateNum+1);
           plateNumField.setText(number); 
           //系统状态变为录入
           state=1;
           for(int i=0;i<8;i++){score[i].setEditable(true);}
        }
        else{prompt.setText("请将本卷判完并点击本卷判毕按钮将成绩保存再判新卷!");}              
       }     
    }
    
    //类SaveL用于监听’计算总分‘按钮的动作事件,作为类ScoreFrame的内部类
    class SumL implements ActionListener{
        public void actionPerformed(ActionEvent e){
        int sum=0;	        
       for(int i=0;i<8;i++){
              String s=score[i].getText();
              //检查是不是没项都判过了,并给分了
              if(s.equals("")){
                prompt.setText("请将所有试题都判完,再计算总分!");
                return;
              }
           double cj;
           //检查所给分数是否合乎要求,如应不大于5	
           try{
              cj=Double.valueOf(s).doubleValue();
           }catch(NumberFormatException nfx){prompt.setText("请给出正确格式的分数!");return;}   
           if(cj>5){
             prompt.setText("错误,各项分数不能超过5!请审查!");
             return;
           }	
           sum+=cj;
       }
        //把计算出来的总分显示到总分域里
         sumField.setText(String.valueOf(sum));
         //系统状态变为待存 
         state=2;
         	
      }
    }
    
    //类SaveL用于监听‘本卷判毕‘按钮的动作事件,作为类ScoreFrame的内部类
    class SaveL implements ActionListener{
        public void actionPerformed(ActionEvent e){	
           // 如果为等待判新卷状态,系统提示判新卷 	
           if(state==0) prompt.setText("请判新卷!");
           //如果还没计算总分,系统提示要求先计算总分	
           else if(sumField.getText().equals("")){
           	  prompt.setText("请先计算总分!");
           	  return;
                }
           /*如果系统处于待存状态,则将当前同学的成绩保存,设置状态为等待判新卷状态*/
           if(state==2) {
           	save(); 
           	state=0;
           	for(int i=0;i<8;i++){score[i].setEditable(false);}
            }	
           
        }
    }
  	
    //类WL用于监听ScoreFrame的窗口事件    
    class WL extends WindowAdapter{
        public void windowClosing(WindowEvent e){
             //创建输出流对象用于保存用户的使用信息即上次最后一份试卷的盘号	
             BufferedWriter bw1;
             try{
                 FileWriter rw1=new FileWriter("plateNum.txt");
                 bw1=new BufferedWriter(rw1);
             }catch(IOException ex){return;}    
             //如果系统处于等待判新卷状态或初始状态,直接退出
             if(state==0){
             	try{
             	   bw1.write((String)plateNumField.getText());
             	   bw1.close();
             	   pw.close();
             	}catch(IOException ex){}   
             	System.exit(0);
             } 
             //如果系统处于待存状态,保存成绩后退出
             else if(state==2) {
             	     save();
             	     try{
             	         bw1.write((String)plateNumField.getText());
             	         bw1.close();
             	         pw.close();
             	     }catch(IOException ex){}   
             	     System.exit(0);
             	  } 
             //如果系统处于录入状态,不正常退出,则注明情况,
             else if(state==1){
             	     sumField.setText("无分,该同学试卷未改完时便退出了,请下次重判!");
             	     save();
             	     try{
             	         bw1.write((String)plateNumField.getText());
             	         bw1.close();
             	         pw.close();
             	     }catch(IOException ex){}    
             	     System.exit(0);
             	  }  	     
        } 
    }    
}

   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -