import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.sql.*;
class ZhuCe extends Frame implements ActionListener// 用户界面类类
{
Box baseBox,boxV1,boxV2;
TextField t1,t2,t3,t4,t5;
Button add,del,update,chaxun;
int chang[]={1,2,3,4};//标记
Connection con = null;//
Statement sql = null;
ResultSet rs = null;
String s_id,s_name,s_sex,s_class,s_room;
ZhuCe(String s)
{
super(s);
t1=new TextField(12);t1.setBackground(Color.orange);
t2=new TextField(12);t2.setBackground(Color.orange);
t3=new TextField(12);t3.setBackground(Color.orange);
t4=new TextField(12);t4.setBackground(Color.orange);
t5=new TextField(12);t5.setBackground(Color.orange);
t1.setEditable(false);
t2.setEditable(false);
t3.setEditable(false);
t4.setEditable(false);
t5.setEditable(false);
add=new Button("添加用户"); add.addActionListener(this);
del=new Button("删除用户");del.addActionListener(this);
update=new Button("更新用户");update.addActionListener(this);
chaxun=new Button("查询用户");chaxun.addActionListener(this);
add.setBackground(Color.magenta);
del.setBackground(Color.magenta);
update.setBackground(Color.magenta);
chaxun.setBackground(Color.magenta);
boxV1=Box.createVerticalBox();
boxV1.add(new Label(" 学号:"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label(" 姓名:"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label(" 性别:"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label(" 班级:"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label(" 宿舍:"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(update);
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(chaxun);
boxV2=Box.createVerticalBox();
boxV2.add(t1);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(t2);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(t3);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(t4);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(t5);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(add);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(del);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
add(baseBox);
setLayout(new FlowLayout());
//add(baseBox,BorderLayout.NORTH);
//add(add,BorderLayout.SOUTH);
/*add(del,BorderLayout.SOUTH);
add(update,BorderLayout.SOUTH);
add(chaxun,BorderLayout.SOUTH);*/
setBounds(120,125,300,350);
setVisible(true);
addWindowListener(new WindowAdapter()//关闭窗口
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});
setBackground(Color.cyan);
// 将窗口置于屏幕中央
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.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);
setResizable(false);
// chang[0]=1;chang[1]=2;chang[2]=3;chang[3]=4;
////标记
}
public void actionPerformed(ActionEvent e)
{
s_id=t1.getText().toString();
s_name=t2.getText().toString();
s_sex=t3.getText().toString();
s_class=t4.getText().toString();
s_room=t5.getText().toString();
if(e.getSource()==add)// 添加用户
{
if( chang[0]==1)
{ chang[0]=0;add.setLabel("确定");
t1.setEditable(true);
t2.setEditable(true);
t3.setEditable(true);
t4.setEditable(true);
t5.setEditable(true);
t1.setBackground(Color.white);
t2.setBackground(Color.white);
t3.setBackground(Color.white);
t4.setBackground(Color.white);
t5.setBackground(Color.white);
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
t4.setText(" ");
t5.setText(" ");
}
else if(chang[0]==0)
{ chang[0]=1;
add.setLabel("添加用户");
con = AccessConnection.getCon();
String addUser = new String("insert into users values('" + s_id
+ "','" + s_name + "','" + s_sex + "','" + s_class + "','" + s_room + "')");
try {
sql = con.createStatement();
sql.executeUpdate(addUser);
JOptionPane.showMessageDialog(null,"用户"+s_name+"已经被添加!");
con.close();
}
catch (SQLException e1) {
System.out.println("添加用户" + e1);
}
}
}
else if(e.getSource()==del)//删除用户(学号和姓名)
{
if( chang[1]==2)
{ chang[1]=0;del.setLabel("确定");
t1.setEditable(true);
t2.setEditable(true);
t3.setEditable(false);
t4.setEditable(false);
t5.setEditable(false);
t1.setBackground(Color.white);
t2.setBackground(Color.white);
t3.setBackground(Color.orange);
t4.setBackground(Color.orange);
t5.setBackground(Color.orange);
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
t4.setText(" ");
t5.setText(" ");
}
else if(chang[1]==0)
{ chang[1]=2;del.setLabel("删除用户");
con = AccessConnection.getCon();
String delUser=new String("delete from users where s_id = '" + s_id + "' or s_name = '" + s_name + "' ");
try {
sql = con.createStatement();
sql.executeUpdate(delUser);
JOptionPane.showMessageDialog(null,"用户"+s_id+"已经被删除!(关键字:学号or姓名)");
con.close();}
catch (SQLException e1)
{System.out.println("删除用户" + e1); }
}
}
else if(e.getSource()==update)//更新用户(名字,性别,班级,宿舍)
{ if( chang[2]==3)
{ chang[2]=0;update.setLabel("确定");
t1.setEditable(true);
t2.setEditable(true);
t3.setEditable(true);
t4.setEditable(true);
t5.setEditable(true);
t1.setBackground(Color.red);
t2.setBackground(Color.white);
t3.setBackground(Color.white);
t4.setBackground(Color.white);
t5.setBackground(Color.white);
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
t4.setText(" ");
t5.setText(" ");
}
else if(chang[2]==0)
{ chang[2]=3;update.setLabel("更新用户");
con = AccessConnection.getCon();
String updateUser=new String("update users set s_name = '" + s_name + "', s_sex = '" + s_sex +
"' , s_class = '" + s_class + "', s_room = '" + s_room + "'" + "where s_id = '" + s_id + "'");
try {
sql = con.createStatement();
sql.executeUpdate(updateUser);
JOptionPane.showMessageDialog(null,"更新用户: "+s_id+"(关键字:学号)");
con.close();
}
catch (SQLException e3)
{System.out.println("更新用户(名字,性别,班级,宿舍)" + e3); }
}
}
else if(e.getSource()==chaxun)//查询用户 (关
- 1
- 2
前往页