📄 usermanagedialog.java
字号:
package mysiloer;
import java.awt.*;
import javax.swing.*;
import java.sql.Connection;
import javax.swing.border.Border;
import java.util.Vector;
import java.awt.event.ActionEvent;
import java.sql.Statement;
import java.sql.SQLException;
import java.awt.event.ActionListener;
/**
* <p>Title: MySiloer</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: yskey</p>
* @author 杨涛
* @version 1.0
*/
public class UserManageDialog extends JDialog {
private JPanel panel1 = new JPanel();
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
private SiloPanel parentPanel;
private Connection con;
private Container cp;
private JPanel controlPanel = new JPanel();
private ButtonGroup group = new ButtonGroup();
private JRadioButton insertRadio = new JRadioButton();
private GridLayout gridLayout1 = new GridLayout();
private JRadioButton deleteRadio = new JRadioButton();
private JPanel contentPanel = new JPanel();
private JPanel buttonPanel = new JPanel();
private JButton okButton = new JButton();
private JButton cancelButton = new JButton();
private JLabel jLabel1 = new JLabel();
private FlowLayout flowLayout1 = new FlowLayout();
private Border border1;
private Vector namesVector;
private GridLayout gridLayout2 = new GridLayout();
private Border border2;
private static int INSERT = 1, DELETE = 0;
private int action = 2;
UserPanel frame = null;
// private JTextField[] params = {new JTextField("", 10),
JTextField param1 = new JTextField("", 10);
private JPasswordField param2 = new JPasswordField();
private JPasswordField param3 = new JPasswordField();
JTextField param4 = new JTextField("", 10);
// new JTextField("", 10),
// new JTextField("", 10)
//};
// private JRadioButton updateRadio = new JRadioButton();
public UserManageDialog(UserPanel frame, String title, boolean modal) {
super(new JFrame(), title, modal);
try {
this.frame = frame;
this.con = frame.con;
param1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
okButton_actionPerformed(e);
}});
param2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
okButton_actionPerformed(e);
}});
param3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
okButton_actionPerformed(e);
}});
jbInit();
this.setResizable(false);
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2 - 150);
// setVisible(true);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public UserManageDialog() {
this(null, "", false);
}
private void jbInit() throws Exception {
border1 = BorderFactory.createEmptyBorder(20,20,20,20);
border2 = BorderFactory.createEmptyBorder(5,10,5,10);
panel1.setLayout(borderLayout1);
cp = getContentPane();
insertRadio.setText("添加新用户");
insertRadio.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
insertRadio_actionPerformed(e);
}
});
controlPanel.setLayout(gridLayout1);
// gridLayout1.setColumns(1);
gridLayout1.setHgap(5);
deleteRadio.setText("删除用户");
deleteRadio.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteRadio_actionPerformed(e);
}
});
buttonPanel.setLayout(flowLayout1);
okButton.setText("确 定");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
okButton_actionPerformed(e);
}
});
cancelButton.setText("取 消");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
setVisible(false);
}
});
jLabel1.setText(" ");
controlPanel.setBorder(border1);
gridLayout2.setHgap(2);
gridLayout2.setVgap(5);
contentPanel.setLayout(gridLayout2);
gridLayout2.setColumns(2);
// gridLayout2.setRows(3);
contentPanel.setBorder(border2);
// updateRadio.setText("更新记录");
// updateRadio.addActionListener(new java.awt.event.ActionListener() {
// public void actionPerformed(ActionEvent e) {
// updateRadio_actionPerformed(e);
// }
// });
cp.add(panel1);
panel1.add(controlPanel, BorderLayout.NORTH);
// controlPanel.add(updateRadio, null);
controlPanel.add(insertRadio, null);
controlPanel.add(deleteRadio, null);
panel1.add(contentPanel, BorderLayout.CENTER);
panel1.add(buttonPanel, BorderLayout.SOUTH);
group.add(deleteRadio);
group.add(insertRadio);
// group.add(updateRadio);
buttonPanel.add(okButton, null);
buttonPanel.add(jLabel1, null);
buttonPanel.add(cancelButton, null);
}
private void insertRadio_actionPerformed(ActionEvent e){
this.action = this.INSERT;
contentPanel.removeAll();
this.gridLayout2.setColumns(2);
this.gridLayout2.setRows(4);
this.contentPanel.setLayout(gridLayout2);
JLabel lab = new JLabel("用 户 名:");
JLabel lab1 = new JLabel("密 码:");
JLabel lab2 = new JLabel("确认密码:");
contentPanel.add(lab);
contentPanel.add(param1);
contentPanel.add(lab1);
contentPanel.add(param2);
contentPanel.add(lab2);
contentPanel.add(param3);
// contentPanel.add(lab3);
// contentPanel.add(param4);
validate();
pack();
repaint();
}
private void deleteRadio_actionPerformed(ActionEvent e){
this.action = this.DELETE;
contentPanel.removeAll();
this.gridLayout2.setColumns(2);
this.gridLayout2.setRows(1);
this.contentPanel.setLayout(gridLayout2);
JLabel lab = new JLabel("请输入您要删除的用户的用户名:");
contentPanel.add(lab);
contentPanel.add(param1);
validate();
pack();
repaint();
}
private void okButton_actionPerformed(ActionEvent e){
//删除用户
try {
if (this.action == this.DELETE) {
String sql = "delete from " + Type.USERTABLE + " where userName=" +"'"+
param1.getText()+"'";
// System.out.println(sql);
Statement sta = con.createStatement();
int i = sta.executeUpdate(sql);
if(i != 0){
JOptionPane.showMessageDialog(this, "您已经成功删除用户"+param1.getText()+"!!");
frame.showAll();
param1.setText("");
}
}
else if (this.action == this.INSERT) {
//添加用户的代码
// try{
if(this.action == this.INSERT){
String pwd = new String(param2.getPassword()).trim();
String pwd1 = new String(param3.getPassword()).trim();
String sql = null;
if(pwd.equals(pwd1)){
sql = "insert into " + Type.USERTABLE + " values('" +
param1.getText() + "',"+"'"+pwd+"')";
System.out.println(sql);
}
else{
JOptionPane.showMessageDialog(this, "您输入的密码不匹配,请重新输入!!");
param3.setText("");
param2.setText("");
}
Statement sta = con.createStatement();
int i = sta.executeUpdate(sql);
if(i > 0){
JOptionPane.showMessageDialog(this, "添加用户成功!!!");
frame.showAll();
param1.setText("");
param2.setText("");
param3.setText("");
}
}
}
// catch(Exception exx){
//
// }
}
// }
catch (SQLException ex) {
System.err.println(ex.toString());
}
}
// private void updateRadio_actionPerformed(ActionEvent e){
//
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -