📄 itemmanagejpanel.java
字号:
package com.cnu.cie.olts.client.teacher;import java.awt.Component;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.Format;import java.text.NumberFormat;import java.util.EventObject;import java.util.Vector;import javax.swing.JButton;import javax.swing.WindowConstants;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableColumn;import javax.swing.table.TableColumnModel;import javax.swing.table.TableModel;import javax.swing.DefaultCellEditor;import javax.swing.DefaultListCellRenderer;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTable;import com.cnu.cie.olts.client.ItemInfo;/*** 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 itemManageJPanel extends javax.swing.JPanel { private JTable itemsjTable; private JScrollPane itemtablejScrollPane; private JButton onsubmitjButton; ItemInfo[] iteminfo; String[] itemsubjectarray; String[] itemtypearray; String[] columnNames = { "Delete", "ItemID", "ItemType", "Content", "Answer", "Score","Limittime","Subject", "Difficulty" }; Object[][] data = { { new Boolean(false), new Integer(1), "单选题","content11111111111111111","answer", new Integer(4),new Integer(5),"数学",new Double(0.78)}, { new Boolean(false), new Integer(2), "填空题","content","answer", new Integer(3),new Integer(3),"语文",new Double(0.56)}, }; static DefaultTableModel model=null; /** * 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 itemManageJPanel()); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.pack(); frame.setVisible(true); } */ public itemManageJPanel(String[] itemsubjectarray,String[] itemtypearray,ItemInfo[] iteminfo) { super(); initGUI(); this.iteminfo=iteminfo; this.itemsubjectarray=itemsubjectarray; this.itemtypearray=itemtypearray; for(int i=0;i<columnNames.length;i++) model.addColumn(columnNames[i]); for(int i=0;i<iteminfo.length;i++){ model.addRow(new Object[]{new Boolean(false),new Integer(iteminfo[i].getItemid()), iteminfo[i].getItemtype(),iteminfo[i].getContent(),iteminfo[i].getAnswer(), new Integer(iteminfo[i].getScore()),new Integer(iteminfo[i].getLimittime()), iteminfo[i].getItemsubject(),new Double(iteminfo[i].getDifficulty()) }); } initializeItemType(itemtypearray); initializeItemSubject(itemsubjectarray); } private void initGUI() { try { this.setPreferredSize(new java.awt.Dimension(612, 312)); this.setLayout(null); { itemtablejScrollPane = new JScrollPane(); this.add(itemtablejScrollPane); itemtablejScrollPane.setBounds(7, 12, 593, 250); { //TableModel itemsjTableModel = // new DefaultTableModel( // new String[][] { { "One", "Two" }, { "Three", "Four" } }, // new String[] { "Column 1", "Column 2" }); model=new DefaultTableModel(){ public boolean isCellEditable(int row,int col){ Class cls = getColumnClass(col); String name = getColumnName(col); return (!name.equals("ItemID")); } //public void setValueAt(Object value, int row, int col) { // System.out.println("("+row+","+col+")="+value); // data[row][col] = value; // dataVector.setElementAt(value,row*data[0].length+col); // fireTableCellUpdated(row, col); // } public Class getColumnClass(int col){ Vector v=(Vector)dataVector.elementAt(0); return v.elementAt(col).getClass(); } }; itemsjTable = new JTable(model); itemtablejScrollPane.setViewportView(itemsjTable); itemsjTable.setBounds(394, 86, 218, 149); } } { onsubmitjButton = new JButton(); this.add(onsubmitjButton); onsubmitjButton.setText("\u63d0 \u4ea4"); onsubmitjButton.setBounds(252, 274, 97, 29); onsubmitjButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onsubmitjButtonActionPerformed(evt); } }); } } catch (Exception e) { e.printStackTrace(); } } private void initializeItemType(String[] itemtypearray){ TableColumn itemtypeColumn = itemsjTable.getColumn("ItemType"); JComboBox combo = new JComboBox(); combo.removeAllItems(); if(itemtypearray.length>0) if("ITEMTYPE".equals(itemtypearray[0])) for(int i=1;i<itemtypearray.length;i++) combo.addItem(itemtypearray[i]); itemtypeColumn.setCellEditor(new DefaultCellEditor(combo)); } private void initializeItemSubject(String[] itemsubjectarray){ TableColumn itemtypeColumn = itemsjTable.getColumn("Subject"); JComboBox combo = new JComboBox(); combo.removeAllItems(); if(itemtypearray.length>0) if("SUBJECT".equals(itemsubjectarray[0])) for(int i=1;i<itemsubjectarray.length;i++) combo.addItem(itemsubjectarray[i]); itemtypeColumn.setCellEditor(new DefaultCellEditor(combo)); } private void onsubmitjButtonActionPerformed(ActionEvent evt) { //System.out.println("onsubmitjButton.actionPerformed, event="+evt); //TODO add your code for onsubmitjButton.actionPerformed Vector DataVector=model.getDataVector(); Vector RowVector; String infotoserver="UPDATEITEM:"; String itemtypestr = null,itemcontentstr = null,itemanswerstr = null,itemsubjectstr = null; int itemscore = 0,itemlimittime = 0; float itemdifficulty = 0; for(int i=0;i<DataVector.size();i++){ RowVector=(Vector)DataVector.elementAt(i); int itemid=Integer.parseInt(RowVector.elementAt(1).toString()); if(!(Boolean)RowVector.elementAt(0)){ for(int j=0;j<RowVector.size();j++){ try{ itemscore=Integer.parseInt(RowVector.elementAt(5).toString()); }catch(Exception e){ JOptionPane.showMessageDialog(null,"第"+(i+1)+"行的试题分数应为正整数","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } try{ itemlimittime=Integer.parseInt(RowVector.elementAt(6).toString()); }catch(Exception e){ JOptionPane.showMessageDialog(null,"第"+(i+1)+"行的试题答题限制时间应为正整数","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } try{ itemdifficulty=Float.parseFloat(RowVector.elementAt(8).toString()); if(itemdifficulty>1||itemdifficulty<0){ JOptionPane.showMessageDialog(null,"第"+(i+1)+"行的试题难度系数应为正0到1的小数","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } }catch(Exception e){ JOptionPane.showMessageDialog(null,"第"+(i+1)+"行的试题难度系数应为正0到1的小数","联机测试系统",JOptionPane.ERROR_MESSAGE); return; } itemtypestr=RowVector.elementAt(2).toString(); itemcontentstr=RowVector.elementAt(3).toString(); itemanswerstr=RowVector.elementAt(4).toString(); itemsubjectstr=RowVector.elementAt(7).toString(); } infotoserver=infotoserver+"update item_info set itemtype='"+ itemtypestr+"',content='"+itemcontentstr+"',answer='"+ itemanswerstr+"',score="+itemscore+",limittime="+ itemlimittime+",subject='"+itemsubjectstr+"',difficulty="+ itemdifficulty+" where id="+itemid; } else { infotoserver=infotoserver+"delete from item_info where id="+itemid; } } TeacherFrame.pswriter.println(infotoserver); TeacherFrame.pswriter.flush(); for(int i=0;i<DataVector.size();i++){ RowVector=(Vector)DataVector.elementAt(i); if((Boolean)RowVector.elementAt(0)) model.removeRow(i); } JOptionPane.showMessageDialog(null,"操作成功","联机测试系统",JOptionPane.ERROR_MESSAGE); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -