📄 tjstu.java~71~
字号:
package testsystem;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class tjStu extends JFrame {
private ImageIcon im1 = new ImageIcon("img\\forum.gif");
public tjStu() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
biaoti.setFont(new java.awt.Font("隶书", Font.BOLD, 18));
getContentPane().setLayout(null);
jt1.setBounds(new Rectangle(123, 49, 201, 31));
this.getContentPane().setBackground(new Color(226, 154, 226));
this.setResizable(false);
setTitle("添加用户");
jButton1.setBounds(new Rectangle(149, 202, 108, 39));
jButton1.setIcon(im1);
jButton1.setText("添加");
jButton1.addActionListener(new tjStu_jButton1_actionAdapter(this));
jr2.setText("学生");
jr2.setBounds(new Rectangle(268, 150, 59, 35));
jr1.setText("教师");
jr1.setBounds(new Rectangle(122, 151, 76, 32));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("用户名:");
jLabel1.setBounds(new Rectangle(27, 44, 77, 35));
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("密 码:");
jLabel2.setBounds(new Rectangle(27, 95, 73, 34));
jLabel3.setText("身 份:");
jLabel3.setBounds(new Rectangle(41, 146, 59, 40));
biaoti.setHorizontalAlignment(SwingConstants.CENTER);
biaoti.setText("添 加 用 户");
biaoti.setBounds(new Rectangle(116, 9, 181, 33));
jp1.setBounds(new Rectangle(123, 95, 200, 30));
this.getContentPane().add(jt1);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jr1);
this.getContentPane().add(jr2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jButton1);
this.getContentPane().add(biaoti);
this.getContentPane().add(jp1);
buttonGroup1.add(jr1);
buttonGroup1.add(jr2);
}
public static void main(String[] args) {
tjStu tjstu = new tjStu();
}
JTextField jt1 = new JTextField();
JRadioButton jr1 = new JRadioButton();
JRadioButton jr2 = new JRadioButton();
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel biaoti = new JLabel();
JPasswordField jp1 = new JPasswordField();
ButtonGroup buttonGroup1 = new ButtonGroup();
public void jButton1_actionPerformed(ActionEvent e) {
String Name = jt1.getText().trim();
String Psw = new String(jp1.getPassword()).trim();
Connection con =null;
Statement s1 = null;
Statement s2 = null;
Statement s3 = null;
ResultSet rs1 = null;
boolean teacher = jr1.isSelected();
boolean student = jr2.isSelected();
if((Name.length()!=0&&Psw.length()!=0)&&(teacher||student))
{
try
{
con=DBcon.link();
s1=con.createStatement();
rs1=s1.executeQuery("select * from Login where Name='"+Name+"'");
if(!rs1.next())
{
if(teacher)
{
try
{
con=DBcon.link();
s2=con.createStatement();
s2.executeUpdate("insert into Login values('"+Name+"','"+Psw+"','教师')");
JOptionPane.showMessageDialog(null,"教师用户添加成功!");
int p=JOptionPane.showConfirmDialog(null,"您是否还要添加?","提示",JOptionPane.YES_NO_OPTION);
if(p!=0)
{
this.setVisible(false);
}
else
{
jt1.setText("");
jp1.setText("");
jt1.requestFocus();
return;
}
}
catch(SQLException ce)
{
ce.getMessage();
}
}
if(student)
{
try
{
con=DBcon.link();
s3=con.createStatement();
s3.executeUpdate("insert into Login values('"+Name+"','"+Psw+"','学生')");
JOptionPane.showMessageDialog(null,"学生用户添加成功!");
int p=JOptionPane.showConfirmDialog(null,"您是否还要添加?","提示",JOptionPane.YES_NO_OPTION);
if(p!=0)
{
this.setVisible(false);
}
else
{
jt1.setText("");
jp1.setText("");
jt1.requestFocus();
return;
}
}
catch(SQLException ct)
{
ct.getMessage();
}
}
}
else
{
JOptionPane.showMessageDialog(null,"用户已经存在,请重新添加!","提示",JOptionPane.ERROR_MESSAGE);
jt1.setText("");
jp1.setText("");
jt1.requestFocus();
}
}
catch(Exception co)
{
co.getMessage();
}
}
else
{
JOptionPane.showMessageDialog(null,"信息未填完全,无法添加!");
}
}
}
class tjStu_jButton1_actionAdapter implements ActionListener {
private tjStu adaptee;
tjStu_jButton1_actionAdapter(tjStu adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -