https://coding.net/u/zachary0320/p/java_test1_git/git
题目要求:
- 记录用户的对错总数,程序退出再启动的时候,能把以前的对错数量保存并在此基础上增量计算。
- 有计时功能,能显示用户开始答题后的消耗时间。
- 界面支持中文简体/中文繁体/英语,用户可以选择一种。
流程图:
关键代码:
记录用户做错题目,在JFrame的History中可以看见:
1 JTextArea textArea = new JTextArea(); 2 textArea.addAncestorListener(new AncestorListener() { 3 public void ancestorAdded(AncestorEvent event) { 4 File file = new File("./test.txt"); 5 6 FileReader fw = null; 7 fw = new FileReader(file); 8 BufferedReader br = new BufferedReader(fw); 9 String line = ""; 10 try { 11 while((line = br.readLine()) != null){ 12 textArea.append(br.readLine()+" "); 13 } 14 } catch (IOException e) { 15 e.printStackTrace(); 16 } 17 try { 18 br.close(); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } 22 }
设置计时功能,在ShowTimeApp的类中可以实现:
1 //显示时间的Jpanel 2 lp = new LabelPanel(); 3 getContentPane().add(new TimePanel()); 4 5 // 显示 6 setVisible(true); 7 8 // 设置时间变化的任务 9 Timer timer = new Timer(); 10 timer.schedule(new ShowTime(), new Date(), 1000); 11 12 //其他调用的类 13 class ShowTime extends TimerTask { 14 public void run() { 15 // 刷新 16 lp.label.setText(sdf.format(new Date())); 17 repaint(); 18 } 19 } 20 21 class TimePanel extends JPanel { 22 public void paint(Graphics g) { 23 super.paint(g); 24 // 显示时间 25 long t = System.currentTimeMillis()-start-8*1000*3600; 26 g.drawString(sdf.format(t), 270, 20); 27 } 28 } 29 30 class LabelPanel extends JPanel { 31 JLabel label = new JLabel(); 32 33 public LabelPanel() { 34 add(label); 35 } 36 }
输入文本框回答后创建的事件:
1 JTextField answer_text = new JTextField(); 2 answer_text.addFocusListener(new FocusListener(){ 3 public void actionPerformed(FocusListener e) { 4 5 } 6 7 @Override 8 public void focusGained(FocusEvent e) { 9 // TODO Auto-generated method stub 10 result_label.setText("请输入答案"); 11 } 12 13 @Override 14 public void focusLost(FocusEvent e) { 15 // TODO Auto-generated method stub 16 answer = answer_text.getText(); 17 int i= (answer_text.getY()-20)/30; 18 if (answer_text.getText().equals(ans)) { 19 result_label.setText("正确"); 20 21 gener.getYourAnswers()[i] = "right"; 22 } 23 else{ 24 result_label.setText("错误"); 25 26 gener.getYourAnswers()[i] = "你做错了"; 27 } 28 if (i == N) { 29 //停止时间 30 } 31 32 } 33 } 34 );
点击提交后的创建的事件:
1 btnNewButton = new JButton("提交"); 2 btnNewButton.addActionListener(new ActionListener() { 3 public void actionPerformed(ActionEvent e) { 4 try { 5 writeInFile(); 6 } catch (FileNotFoundException e1) { 7 // TODO Auto-generated catch block 8 e1.printStackTrace(); 9 } catch (IOException e1) { 10 // TODO Auto-generated catch block 11 e1.printStackTrace(); 12 } 13 //退出窗口 14 }
写入文件的函数:
1 private void writeInFile() throws IOException { 2 // 写入文件 3 long date = System.currentTimeMillis(); 4 File file = new File("./test.txt"); 5 6 FileWriter fw = new FileWriter(file,true); 7 BufferedWriter bw = new BufferedWriter(fw); 8 bw.write(sdf2.format(date)+""); 9 bw.newLine(); 10 for (int j = 0; j < N; j++) { 11 String str = gener.getTitles()[j] +" "+gener.getAnswers()[j]+" "+gener.getYourAnswers()[j]+""; 12 bw.write(str); 13 System.out.println("here"); 14 bw.newLine(); 15 } 16 bw.close(); 17 }
GUI部分截图:
GUI主页面:
生成题目主页面:
查看历史主页面:
test.txt文件内容:
PSP
PSP2.1 |
Personal Software Process Stages |
Time (%) Senior Student |
Time (%) |
Planning |
计划 |
10 |
5 |
· Estimate |
估计这个任务需要多少时间 |
10 |
5 |
Development |
开发 |
85 |
90 |
· Analysis |
需求分析 (包括学习新技术) |
7 |
10 |
· Design Spec |
生成设计文档 |
5 |
6 |
· Design Review |
设计复审 |
4 |
6 |
· Coding Standard |
代码规范 |
3 |
3 |
· Design |
具体设计 |
10 |
12 |
· Coding |
具体编码 |
36 |
21 |
· Code Review |
代码复审 |
7 |
9 |
· Test |
测试(自我测试,修改代码,提交修改) |
13 |
23 |
Reporting |
报告 |
5 |
5 |
· |
测试报告 |
2 |
2 |
· |
计算工作量 |
2 |
1 |
· |
并提出过程改进计划 |
1 |
2 |
心得总结:
1、对GUI界面编程不熟悉,还需要加强;
2、GUI界面的控件操作要加强练习