import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.html.HTMLDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
public class MangeUserFrame extends JFrame {
JTabbedPane tabbedPane = new JTabbedPane(); //选项卡组件
static String[] col = {"用户名","口令","角色"};
static Object[][] row = new Object[15][3];
static JComboBox comboBox1;
//DefaultTableModel dtm;
public MangeUserFrame()
{
setTitle("用户管理界面");
setSize(400,280);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
panel_add();
panel_mod();
panel_del();
setContentPane(tabbedPane);
setVisible(true);
}
private JComponent panel_add ()
{
//新增用户选项
JPanel panel = new JPanel();
tabbedPane.addTab("新增用户",panel);
Box b1 = Box.createVerticalBox(); //盒式布局管理器
Box b2 = Box.createVerticalBox();
JLabel username = new JLabel("用户名");
JLabel password = new JLabel("密码");
JLabel role = new JLabel("角色");
JTextField name = new JTextField(10);
JPasswordField paw = new JPasswordField(10);
JButton button1 = new JButton("添加");
JButton button2 = new JButton("取消");
JComboBox cmb = new JComboBox(); //下拉列表
panel.add(b1);
b1.add(Box.createVerticalStrut(20));
b1.add(username);
b1.add(Box.createVerticalStrut(20));
b1.add(password);
b1.add(Box.createVerticalStrut(20));
b1.add(role);
b1.add(Box.createVerticalStrut(35));
b1.add(button1);
panel.add(b2);
b2.add(Box.createVerticalStrut(20));
b2.add(name);
b2.add(Box.createVerticalStrut(20));
b2.add(paw);
b2.add(Box.createVerticalStrut(20));
b2.add(cmb);
b2.add(Box.createVerticalStrut(20));
b2.add(button2);
cmb.addItem("----请选择----");
cmb.addItem("administrator");
cmb.addItem("operator");
cmb.addItem("browser");
//注册监听事件
//点确认时
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String password = new String(paw.getPassword());
boolean flag = false;
try { //添加用户
JOptionPane.showMessageDialog(null, "确认新增用户吗?单击按钮将添加", "确认添加提示", 0);
flag = DataProcessing.insertUser(name.getText(), password, cmb.getSelectedItem().toString().trim());
} catch (SQLException ex) {
ex.printStackTrace();
}
if (flag) {
Enumeration<User> u1 = null;
int i = 0;
try {
u1 = DataProcessing.getAllUser();
} catch (SQLException ex) {
ex.printStackTrace();
}
while(u1.hasMoreElements()){
User temp = u1.nextElement();
row[i][0] = temp.getName();
row[i][1] = temp.getPassword();
row[i][2] = temp.getRole();
i ++;
}
JOptionPane.showMessageDialog(null, "添加成功!", "提示", 0);
// tabbedPane.remove(1);
// tabbedPane.remove(1);
// panel_mod();
// panel_del();
new MangeUserFrame();
dispose();
//dtm = new DefaultTableModel(row,col);
}
}
});
//点取消时清空文本框
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
name.setText(" ");
paw.setText(null);
}
});
return panel;
}
private JComponent panel_mod()
{
JPanel panel = new JPanel();
tabbedPane.addTab("修改用户",panel);
Box b1 = Box.createVerticalBox();
Box b2 = Box.createVerticalBox();
JLabel username = new JLabel("用户名");
JLabel password = new JLabel("口令");
JLabel role = new JLabel("角色");
JPasswordField pwd = new JPasswordField(10);
JComboBox comboBox2 = new JComboBox();
JButton button1 = new JButton("修改");
JButton button2 = new JButton("取消");
String c1;
//String c2;
Enumeration<User> t=null;
try {
t = DataProcessing.getAllUser();
} catch (SQLException e) {
e.printStackTrace();
}
comboBox1 = new JComboBox();
while(t.hasMoreElements()){
User temp = t.nextElement();
c1 = temp.getName();
// c2 = temp.getRole();
comboBox1.addItem(c1);
}
comboBox2.addItem("operator");
comboBox2.addItem("browser");
comboBox2.addItem("administrator");
panel.add(b1);
b1.add(Box.createVerticalStrut(20));
b1.add(username);
b1.add(Box.createVerticalStrut(25));
b1.add(password);
b1.add(Box.createVerticalStrut(20));
b1.add(role);
b1.add(Box.createVerticalStrut(40));
b1.add(button1);
panel.add(b2);
b2.add(Box.createVerticalStrut(20));
b2.add(comboBox1);
b2.add(Box.createVerticalStrut(20));
b2.add(pwd);
b2.add(Box.createVerticalStrut(20));
b2.add(comboBox2);
b2.add(Box.createVerticalStrut(20));
b2.add(button2);
//点确认按钮时
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
User user=null;
boolean flag = false;
String s1 = comboBox1.getSelectedItem().toString().trim();
String s2 = comboBox2.getSelectedItem().toString().trim();
String paw = new String(pwd.getPassword());
try {
user=DataProcessing.searchUser(s1); //
} catch (SQLException ex) {
ex.printStackTrace();
}
if(user!=null) //
{
try {
JOptionPane.showMessageDialog(null, "确认要修改用户信息吗?单击按钮将修改", "修改信息提示", 0);
flag = DataProcessing.updateUser(s1, paw, s2);
} catch (SQLException ex) {
ex.printStackTrace();
}
if (flag) {
Enumeration<User> u = null;
int j = 0;
try {
u = DataProcessing.getAllUser();
} catch (SQLException ex) {
}
while (u.hasMoreElements()) {
User temp = u.nextElement();
row[j][0] = temp.getName();
row[j][1] = temp.getPassword();
row[j][2] = temp.getRole();