📄 testgui.java
字号:
private void operationBoxSetting() {
operationBox = Box.createHorizontalBox();
operationBox.add(new JLabel("操作"));
operationBox.add(Box.createHorizontalStrut(5));
operationBox.add(Box.createHorizontalStrut(5));
// 查看分数
lookScore = new JButton("查看分数");
lookScore.setEnabled(false);
lookScore.addActionListener(this);
operationBox.add(lookScore);
// 读上一题
previous = new JButton("上一题");
previous.addActionListener(this);
previous.setEnabled(false);
operationBox.add(previous);
operationBox.add(Box.createHorizontalStrut(5));
// 读下一题
next = new JButton("下一题");
next.addActionListener(this);
next.setEnabled(false);
operationBox.add(next);
operationBox.add(Box.createHorizontalStrut(5));
// 查看所作题目
preview = new JButton("预览");
operationBox.add(preview);
preview.addActionListener(this);
operationBox.add(Box.createHorizontalStrut(5));
// 查找所做完的题目答案
search = new JButton("查找");
search.addActionListener(this);
search.setEnabled(false);
operationBox.add(search);
operationBox.add(Box.createHorizontalStrut(5));
// 将答案提交到服务器
submit = new JButton("提交");
submit.addActionListener(this);
operationBox.add(submit);
operationBox.add(Box.createHorizontalStrut(5));
operationBox.add(Box.createHorizontalGlue());
operationBox.add(answerBox);
operationBox.add(Box.createHorizontalGlue());
Border caozuoborder = BorderFactory.createTitledBorder(BorderFactory
.createLineBorder(Color.black, 1), "", TitledBorder.CENTER,
TitledBorder.ABOVE_TOP, new Font("GB_2312", Font.BOLD, 16),
Color.BLACK);
operationBox.setBorder(caozuoborder);
}
private void testBoxSetting() {
lastTimeL = new JLabel("离考试结束时间还有:");
lastTimeF = new JTextField(10);
lastTimeP = new JPanel();
lastTimeL.setForeground(Color.RED);
lastTimeF.setForeground(Color.RED);
lastTimeF.setEditable(false);
lastTimeP.add(lastTimeL);
lastTimeP.add(lastTimeF);
testBox = Box.createVerticalBox();
String a = new String();
testArea = new JTextArea(20, 10);
testArea.setEditable(false);
a = "天是透明的 因为雪慢慢的停了\n心是透明的 因为我不想隐藏了 \n天是透明的 因为黑夜已过去了 \n因为你对我笑了 所以想念很快乐 \n心是透明的 因为有你永远陪着 \n因为你决定爱了 所以等待也是值得\n你看不到我的苍凉,我依然带你去飞翔;\n你看不到我的迷惘,我依然带你去流浪; \n你找不到的地方,我和你一起跳舞、歌唱; \n你找不到的天堂,我把它拿来放在你手上。 \n";
// a="十里平湖霜满天\n寸寸青丝愁华年\n对月形单望相顾\n只羡鸳鸯不羡仙";
testArea.setText(a);
testArea.setForeground(Color.RED);
testArea.setFont(new Font("楷体_GB2312", Font.PLAIN, 24));
answerAndWorkBox = Box.createVerticalBox();
answerAndWorkBox.setSize(60, 30);
answerAndWorkBox.add(workBox, BorderLayout.NORTH);
answerAndWorkBox.add(new JScrollPane(testArea), BorderLayout.CENTER);
answerAndWorkBox.add(operationBox, BorderLayout.SOUTH);
testLabel = new JLabel("试 题");
testLabel.setFont(new Font("新宋", Font.PLAIN, 24));
testBox.setForeground(Color.BLUE);
testBox.add(Box.createVerticalStrut(10));
testBox.add(testLabel);
testBox.add(lastTimeP);
// testBox.add()
testBox.add(answerAndWorkBox);
}
private void inputBoxSetting() {
inputBox = Box.createHorizontalBox();
inputBox.setVisible(false);
inputBox.add(split_one, BorderLayout.CENTER);
Border border = BorderFactory.createTitledBorder(BorderFactory
.createLineBorder(Color.BLACK, 1), "学生考试系统",
TitledBorder.CENTER, TitledBorder.ABOVE_TOP, new Font(
"楷体_GB2312", Font.BOLD, 28), Color.BLACK);
inputBox.setBorder(border);
}
private void bottomBoxSetting() {
ActionListener time = new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispTime.setText(new java.util.Date().toString());
}
};
new Timer(1000, time).start();
bottomBox = Box.createHorizontalBox();
dispTime = new JLabel();
dispTime.setHorizontalAlignment(JLabel.LEFT);
dispTime.setForeground(Color.BLACK);
position.setHorizontalAlignment(JLabel.RIGHT);
position
.setText("ctj_2制作 济南大学 计算0207 2002091048 &^-^& ");
bottomBox.add(position);
bottomBox.add(dispTime);
}
public void actionPerformed(ActionEvent e) {
String ip = ipField.getText().trim();
String user = userField.getText().trim();
String pass = passField.getText().trim();
if (e.getSource() == callIP) { // 呼叫服务器
if (socket != null && in != null && out != null) // 消除以往的连接信息
{
try {
socket.close();
in.close();
out.close();
} catch (Exception ee) {
}
}
String login = user + "," + pass;
int port1 = 8001;
try { // 建立网络连接
socket = new Socket(ip, port1);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(login);
} catch (IOException ee) // 如果连接失败
{
ipField.setText("呼叫失败");
stateField.setText("你呼叫没有成功");
}
if (socket != null) // 如果连接成功,对返回信息进行分析;
{
try {
String loginInfo = in.readUTF().trim(); // 返回的信息
if (!loginInfo.equals("用户已经完成考试,不能再考试")) {
stateField.setText(loginInfo);
if (!loginInfo.equals("密码错误")) {
stateField.setText(loginInfo);
if (loginInfo.equals("已经达到最大连接数")) {
otherinfo.setText(loginInfo);
stateField.setText(loginInfo);
out.close();
in.close();
return;
} else { // 成功登录
stuinfo.setText(loginInfo+",\n你的学生号为:" + user);
stateField.setText(loginInfo);
queryButton.setEnabled(true);
beginExe.setEnabled(true);
out.close();
in.close();
}
} else {
stateField.setText(loginInfo);
passField.setText("");
userField.setText("请重新输入用户名和密码!");
}
} else {
stateField.setText(loginInfo);
queryButton.setVisible(true);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
} else if (e.getSource() == queryButton) { // 查询学生信息
stuinfo.setText("你正在查询,请稍候");
int port4 = 8004;
try {
socket = new Socket(ip, port4);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(user);
stuinfo.setText("请求信息已发送");
} catch (IOException ee) // 如果连接失败
{
ipField.setText("呼叫失败");
stateField.setText("你呼叫没有成功");
stuinfo.setText("你呼叫没有成功");
}
if (socket != null) // 如果连接成功
{
try {
String queryInfo = in.readUTF(); // 得到查询结果
stuinfo.setText(queryInfo);
out.flush();
out.close();
} catch (IOException e1) { // 出现异常
otherinfo.setText("数据传输错误");
}
}
} else if (e.getSource() == beginExe) { // 生成试题
String mgr = (String) subject.getSelectedItem();
if (mgr.equals("英语")) { // 选择英语
selectT("e");
} else if (mgr.equals("数学")) { // 选择数学
selectT("m");
}
stateField.setText("你已经选择了" + mgr + "试题");
testinfo.setText("你已经选择了" + mgr + "试题");
startTest.setEnabled(true);
} else if (e.getSource() == startTest) { // 开始考试
stateField.setText("考试开始,时钟计时");
timeFlag = true;
start();
testArea.setText(tempTest[0]);
startTest.setEnabled(false);
beginExe.setEnabled(false);
readNext.setEnabled(true);
previous.setEnabled(false);
search.setEnabled(false);
next.setEnabled(false);
} else if (e.getSource() == readNext) { // 读下一题目
tempStr1 = keyField.getText().trim();
uanswer += tempStr1 + "@";
if (n <= enu-1) {
System.out.println(tempStr1);
stateField.setText("第" + n + "题,你选择的答案是:" + tempStr1);
++n;
// 显示下一题
testArea.setText(tempTest[num]);
++num;
} else {
JOptionPane.showMessageDialog(null, "你已经答题完毕,请检查后提交!",
"确认完毕对话框", JOptionPane.INFORMATION_MESSAGE);
readNext.setEnabled(false);
preview.setEnabled(true);
submit.setEnabled(true);
}
} else if (e.getSource() == preview) { // 预览
answerinfo.setText("用户:" + user + ", 你选的试题为:" + sub + "\n答题个数为:" + enu
+ " 你的答案为:" + uanswer);
} else if (e.getSource()==search){
String testnum = JOptionPane.showInputDialog(null,"请输入你要查询的题目号");
if(testnum==null){
System.out.println("出错了");
return ;
}else{ // 查询HashMap搜索相应的题目号,将对应试题显示??????
}
}
else if (e.getSource() == submit) { // 提交答案
if (socket != null && in != null && out != null) // 消除以往的连接信息
try {
socket.close();
in.close();
out.close();
} catch (Exception ee) {
System.out.print("消除以往的连接");
}
try {
socket = new Socket(ip, port3);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(user + "," + sub + "," + uanswer + "," + enu);
stateField.setText("你已经提交完毕,请点击查看分数");
testinfo.setText("你已经提交完毕,请点击查看分数");
} catch (UnknownHostException e1) {
stateField.setText("未找到服务器");
} catch (IOException e1) {
stateField.setText("数据传输出错");
}
timeFlag = false;
lookScore.setEnabled(true);
} else if (e.getSource() == lookScore) { // 查看分数
try {
String s;
s = in.readUTF().trim();
StringBuffer sb = new StringBuffer(s);
String s1 = sb.toString();
JOptionPane.showMessageDialog(null, "用户:" + user + "你的得分为:"
+ s1, "查询分数对话框", JOptionPane.INFORMATION_MESSAGE);
stuinfo.setText("用户:" + user + "你的得分为:" + s1);
in.close();
submit.setEnabled(false);
preview.setEnabled(false);
readNext.setEnabled(false);
int temp = JOptionPane.showConfirmDialog(null, "您确认要关闭吗?",
"考试完毕,退出系统!", JOptionPane.YES_NO_OPTION);
if (temp == JOptionPane.YES_OPTION) {
System.exit(0);
} else if (temp == JOptionPane.NO_OPTION) {
return;
}
} catch (IOException e1) {
e1.printStackTrace();
}
} else if (e.getSource() == stuLog) { // 学生登录
enterButton.setEnabled(true);
} else if (e.getSource() == techLog) { // 教师登录
JFrame.setDefaultLookAndFeelDecorated(true);
new TechGui();
jf.setVisible(false);
} else if (e.getSource() == enterButton) { // 学生进入考试
inputBox.setVisible(true);
} else if (e.getSource() == version) { // 查看版本信息
JOptionPane.showMessageDialog(null, "ctj_2制作 &^-^& ",
"version1.0", JOptionPane.INFORMATION_MESSAGE);
} else if (e.getSource() == exit || e.getSource() == exitButton) { // 离开本系统
int temp = JOptionPane.showConfirmDialog(null, "您已经考试完毕,请退出本系统!",
"关闭系统!?", JOptionPane.YES_NO_OPTION);
if (temp == JOptionPane.YES_OPTION) {
System.exit(0);
} else if (temp == JOptionPane.NO_OPTION) {
return;
}
}
}
public void run() {
--minute;
while (minute >= 0 && timeFlag) {
try {
sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
second--;
if (second == 0) {
minute--;
second = 59;
}
lastTimeF.setText(Integer.toString(minute) + " : "
+ Integer.toString(second));
if (minute == 0 && second == 59) {
JOptionPane.showMessageDialog(null, "离考试结束还有1分钟,请抓紧答题!",
"时间提示对话框", JOptionPane.INFORMATION_MESSAGE);
}
if (minute == 0 && second == 0) {
System.exit(0);
}
}
}
public void selectT(String mgr) {
tempTest = new String[100];
String str1 = null, str2 = null;
if (socket != null && in != null && out != null) // 消除以往的连接信息
try {
socket.close();
in.close();
out.close();
} catch (Exception ee) {
otherinfo.setText("消除以往的连接");
}
try {
socket = new Socket(ip, port2);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
sub = mgr;
testArea.setText("");
stateField.setText(in.readUTF());
out.writeUTF(sub);
System.out.println("发送成功");
// 接收服务器传送来的题目数量
str1 = in.readUTF();
enu = Integer.parseInt(str1);
System.out.println(Integer.toString(enu));// 显示题目数量
// 接收服务器传过来的试题
str2 = in.readUTF();
System.out.println("试题:" + str2);
StringTokenizer stc = new StringTokenizer(str2, "^");
for (int i = 0; i <= enu; i++) {
String s = "";
if (stc.hasMoreTokens()) {
tempTest[i] = stc.nextToken().trim();
}
System.out.println("第" + i + "题\n" + tempTest[i]);
}
minute = Integer.parseInt(in.readUTF());
out.flush();
out.close();
} catch (NumberFormatException e) {
otherinfo.setText("数据传输格式错误");
} catch (UnknownHostException e) {
otherinfo.setText("未找到服务器");
} catch (IOException e) {
otherinfo.setText("数据传输错误");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -