?? 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 + -