基于java swing实现答题系统
程序员文章站
2023-12-16 10:18:52
本文实例为大家分享了java swing实现答题系统的具体代码,供大家参考,具体内容如下
如上图所示,代码亲测可用,如下:
import java.awt....
本文实例为大家分享了java swing实现答题系统的具体代码,供大家参考,具体内容如下
如上图所示,代码亲测可用,如下:
import java.awt.borderlayout; import java.awt.color; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.util.arraylist; import java.util.list; import java.util.random; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; /** * 2017-01-09 * @author koow * */ public class sa extends jframe implements actionlistener, keylistener { private jlabel title = null; private jbutton submit = null; private jpanel center = null; // items用来跳转 private int item1 = 0; private int item2 = 1; private int item3 = 2; private int item4 = 3; private jlabel[] formulalabels; // 用来存放表达式的各个项 private list<string> allresult; // 用来存放随机出来的结果 private jtextfield[] userresults; // 用来让用户输入结果 private jlabel[] judge; // 用来显示图标,以显示用户输入的结果是否正确 private list<string> inputresults = null; // 可以不要 private jtextfield scorefield = null; private int textcount = 1; // 用来记录当前焦点的位置 public static void main(string[] args){ sa s=new sa(); } public sa() { // todo auto-generated constructor stub inputresults = new arraylist<string>(); // 实例化 title = new jlabel("题目很简单,认真答哦!~(每题1分,答对加1分,答错不扣分!)"); submit = new jbutton("提交"); submit.addactionlistener(this); gridlayout layout = new gridlayout(21, 6); center = new jpanel(layout); // 调用方法实例化方法 initlables(); inituserrestult(); initjudge(); // 将各项填入到center面板(gridlayout) int count = 1; int formulacount = 0; int userresultscount = 0; int judgecount = 0; while (count <= 120) { // 判断count%6,确定要填哪个组件 if (count % 6 == 1 || count % 6 == 2 || count % 6 == 3 || count % 6 == 4) { center.add(formulalabels[formulacount]); formulacount++; } else if (count % 6 == 5) { center.add(userresults[userresultscount]); userresultscount++; } else if (count % 6 == 0) { center.add(judge[judgecount]); judgecount++; } count++; } center.add(new jlabel()); center.add(new jlabel()); center.add(new jlabel("总分:")); scorefield = new jtextfield(); scorefield.seteditable(false); center.add(scorefield); center.add(new jlabel()); center.add(new jlabel()); this.add(center, borderlayout.center); // 添加组件 this.add(submit, borderlayout.south); this.add(title, borderlayout.north); // 显示 this.setlocation(400, 10); this.setvisible(true); this.setsize(500, 700); this.setdefaultcloseoperation(jframe.exit_on_close); this.settitle("简单测试系统"); try { thread.sleep(500); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } userresults[0].requestfocus(); } /** * 这个方法用来初始化算式各项的数组 */ private void initlables() { int sum; int firstnumber; int secondnumber; allresult = new arraylist<string>(); // 建立算式标签 this.formulalabels = new jlabel[80]; // 将数组中的每一个标签都实例化 for (int i = 0; i < 80; i++) { formulalabels[i] = new jlabel(); } // 将表达式各项赋值内容,并将随机数的和算出结果存入arraylist中,以便后期核对结果使用 while (item4 < 80) { firstnumber = new random().nextint(99) + 1; formulalabels[item1].settext(string.valueof(firstnumber)); formulalabels[item2].settext("+"); secondnumber = new random().nextint(99) + 1; formulalabels[item3].settext(string.valueof(secondnumber)); formulalabels[item4].settext("="); sum = firstnumber + secondnumber; allresult.add(string.valueof(sum)); item1 += 4; item2 += 4; item3 += 4; item4 += 4; } // for(int i=0;i<80;i++){ // system.out.println(formulalabels[i].gettext()); // } } /** * 这个方法用来实例化userresults数组,并实例化里面的每一个jtextfield */ private void inituserrestult() { userresults = new jtextfield[20]; for (int i = 0; i < 20; i++) { userresults[i] = new jtextfield(); userresults[i].setsize(20, 20); // 后加的监听 userresults[i].addkeylistener(this); } } /** * 这个方法用来实例化judge数组,并将里面的每一个label实例化 */ private void initjudge() { judge = new jlabel[20]; for (int i = 0; i < 20; i++) { judge[i] = new jlabel("好好算呦"); } } // 实现监听方法,当用户点击提交后要做的事情 @override public void actionperformed(actionevent arg0) { // todo auto-generated method stub int score = 0; for (int i = 0; i < 20; i++) { // 将用户输入的结果全部获取,存入一个arraylist中,可以不要 inputresults.add(userresults[i].gettext().tostring().trim()); system.out.println(userresults[i].gettext().tostring()); // 取出来用户输入的答案和之前算好的结果去比较,不对就把后面的标签设置为回答错误, // 如果正确就设置为回答正确 string result = userresults[i].gettext().tostring().trim(); if (result.equals(allresult.get(i))) { judge[i].settext("回答正确"); judge[i].setforeground(color.red); score++; } else { judge[i].settext("回答错误"); judge[i].setforeground(color.green); } } scorefield.settext(string.valueof(score)); } @override public void keypressed(keyevent e) { // todo auto-generated method stub if (e.getkeycode() == keyevent.vk_enter) { userresults[textcount].requestfocus(); if (textcount < 19) { textcount++; } } } @override public void keytyped(keyevent e) { // todo auto-generated method stub } @override public void keyreleased(keyevent e) { // todo auto-generated method stub } }
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。