public class TestFrame extends JFrame {
/**
 * 主界面
 */
JPanel jpanel_top, jpanel_center, jpanel_bottom, jpanel_choise, jpanel_quetion, jpanel_answer, jpanel_ans_choise;
JLabel jl_top_title, jl_top_username, jl_top_userid, jl_top_Number, jl_quetion;
JButton jbtn_bottom_back, jbtn_bottom_next, jbtn_bottom_complete, jbtn_Start;
JScrollPane jscroll_quetion, jscroll_answer;
JTextArea jta_quetion, jta_answer;
JRadioButton jrb_A, jrb_B, jrb_C, jrb_D;
ButtonGroup butgroup;
String uid, uname;
static ArrayList<String> arraylist1, arraylist2;// 题目编号,答案
static int tempcount = 0, total = 0;// 题号,成绩
static String choiseAbcd = null, tempdn = null;// 选择和答案
static int[] result = new int[25];// 考生答案
static int[] result1 = new int[25];// 标准答案 public TestFrame(String stuName, String stuId) {
this.uname = stuName;
this.uid = stuId;
this.setTitle("模拟考试");
this.setBounds(0, 0, 1024, 768);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void goTestFrame() {
/**
 * 顶部信息
 */
jpanel_top = new JPanel();
jpanel_top.setLayout(null);
jpanel_top.setBounds(10, 5, 1000, 150);
jpanel_top.setBorder(BorderFactory.createTitledBorder("考生信息"));
jl_top_title = new JLabel("NIIT");
jl_top_title.setBounds(30, 50, 100, 50);
jl_top_title.setFont(new Font("MS Mincho", 50, 50));
jl_top_userid = new JLabel();
jl_top_userid.setBounds(150, 20, 150, 25);
jl_top_userid.setText("考生学号:" + uid);
jl_top_username = new JLabel();
jl_top_username.setText("考生姓名:" + uname);
jl_top_username.setBounds(150, 50, 150, 25);
jl_top_Number = new JLabel();
jl_top_Number.setBounds(150, 80, 150, 25);
jbtn_Start = new JButton("开始考试");
jbtn_Start.setBounds(150, 120, 100, 25);
jbtn_Start.addActionListener(new actionStart());
jpanel_choise = new JPanel(new FlowLayout());
jpanel_choise.setBounds(300, 10, 690, 130);
jpanel_choise.setBorder(BorderFactory.createTitledBorder("快速选题"));
jpanel_top.add(jpanel_choise);
jpanel_top.add(jl_top_Number);
jpanel_top.add(jl_top_title);
jpanel_top.add(jl_top_userid);
jpanel_top.add(jl_top_username);
jpanel_top.add(jbtn_Start);
this.add(jpanel_top); /**
 * 中部信息
 */
jpanel_center = new JPanel();
jpanel_center.setLayout(null);
jpanel_center.setBounds(10, 160, 1000, 500);
jpanel_center.setBorder(BorderFactory.createTitledBorder(null, "题目信息", TitledBorder.LEADING, TitledBorder.TOP,
new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
/**
 * 题目
 */
jta_quetion = new JTextArea();
jta_quetion.setLineWrap(true);
jta_quetion.setEditable(false);
jta_quetion.setBackground(null);
jscroll_quetion = new JScrollPane(jta_quetion);
jscroll_quetion.setBounds(15, 20, 975, 180);
jpanel_center.add(jscroll_quetion); /**
 * 答案
 */
jta_answer = new JTextArea();
jta_answer.setEditable(false);
jta_answer.setLineWrap(true);
jta_answer.setBackground(null);
jscroll_answer = new JScrollPane(jta_answer);
jscroll_answer.setBounds(15, jscroll_quetion.getY() + 185, 975, 240);
jpanel_ans_choise = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpanel_ans_choise.setBounds(15, jscroll_quetion.getY() + 430, 975, 30);
butgroup = new ButtonGroup();
jrb_A = new JRadioButton("A");
jrb_B = new JRadioButton("B");
jrb_C = new JRadioButton("C");
jrb_D = new JRadioButton("D");
butgroup.add(jrb_A);
butgroup.add(jrb_B);
butgroup.add(jrb_C);
butgroup.add(jrb_D);
jpanel_center.add(jscroll_answer);
jpanel_center.add(jpanel_ans_choise);
this.add(jpanel_center); /**
 * 底部信息
 */
jpanel_bottom = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpanel_bottom.setBounds(10, 660, 1000, 70);
jpanel_bottom.setBorder(BorderFactory.createTitledBorder(null, "操作信息", TitledBorder.LEADING, TitledBorder.TOP,
new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
jbtn_bottom_back = new JButton("上一题");
jbtn_bottom_back.setEnabled(false);
jbtn_bottom_back.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
tempcount--;
goStart();
tempResult(); }
});
jbtn_bottom_next = new JButton("下一题");
jbtn_bottom_next.setEnabled(false);
jbtn_bottom_next.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
tempcount++;
goStart();
tempResult();
}
});
jbtn_bottom_complete = new JButton("交卷");
jbtn_bottom_complete.setForeground(Color.red);
jbtn_bottom_complete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
// int tempComplete = JOptionPane
// .showConfirmDialog(null, "是否确认交卷", "提示",
// JOptionPane.YES_NO_CANCEL_OPTION);
// if (tempComplete == 0) {
// for (int i = 0; i < result.length; i++) {
// System.out.print("--\t\nresult:" + result[i]);
// System.out.print("--\t\nresult1:" + result1[i]);
// System.out.print("\t\n===" + arraylist1.get(i));
// // if (result[i] == result1[i]) {
// // total += 4;
// // }
// }
//
// System.out.print("得分:" + total);
// dispose();
// }
goEnd();
}
});
jpanel_bottom.add(jbtn_bottom_back);
jpanel_bottom.add(jbtn_bottom_next);
jpanel_bottom.add(jbtn_bottom_complete);
this.add(jpanel_bottom);
} /**
 * 开始考试
 */
class actionStart extends Conn implements ActionListener {
public void actionPerformed(ActionEvent e) {
arraylist1 = new ArrayList<String>();
arraylist2 = new ArrayList<String>();
con = getConn();
try {
String sql = "select * from test order by rand() limit 25";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
for (int i = 0; i < 24; i++) {
while (rs.next()) {
String tempid = rs.getString(1);
String tempAbcd = rs.getString(3) + rs.getString(4) + rs.getString(5) + rs.getString(6);
arraylist1.add(i, tempid);
arraylist2.add(i, tempAbcd);
result1[i] = rs.getInt(7);// 这里已经获得标准答案
System.out.println(result1[i]);// 这里打印是有值的
}
}
jbtn_Start.setEnabled(false);
jbtn_bottom_back.setEnabled(true);
jbtn_bottom_next.setEnabled(true);
addAbcd();// 加入选项
goStart();
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
sqlClose();
}
}
} public void goStart() {
/**
 * 设置上下题到顶变色开关
 */
if (tempcount == 0) {
jbtn_bottom_back.setEnabled(false);
} else {
jbtn_bottom_back.setEnabled(true);
}
if (tempcount == 24) {
jbtn_bottom_next.setEnabled(false);
} else {
jbtn_bottom_next.setEnabled(true);
}
/**
 * 
 */
jl_top_Number.setText("第  " + (tempcount + 1) + " 题");
Conn conn = new Conn();
conn.con = conn.getConn();
try {
String sql = "select * from test where t_id=?";
conn.psmt = conn.con.prepareStatement(sql);
conn.psmt.setString(1, arraylist1.get(tempcount));
conn.rs = conn.psmt.executeQuery();
if (conn.rs.next()) {
jta_quetion.setText(conn.rs.getString(2));
jta_answer.setText(arraylist2.get(tempcount));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.sqlClose();
}
} /**
 * 选择答案事件
 */
class choiseAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (jrb_A.isSelected()) {
result[tempcount] = 1;
}
if (jrb_B.isSelected()) {
result[tempcount] = 2;
}
if (jrb_C.isSelected()) {
result[tempcount] = 3;
}
if (jrb_D.isSelected()) {
result[tempcount] = 4;
}
}
} /**
 * 产生选题BUTTON方法
 */
public void choiseButton() {
for (int i = 1; i < 26; i++) {
jpanel_choise.add(new JButton(String.valueOf(i)));
}
this.setVisible(true);
} /**
 * 加ABCD方法
 */
public void addAbcd() {
jrb_A.addActionListener(new choiseAction());
jrb_B.addActionListener(new choiseAction());
jrb_C.addActionListener(new choiseAction());
jrb_D.addActionListener(new choiseAction());
jpanel_ans_choise.add(jrb_A);
jpanel_ans_choise.add(jrb_B);
jpanel_ans_choise.add(jrb_C);
jpanel_ans_choise.add(jrb_D);
} /**
 * 临时存放答案
 */ public void tempResult() {
switch (result[tempcount]) {
case 0:
butgroup.clearSelection();
break;
case 1:
jrb_A.setSelected(true);
break;
case 2:
jrb_B.setSelected(true);
case 3:
jrb_C.setSelected(true);
break;
case 4:
jrb_D.setSelected(true);
break;
}
} /**
 * 交卷方法
 */
public void goEnd() {
int tempComplete = JOptionPane.showConfirmDialog(null, "是否确认交卷", "提示", JOptionPane.YES_NO_CANCEL_OPTION);
if (tempComplete == 0) {
for (int i = 0; i < 25; i++) {
// System.out.print("--\t\nresult:" + result[i]);
System.out.println("result1:" + result1[i]);// 这里打印的值却是不对的
// System.out.print("\t\n===" + arraylist2.get(i));
// if (result[i] == result1[i]) {
// total += 4;
// }
} // System.out.print("得分:" + total);
dispose();
}
}
}代码比较乱.我想问的是什么我在开始事件里就已经给result1[]的所有的元素赋值了,为什么在下面的public void goEnd()方法里result1[]却没有值呢?
百思不得其解