📄 teacherframe.java
字号:
package com.cnu.cie.olts.client.teacher;import java.awt.BorderLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.JTextPane;import javax.swing.JToolBar;import javax.swing.WindowConstants;import javax.swing.SwingUtilities;import com.cnu.cie.olts.client.AboutJPanel;import com.cnu.cie.olts.client.BriefAnswerJPanel;import com.cnu.cie.olts.client.FillBlankJPanel;import com.cnu.cie.olts.client.ItemInfo;import com.cnu.cie.olts.client.Login;import com.cnu.cie.olts.client.TrueFalseJPanel;import com.cnu.cie.olts.client.multipleChoiceJPanel;import com.cnu.cie.olts.client.singleChoiceJPanel;public class TeacherFrame extends javax.swing.JFrame { private JMenuBar teacherMenuBar; private JMenuItem reconnectMenuItem; private JMenuItem helpMenuItem; private JScrollPane jScrollPane1; private static JPanel mainPanel; private JPanel titlepanel; private static JPanel centerpanel; private JTextPane textpagepane; private JTextField difficulttfone; private JTextField difficulttftwo; private JButton submitbt; private JButton mybutton; private JComboBox subjectcombobox; private JButton AddNewItemButton; private JButton ItemMaintenanceButton; private JButton reconnectButton; private JButton scoreAccountButton; private JButton starttestButton; private JToolBar teacherToolBar; private JMenu aboutMenu; private JMenuItem exitMenuItem; private JMenuItem scoreAccountMenuItem; private JMenuItem starttestMenuItem; private JMenu jMenu1; private JMenu ItemManageMenu; private JMenuItem AddNewItemMenuItem; private JMenuItem ItemMaintenanceMenuItem; String currentstate; Socket socket; BufferedReader bufreader=null; public static PrintWriter pswriter=null; static ItemInfo[] iteminfo; static int[] itemstate; static int unfinishitem=0; static int itemsnumber=0; public static int testscore=0; /** * Auto-generated main method to display this JFrame */ /*public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { // TeacherFrame inst = new TeacherFrame(); // inst.setLocationRelativeTo(null); // inst.setVisible(true); } }); } */ public TeacherFrame(Socket socket,BufferedReader bufreader,PrintWriter pswriter) { super(); initGUI(); // Dimension screen=Toolkit.getDefaultToolkit().getScreenSize(); // setLocation(((screen.width-this.WIDTH)/2),((screen.height-this.HEIGHT)/2)); this.socket=socket; this.bufreader=bufreader; this.pswriter=pswriter; } //实现关闭登录程序要进行的操作 protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { try { if(socket!=null&&!socket.isClosed()){ socket.close(); socket=null; } if(bufreader!=null) bufreader.close(); if(pswriter!=null) pswriter.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.exit(0); } } //添加新试题 private void AddNewItemButtonActionPerformed(ActionEvent evt) { //从通过服务器从数据库获取科目信息 String info="SUBJECT:"; info=new String(info.getBytes()); pswriter.println(info); pswriter.flush(); String returninfo = null; try { returninfo=bufreader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); return; } if(returninfo==null){ JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); return ; } System.out.println("subject ---"+returninfo); String[] subjects=returninfo.split("#_#"); info="ITEMTYPE:"; info=new String(info.getBytes()); pswriter.println(info); pswriter.flush(); returninfo = null; try { returninfo=bufreader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); e.printStackTrace(); return; } if(returninfo==null){ JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); return ; } System.out.println("itemtypes ---"+returninfo); String[] itemtypes=returninfo.split("#_#"); mainPanel.removeAll(); mainPanel.setLayout(new BorderLayout()); mainPanel.add(new AddNewItemInfoJPanel(subjects,itemtypes),BorderLayout.CENTER); mainPanel.validate(); mainPanel.repaint(); } private void ItemMaintaince(){ //从通过服务器从数据库获取科目信息 String info="SUBJECT:"; info=new String(info.getBytes()); pswriter.println(info); pswriter.flush(); String returninfo = null; try { returninfo=bufreader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); return; } if(returninfo==null){ JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); return ; } System.out.println("subject ---"+returninfo); String[] subjects=returninfo.split("#_#"); info="ITEMTYPE:"; info=new String(info.getBytes()); pswriter.println(info); pswriter.flush(); returninfo = null; try { returninfo=bufreader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); e.printStackTrace(); return; } if(returninfo==null){ JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); return ; } System.out.println("itemtypes ---"+returninfo); String[] itemtypes=returninfo.split("#_#"); centerpanel.removeAll(); centerpanel.add(new itemManageJPanel(subjects,itemtypes,iteminfo)); centerpanel.validate(); // mainPanel.removeAll(); // mainPanel.setLayout(new BorderLayout()); // mainPanel.add(titlepanel,BorderLayout.NORTH); // mainPanel.add(centerpanel,BorderLayout.CENTER); // mainPanel.validate(); // mainPanel.repaint(); } //已有试题信息维护 private void ItemMaintenanceButtonActionPerformed(ActionEvent evt) { currentstate="itemmaintainceing"; mainPanelLayout(); } //开始试题测试 private void starttestButtonActionPerformed(ActionEvent evt) { currentstate="testing"; mainPanelLayout(); } private void mainPanelLayout(){ String info="SUBJECT:"; info=new String(info.getBytes()); pswriter.println(info); pswriter.flush(); String returninfo = null; try { returninfo=bufreader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(returninfo==null){ JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); reconnectButton.setEnabled(true); reconnectMenuItem.setEnabled(true); return ; } String[] subjects=returninfo.split("#_#"); subjectcombobox.removeAllItems(); subjectcombobox.addItem("全部"); if(subjects.length>0) if("SUBJECT".equals(subjects[0])) for(int i=1;i<subjects.length;i++) subjectcombobox.addItem(subjects[i]); submitbt.setEnabled(true); mainPanel.removeAll(); mainPanel.setLayout(new BorderLayout()); mainPanel.add(titlepanel,BorderLayout.NORTH); centerpanel.removeAll(); centerpanel.validate(); mainPanel.add(centerpanel,BorderLayout.CENTER); mainPanel.validate(); mainPanel.repaint(); } private void scoreAccountButtonActionPerformed(ActionEvent evt) { JOptionPane.showMessageDialog(null,"你当前成绩:"+testscore,"联机测试系统",JOptionPane.INFORMATION_MESSAGE); System.out.println("你当前成绩:"+testscore); } private void reconnectButtonActionPerformed(ActionEvent evt) { String id=Login.id; String ps=Login.ps; String status=Login.status; String serverip=Login.serverip; int serverport=Login.serverport; try { if(socket!=null&&!socket.isClosed()) socket.close(); bufreader.close(); pswriter.close(); socket=new Socket(serverip,serverport); bufreader=new BufferedReader(new InputStreamReader(socket.getInputStream())); pswriter=new PrintWriter(socket.getOutputStream()); String st="LOGIN:"+id+":"+ps+":"+status; st=new String(st.getBytes()); pswriter.println(st); pswriter.flush(); reconnectButton.setEnabled(false); reconnectMenuItem.setEnabled(false); new Thread(){public void run(){ String loginfostring = ""; try { loginfostring = bufreader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block if(socket!=null&&!socket.isClosed()) System.out.println(e.getMessage()); reconnectButton.setEnabled(true); reconnectMenuItem.setEnabled(true); } if(loginfostring==null){ JOptionPane.showMessageDialog(null,"网络不通或者服务器没开启服务","连接失败",JOptionPane.ERROR_MESSAGE); reconnectButton.setEnabled(true); reconnectMenuItem.setEnabled(true); } else if("LOGINSUCC".equals(loginfostring)){ JOptionPane.showMessageDialog(null,"重新连接成功","连接成功",JOptionPane.INFORMATION_MESSAGE); } else if("ALREADYONLINE".equals(loginfostring)){ JOptionPane.showMessageDialog(null,"你已经在线了,不能重复登录","登录失败",JOptionPane.ERROR_MESSAGE); reconnectButton.setEnabled(true); reconnectMenuItem.setEnabled(true); } }}.start(); } catch (Exception e1) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null,"网络连接失败,请检查网络设置以及目标主机服务是否启动","登录失败",JOptionPane.ERROR_MESSAGE); reconnectButton.setEnabled(true); reconnectMenuItem.setEnabled(true); if(socket!=null&&!socket.isClosed()) try { socket.close(); } catch (IOException e) { System.out.println(e.getMessage()); } System.out.println(e1.getMessage()); } System.out.println("id--"+id);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -