📄 studentframe.java
字号:
package com.studentFrame;
/*学生信息管理窗口*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPasswordField;
import java.util.*;
import java.util.StringTokenizer;
import javax.swing.border.*;
import javax.swing.JPanel;
import javax.swing.*;
import java.sql.*;
public class StudentFrame extends JFrame implements ActionListener
{
JButton btinfomodify,btpasmodify,btmodify,btclose;
Label lab[];
String s[] = {"学号","姓名","性别","籍贯","住址","系别","班级","生日"};
TextField tf[]; //用来显示个人信息的文本域
JPanel p,p1,p2;
StudentInfo temp;
Dialog pasdg; //修改密码的对话框
Label l1,l2;
JPasswordField jp1,jp2;
StudentFrame(StudentInfo si)
{
/*界面布局*/
Container c = getContentPane();
temp = si;
p1 = new JPanel();
p2 = new JPanel();
p1.setBorder(new TitledBorder("个人信息"));
p1.setLayout(new GridLayout(8,2));
btinfomodify = new JButton("修改个人信息");
btpasmodify = new JButton("修改密码");
btmodify = new JButton("修改");
tf = new TextField[8];
lab = new Label[8];
for(int i = 0;i<8;i++)
{
lab[i] = new Label();
tf[i] = new TextField();
tf[i].setText("");
lab[i].setText(s[i]);
p1.add(lab[i]);
p1.add(tf[i]);
tf[i].addActionListener(this);
}
tf[0].setEditable(false);
p2.add(btinfomodify);
p2.add(btpasmodify);
c.add(p1,BorderLayout.CENTER);
c.add(p2,BorderLayout.SOUTH);
displayInfo(); //显示登陆用户的基本信息
pasdg = new Dialog(this,"密码修改",true);
p = new JPanel(new GridLayout(3,2));
l1 = new Label("请输入新密码");
l2 = new Label("请再次输入新密码");
jp1 = new JPasswordField(10);
jp2 = new JPasswordField(10);
btclose = new JButton("关闭");
p.add(l1);
p.add(jp1);
p.add(l2);
p.add(jp2);
p.add(btmodify);
p.add(btclose);
pasdg.add("Center",p);
pasdg.setSize(240,140);
btmodify.addActionListener(this);
btpasmodify.addActionListener(this);
btinfomodify.addActionListener(this);
btclose.addActionListener(this);
setSize(300,300);
setLocation(260,260);
setVisible(true);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void displayInfo() //显示个人基本信息
{
StringTokenizer st;
int j = 0;
st = new StringTokenizer(temp.toString());
while (st.hasMoreTokens())
tf[j++].setText(st.nextToken());
}
public void actionPerformed(ActionEvent e1)
{
/*连接数据库*/
String url="jdbc:odbc:javadbc";
String username="";
String password="";
Connection con =null;
Statement stmt = null;
String sql; //查询语句
try{
/*连接数据库*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement();
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
catch(Exception e){}
if(e1.getSource() == btinfomodify)
{
/*修改个人信息*/
int result = 0;
sql = "update student set name ='"+tf[1].getText()+"',sex ='"+tf[2].getText()+
"',nativeplace ='"+tf[3].getText()+"',address ='"+tf[4].getText()+
"',dept ='"+tf[5].getText()+"',grade ='"+tf[6].getText()+
"',birth ='"+tf[7].getText().trim()+ "'where id ='"+tf[0].getText()+"'";
try { result=stmt.executeUpdate(sql);}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
/*关闭数据库*/
finally
{
try
{
if(stmt!=null)
stmt.close();
con.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
if(result!=0)
JOptionPane.showMessageDialog(this, "信息修改成功");
else JOptionPane.showMessageDialog(this, "信息修改失败");
}
else if(e1.getSource() == btpasmodify)
{
pasdg.setLocation(200,260);
pasdg.show();
}
else if(e1.getSource() == btclose)
pasdg.dispose();
else {
/*修改密码*/
if(String.valueOf(jp1.getPassword()).equals(String.valueOf(jp2.getPassword())))
{
int result =0;
sql = "update student set password ='"+String.valueOf(jp1.getPassword())+
"'where id ='"+tf[0].getText()+"'";
try { result = stmt.executeUpdate(sql);}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
finally
{
try
{
if(stmt!=null)
stmt.close();
con.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
if(result!=0)
{
JOptionPane.showMessageDialog(this, "密码修改成功");
pasdg.dispose();
}
}
else
JOptionPane.showMessageDialog(this, "两次密码不一致请重新输入","", JOptionPane.ERROR_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -