📄 usermanagejpanel.java
字号:
package com.cnu.cie.olts.client.admin;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.util.EventObject;import java.util.Vector;import javax.swing.DefaultCellEditor;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.WindowConstants;import javax.swing.JFrame;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableColumn;import com.cnu.cie.olts.client.ItemInfo;import com.cnu.cie.olts.client.teacher.TeacherFrame;public class userManageJPanel extends javax.swing.JPanel { private JTable usersjTable; private JScrollPane itemtablejScrollPane; private JButton onsubmitjButton; UserInfo[] userinfo; String[] columnNames = {"Delete", "UserID", "Username", "Password", "Status"}; Object[][] data; 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 userManageJPanel()); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.pack(); frame.setVisible(true); } */ public userManageJPanel(UserInfo[] userinfo) { super(); this.userinfo=userinfo; // data=new Object[userinfo.length][columnNames.length]; // for(int i=0;i<userinfo.length;i++) // data[i]=new Object[]{new Boolean(false),new Integer(userinfo[i].getUserid()), // userinfo[i].getUsername(),userinfo[i].getPassword(),userinfo[i].getStatus()}; initGUI(); for(int i=0;i<columnNames.length;i++) model.addColumn(columnNames[i]); for(int i=0;i<userinfo.length;i++) model.addRow(new Object[]{new Boolean(false),new Integer(userinfo[i].getUserid()), userinfo[i].getUsername(),userinfo[i].getPassword(),userinfo[i].getStatus() }); initializeStatus(); } 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(){ // model=new DefaultTableModel(data,columnNames){ public boolean isCellEditable(int row,int col){ Class cls = getColumnClass(col); String name = getColumnName(col); return (!name.equals("UserID")); } // 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(); } }; usersjTable = new JTable(model); itemtablejScrollPane.setViewportView(usersjTable); usersjTable.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 initializeStatus(){ TableColumn itemtypeColumn = usersjTable.getColumn("Status"); JComboBox combo = new JComboBox(); combo.removeAllItems(); combo.addItem("学生"); combo.addItem("教师"); combo.addItem("管理员"); 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="UPDATEUSERINFO:"; String usernamestr = null,passwordstr = null,statusstr = null; int userid; for(int i=0;i<DataVector.size();i++){ RowVector=(Vector)DataVector.elementAt(i); userid=Integer.parseInt(RowVector.elementAt(1).toString()); if(!(Boolean)RowVector.elementAt(0)){ for(int j=0;j<RowVector.size();j++){ usernamestr=RowVector.elementAt(2).toString(); passwordstr=RowVector.elementAt(3).toString(); statusstr=RowVector.elementAt(4).toString(); } infotoserver=infotoserver+"update user_info set username='"+ usernamestr+"',password='"+passwordstr+"',status='"+ statusstr+"' where id="+userid; } else { infotoserver=infotoserver+"delete from user_info where id="+userid; } } AdminFrame.pswriter.println(infotoserver); AdminFrame.pswriter.flush(); String returninfo = null; try { returninfo=AdminFrame.bufreader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); AdminFrame.reconnectButton.setEnabled(true); AdminFrame.reconnectMenuItem.setEnabled(true); return; } if(returninfo==null){ JOptionPane.showMessageDialog(null,"与服务器的连接已经中断,请重新连接","连接中断",JOptionPane.ERROR_MESSAGE); AdminFrame.reconnectButton.setEnabled(true); AdminFrame.reconnectMenuItem.setEnabled(true); return ; } if("UPDATEUSERFAIL".equals(returninfo)){ JOptionPane.showMessageDialog(null,"操作失败,请重试","联机测试系统",JOptionPane.ERROR_MESSAGE); return ; } 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); }}class PriceEditor extends DefaultCellEditor { public PriceEditor(JComboBox combo) { super(combo); } public boolean isCellEditable(EventObject e) { JPanel messagePanel = new JPanel(); JPasswordField pwf = new JPasswordField(10); System.out.println(e); messagePanel.add(new JLabel("Password:")); messagePanel.add(pwf); JOptionPane.showMessageDialog(null, messagePanel, "Password Required", JOptionPane.INFORMATION_MESSAGE); if(pwf.getText().trim().equals("dolby")) { return true; } else { JOptionPane.showMessageDialog(null, "Wrong Password!", "Access Failed", JOptionPane.INFORMATION_MESSAGE); return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -