📄 addnewiteminfojpanel.java
字号:
package com.cnu.cie.olts.client.teacher;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import javax.swing.ComboBoxModel;import javax.swing.DefaultComboBoxModel;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.WindowConstants;import javax.swing.JFrame;import com.cnu.cie.olts.client.Login;/*** This code was edited or generated using CloudGarden's Jigloo* SWT/Swing GUI Builder, which is free for non-commercial* use. If Jigloo is being used commercially (ie, by a corporation,* company or business for any purpose whatever) then you* should purchase a license for each developer using Jigloo.* Please visit www.cloudgarden.com for details.* Use of Jigloo implies acceptance of these licensing terms.* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.*/public class AddNewItemInfoJPanel extends javax.swing.JPanel { private JLabel itemtitleLabel; private JTextArea itemcontentTextArea; private JLabel itemanswerLabel; private JButton fillhelpjButton; private JComboBox itemtypejComboBox; private JLabel itemtypejLabel; private JTextField itemscorejTextField; private JButton submitjButton; private JTextField itemdifficultyjTextField; private JLabel itemdifficultyjLabel; private JComboBox itemsubjectjComboBox; private JLabel itemsubjectjLabel; private JTextField itemlimittimejTextField; private JLabel itemlimittimejLabel; private JLabel itemscorejLabel; private JTextField itemanswerTextField; String[] itemtypearray; String[] itemsbujectarray; /** * Auto-generated main method to display this * JPanel inside a new JFrame. *//* public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new AddNewItemInfoJPanel()); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.pack(); frame.setVisible(true); } */ public AddNewItemInfoJPanel(String[] itemsubjectarray,String[] itemtypearray) { super(); initGUI(); this.itemtypearray=itemtypearray; this.itemsbujectarray=itemsubjectarray; itemtypejComboBox.removeAllItems(); if(itemtypearray.length>0) if("ITEMTYPE".equals(itemtypearray[0])) for(int i=1;i<itemtypearray.length;i++) itemtypejComboBox.addItem(itemtypearray[i]); itemsubjectjComboBox.removeAllItems(); if(itemtypearray.length>0) if("SUBJECT".equals(itemsubjectarray[0])) for(int i=1;i<itemsubjectarray.length;i++) itemsubjectjComboBox.addItem(itemsubjectarray[i]); } private void initGUI() { try { this.setPreferredSize(new java.awt.Dimension(509, 371)); this.setLayout(null); { itemtitleLabel = new JLabel(); this.add(itemtitleLabel); itemtitleLabel.setText("\u8bd5\u9898\u5185\u5bb9"); itemtitleLabel.setBounds(69, 90, 57, 24); } { itemcontentTextArea = new JTextArea(); this.add(itemcontentTextArea); itemcontentTextArea.setBounds(144, 80, 332, 54); } { itemanswerLabel = new JLabel(); this.add(itemanswerLabel); itemanswerLabel.setText("\u8bd5\u9898\u53c2\u8003\u7b54\u6848"); itemanswerLabel.setBounds(44, 169, 88, 29); } { itemanswerTextField = new JTextField(); this.add(itemanswerTextField); itemanswerTextField.setBounds(144, 169, 332, 29); } { itemscorejLabel = new JLabel(); this.add(itemscorejLabel); itemscorejLabel.setText("\u8bd5\u9898\u5206\u503c"); itemscorejLabel.setBounds(66, 222, 71, 23); } { itemscorejTextField = new JTextField(); this.add(itemscorejTextField); itemscorejTextField.setBounds(144, 223, 63, 21); } { itemlimittimejLabel = new JLabel(); this.add(itemlimittimejLabel); itemlimittimejLabel.setText("\u7b54\u9898\u65f6\u95f4(\u5206\u949f)"); itemlimittimejLabel.setBounds(282, 223, 95, 21); } { itemlimittimejTextField = new JTextField(); this.add(itemlimittimejTextField); itemlimittimejTextField.setBounds(389, 223, 87, 21); } { itemsubjectjLabel = new JLabel(); this.add(itemsubjectjLabel); itemsubjectjLabel.setText("\u6240\u5c5e\u79d1\u76ee"); itemsubjectjLabel.setBounds(66, 271, 66, 21); } { //ComboBoxModel itemsubjectjComboBoxModel = // new DefaultComboBoxModel( // new String[] { "Item One", "Item Two" }); itemsubjectjComboBox = new JComboBox(); this.add(itemsubjectjComboBox); //itemsubjectjComboBox.setModel(itemsubjectjComboBoxModel); itemsubjectjComboBox.setBounds(144, 267, 94, 25); } { itemdifficultyjLabel = new JLabel(); this.add(itemdifficultyjLabel); itemdifficultyjLabel.setText("\u96be\u5ea6\u7cfb\u6570"); itemdifficultyjLabel.setBounds(292, 269, 85, 20); } { itemdifficultyjTextField = new JTextField(); this.add(itemdifficultyjTextField); itemdifficultyjTextField.setBounds(389, 269, 87, 21); } { submitjButton = new JButton(); this.add(submitjButton); submitjButton.setText("\u63d0\u4ea4"); submitjButton.setBounds(202, 326, 73, 25); submitjButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { submitjButtonActionPerformed(evt); } }); } { itemtypejLabel = new JLabel(); this.add(itemtypejLabel); itemtypejLabel.setText("\u8bd5\u9898\u7c7b\u578b"); itemtypejLabel.setBounds(62, 28, 64, 21); } { //ComboBoxModel itemtypejComboBoxModel = // new DefaultComboBoxModel( // new String[] { "Item One", "Item Two" }); itemtypejComboBox = new JComboBox(); this.add(itemtypejComboBox); //itemtypejComboBox.setModel(itemtypejComboBoxModel); itemtypejComboBox.setBounds(144, 26, 126, 25); } { fillhelpjButton = new JButton(); this.add(fillhelpjButton); fillhelpjButton.setText("\u586b\u5199\u8bf4\u660e"); fillhelpjButton.setBounds(282, 28, 95, 21); fillhelpjButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { fillhelpjButtonActionPerformed(evt); } }); } } catch (Exception e) { e.printStackTrace(); } } //新试题封装发送到服务器 private void submitjButtonActionPerformed(ActionEvent evt) { //System.out.println("submitjButton.actionPerformed, event="+evt); //TODO add your code for submitjButton.actionPerformed String itemtypestr=(String)itemtypejComboBox.getSelectedItem(); String itemsubjectstr=(String)itemsubjectjComboBox.getSelectedItem(); String itemcontentstr=itemcontentTextArea.getText().trim(); String itemanswerstr=itemanswerTextField.getText().trim(); String scorestr,limittimestr,difficultystr; int score,limittime; float difficulty; if(itemcontentstr==""){ JOptionPane.showMessageDialog(null,"试题题目不能为空","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } if(itemanswerstr==""){ JOptionPane.showMessageDialog(null,"试题参考答案不能为空","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } try { scorestr=itemscorejTextField.getText(); if(scorestr==""){ JOptionPane.showMessageDialog(null,"试题分值不能为空","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } score=Integer.parseInt(scorestr); if(score<1){ JOptionPane.showMessageDialog(null,"分值为正整数","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } } catch(Exception ex) { // only allow integer values Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null,"分值为正整数","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } try { limittimestr=itemlimittimejTextField.getText(); if(limittimestr==""){ JOptionPane.showMessageDialog(null,"试题答题时限不能为空","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } limittime=Integer.parseInt(limittimestr); if(limittime<1){ JOptionPane.showMessageDialog(null,"答题时限为正整数","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } } catch(Exception ex) { // only allow integer values Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null,"分值为正整数","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } try { difficultystr=itemdifficultyjTextField.getText(); if(difficultystr==""){ JOptionPane.showMessageDialog(null,"试题难度系数不能为空","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } difficulty=Float.parseFloat(difficultystr); if(difficulty>1||difficulty<0){ JOptionPane.showMessageDialog(null,"难度系数为0-1的小数","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } } catch(Exception ex) { // only allow integer values Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null,"难度系数为0-1的小数","错误",JOptionPane.ERROR_MESSAGE); return; } String info="ADDNEWITEM:insert into item_info(itemtype,content,answer,score,limittime,subject,difficulty,author) "+ "values('"+itemtypestr+"','"+itemcontentstr+"','"+itemanswerstr+"',"+score+","+limittime+",'"+itemsubjectstr+"',"+difficulty+",'"+Login.id+"')"; info=new String(info.getBytes()); TeacherFrame.pswriter.println(info); TeacherFrame.pswriter.flush(); JOptionPane.showMessageDialog(null,"试题添加成功","联机测试系统",JOptionPane.ERROR_MESSAGE); submitjButton.setEnabled(false); System.out.println("new iteminfo---"+info); } //添加新试题帮助 private void fillhelpjButtonActionPerformed(ActionEvent evt) { //System.out.println("fillhelpjButton.actionPerformed, event="+evt); //TODO add your code for fillhelpjButton.actionPerformed String youCmd="explorer anddnewitemhelp.html"; try { String osName = System.getProperty("os.name"); String[] cmd = new String[3]; if(("Windows 2000").equalsIgnoreCase(osName) || "Windows XP".equalsIgnoreCase(osName)){ cmd[0] = "cmd.exe" ; cmd[1] = "/C" ; cmd[2] = youCmd; }else if( osName.equals( "Windows 95" ) ){ cmd[0] = "command.com" ; cmd[1] = "/C" ; cmd[2] = youCmd; }// System.out.println("cmd:\r\n" + cmd[0] + " " + cmd[1] + " " + cmd[2]); Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd); InputStreamPipe errorGobbler = new InputStreamPipe(proc.getErrorStream(), "ERROR"); InputStreamPipe outputGobbler = new InputStreamPipe(proc.getInputStream(), "OUTPUT"); errorGobbler.start(); outputGobbler.start(); int exitVal = proc.waitFor();//等待事情干完,返回值0表示正确返回 if(exitVal != 0){ throw new Exception(errorGobbler.getOutputMsg() + "(异常退出值exitVal=" + exitVal + ")"); }else{ outputGobbler.getOutputMsg(); } } catch (Exception e){ e.printStackTrace(); } }}/*** 输出流读取类* @author Daley**/class InputStreamPipe extends Thread {InputStream inputStream;String msgType;StringBuffer bufMsg = new StringBuffer();InputStreamPipe(InputStream inputStream, String msgType){ this.inputStream = inputStream; this.msgType = msgType;}public String getOutputMsg(){ return bufMsg.toString();}public void run(){ try { BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); String line=null; //如果命令行不断返回消息则会不断输出 while ((line = br.readLine()) != null){ if(line.trim().length() > 0){ bufMsg.append(line + "\r\n"); System.out.println(line); } } } catch (IOException ioe){ ioe.printStackTrace(); }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -