基于javaswing实现答题系统本⽂实例为⼤家分享了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;
javaswing实现购买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.(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
}
}
更多学习资料请关注专题《》。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论