📄 user.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class UserAdd extends JDialog implements ActionListener
{
ConnectionSQL conSql;
JButton okButn, canceButn;
JTextField user;
JPasswordField pasw;
UserAdd(JFrame f, String s, boolean b)
{
super(f,s,b);
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
Container con = getContentPane();
//con.setLayout(new FlowLayout());
user = new JTextField(10);
pasw = new JPasswordField(10);
okButn = new JButton("确定");
canceButn = new JButton("取消");
okButn.addActionListener(this);
canceButn.addActionListener(this);
JPanel panel = new JPanel();
panel.add(okButn);
panel.add(canceButn);
Box box1, box2, box3, box;
box1 = Box.createVerticalBox();
box1.add(new JLabel("新用户:"));
box1.add(Box.createVerticalStrut(18));
box1.add(new JLabel("密 码:"));
box2 = Box.createVerticalBox();
box2.add(Box.createVerticalStrut(18));
box2.add(user);
box2.add(Box.createVerticalStrut(8));
box2.add(pasw);
box2.add(Box.createVerticalStrut(18));
box3 = Box.createHorizontalBox();
box3.add(Box.createHorizontalStrut(18));
box3.add(box1);
box3.add(Box.createHorizontalStrut(8));
box3.add(box2);
box3.add(Box.createHorizontalStrut(18));
box = Box.createVerticalBox();
box.add(box3);
box.add(panel);
box.add(Box.createVerticalStrut(8));
con.add(box);
setSize(200, 180);
validate();
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getSource()==okButn)
{
if(user.getText().equals("") || pasw.getText().equals(""))
{
JOptionPane.showMessageDialog(this, "用户名和密码不能为空!", "警告", JOptionPane.WARNING_MESSAGE);
}
else
{
try {
conSql = new ConnectionSQL();
conSql.sql = conSql.con.createStatement();
conSql.sql.executeUpdate("insert into PASSWORDS values('"+user.getText()+"','"+pasw.getText()+"')");
}
catch(SQLException ee) {}
user.setText(null);
pasw.setText(null);
}
}
else if(e.getSource()==canceButn)
{
setVisible(false);
}
}
}
class UserDel extends JDialog implements ActionListener
{
ConnectionSQL conSql;
JButton okButn, canceButn;
JTextField user;
JPasswordField pasw;
UserDel(JFrame f, String s, boolean b)
{
super(f,s,b);
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
Container con = getContentPane();
//con.setLayout(new FlowLayout());
user = new JTextField(10);
pasw = new JPasswordField(10);
okButn = new JButton("确定");
canceButn = new JButton("取消");
okButn.addActionListener(this);
canceButn.addActionListener(this);
JPanel panel = new JPanel();
panel.add(okButn);
panel.add(canceButn);
Box box1, box2, box3, box;
box1 = Box.createVerticalBox();
box1.add(new JLabel("用户:"));
box1.add(Box.createVerticalStrut(18));
box1.add(new JLabel("密 码:"));
box2 = Box.createVerticalBox();
box2.add(Box.createVerticalStrut(18));
box2.add(user);
box2.add(Box.createVerticalStrut(8));
box2.add(pasw);
box2.add(Box.createVerticalStrut(18));
box3 = Box.createHorizontalBox();
box3.add(Box.createHorizontalStrut(18));
box3.add(box1);
box3.add(Box.createHorizontalStrut(8));
box3.add(box2);
box3.add(Box.createHorizontalStrut(18));
box = Box.createVerticalBox();
box.add(box3);
box.add(panel);
box.add(Box.createVerticalStrut(8));
con.add(box);
setSize(200, 180);
validate();
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getSource()==okButn)
{
if(user.getText().equals("") || pasw.getText().equals(""))
{
JOptionPane.showMessageDialog(this, "用户名和密码不能为空!", "警告", JOptionPane.WARNING_MESSAGE);
}
else
{
try {
conSql = new ConnectionSQL();
conSql.sql = conSql.con.createStatement();
conSql.sql.executeUpdate("delete from PASSWORDS where 用户名='"+user.getText()+"' and 密码='"+pasw.getText()+"'");
}
catch(SQLException ee) {}
user.setText(null);
pasw.setText(null);
}
}
else if(e.getSource()==canceButn)
{
setVisible(false);
}
}
}
class UserChange extends JDialog implements ActionListener
{
ConnectionSQL conSql;
JButton okButn, canceButn;
JTextField user, newuser;
JPasswordField pasw, newpasw;
UserChange(JFrame f, String s, boolean b)
{
super(f,s,b);
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
Container con = getContentPane();
//con.setLayout(new FlowLayout());
user = new JTextField(10);
newuser = new JTextField(10);
pasw = new JPasswordField(10);
newpasw = new JPasswordField(10);
okButn = new JButton("确定");
canceButn = new JButton("取消");
okButn.addActionListener(this);
canceButn.addActionListener(this);
JPanel panel = new JPanel();
panel.add(okButn);
panel.add(canceButn);
Box box1, box2, box3, box;
box1 = Box.createVerticalBox();
box1.add(new JLabel("用户名:"));
box1.add(Box.createVerticalStrut(18));
box1.add(new JLabel("旧 密 码:"));
box1.add(Box.createVerticalStrut(18));
box1.add(new JLabel("新用户:"));
box1.add(Box.createVerticalStrut(18));
box1.add(new JLabel("新 密 码:"));
box2 = Box.createVerticalBox();
box2.add(Box.createVerticalStrut(18));
box2.add(user);
box2.add(Box.createVerticalStrut(8));
box2.add(pasw);
box2.add(Box.createVerticalStrut(8));
box2.add(newuser);
box2.add(Box.createVerticalStrut(8));
box2.add(newpasw);
box2.add(Box.createVerticalStrut(18));
box3 = Box.createHorizontalBox();
box3.add(Box.createHorizontalStrut(18));
box3.add(box1);
box3.add(Box.createHorizontalStrut(8));
box3.add(box2);
box3.add(Box.createHorizontalStrut(18));
box = Box.createVerticalBox();
box.add(box3);
box.add(panel);
box.add(Box.createVerticalStrut(8));
con.add(box);
setSize(200, 260);
validate();
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getSource()==okButn)
{
if(user.getText().equals("") || pasw.getText().equals("") ||
newuser.getText().equals("") || newpasw.getText().equals(""))
{
JOptionPane.showMessageDialog(this, "用户名和密码不能为空!", "警告", JOptionPane.WARNING_MESSAGE);
}
else
{
//String select = String.format(format, args)
try {
conSql = new ConnectionSQL();
conSql.sql = conSql.con.createStatement();
conSql.rs = conSql.sql.executeQuery("select * from PASSWORDS where 用户名='"+user.getText()+"' and 密码='"+pasw.getText()+"'");
if(conSql.rs.next())
{
conSql.sql.executeUpdate("delete from PASSWORDS where 用户名='"+user.getText()+"' and 密码='"+pasw.getText()+"'");
conSql.sql.executeUpdate("insert into PASSWORDS values('"+newuser.getText()+"','"+newpasw.getText()+"')");
}
else
JOptionPane.showMessageDialog(this, "用户或密码错误!\n请查正", "警告", JOptionPane.WARNING_MESSAGE);
}
catch(SQLException ee) {}
user.setText(null); pasw.setText(null);
newuser.setText(null); newpasw.setText(null);
}
}
else if(e.getSource()==canceButn)
{
setVisible(false);
}
}
}
public class User {
public static void main(String args[])
{
JFrame f;
f = new JFrame("ss");
f.setLocationRelativeTo(null);
f.setVisible(true);
UserChange U = new UserChange(f, "修改用户", true);
U.setLocationRelativeTo(null);
U.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -