Eclipseを使用して学生管理システムを設計する
1. eclipseをダウンロードしてインストールし、Javaプロジェクトを作成します(ファイル>>新規>> Javaプロジェクト>>プロジェクト名:StudentManager)
2. srcにビューパッケージを作成して、ビュープロジェクトを保存します(srcの右クリック>>新規>>パッケージ>>名前:ビュー)
3.ビジュアルデザイナーとレイアウトツールを使用して、ログインページデザイン用のシンプルなWindowsフォームを作成し、[表示] >> [新規] >> [その他] >> [WindowBuilder] >> [Swingデザイン] >> [JFrame]、[次へ]、[名前:loginframe]を右クリックします。
4.ログインページを視覚的にデザインします。
1)デザインをクリックして、ビジュアル編集ページに入ります。
(礼儀とホープドイツ語 ブログの日食漢字表示の解決策について )
2)クラスjavax.swing.JFrame:Student ManagementSystemのタイトルにインターフェース名を入力します。
3)contentPane >> Layout >>でGroupLaoutを選択して、自由にレイアウトできるようにします
4)タイトルが必要です。コンポーネントでJLableを選択してテキストボックスを追加し、表示されたテキストをテキスト列に入力します。学生管理システムの繰り返し
5)上記の手順を繰り返します。また、3つのテキストボックスを作成し、名前をlable lable_1 lable_2に変更する必要があります。内容は次のとおりです。ユーザー名:パスワード:ユーザータイプ:この時点では、長さが問題であるため、問題が見つかります。テキストの割合が異なるため、整列しておらず、見苦しいため、対処する必要があります。まず、テキストコンテンツに直接スペースを追加する方法を考えますが、このスペースを追加する方法は非常に不安定で、詳細を揃えるのは簡単ではありません。ソースインターフェイスに戻り、16進数を使用して中国語の文字を入力し、スペースを追加することを検討します。これは比較的簡単ですが、面倒です。3番目の方法を使用します。ここでは、右揃えです。記号のテキストがラベル内で右揃えになっている場合でも、JLabel a = new JLabel( "Username"、JLabel.RIGHT)を入力し、適切なスペースで埋めます。GBKでは、漢字は2バイトを占め、スペースは1バイトを占めます。UTF-8では、漢字は3バイトを占め、スペースは1バイトを占めます。
6)2つのテキスト入力ボックスを追加します
コンポーネントでJTextFieldを選択し、適切なサイズを調整し、2つのテキスト入力ボックスを作成して、名前を次のように変更します。usernametextFieldpasswordtextField
7)コンボボックスを追加します
コンポーネントでJComboxを選択します(コンボボックス:編集可能なテキストボックスとドロップダウンリストを表示します。ユーザーはドロップダウンリストから1つ以上の値を選択できます。ユーザーはリストの一番上にテキストを直接入力できます。 、または1つまたは複数の既製の値を選択します。)、適切なサイズを調整し、名前を次のように変更します。usertypecomboBox(名前の変更の役割を説明するために、これは後でソースインターフェイスでのデバッグを容易にするためのものです。モジュールのコードその部分では、ユーザー名入力ボックスをパスワード入力ボックスとして使用するという同様のエラーを防ぐために、モデルにシステム管理者、教師、学生、および学生を入力します。3つの選択肢があります。注意してください。後で列挙クラスを作成します。これらの3つのオプションの代わりに、この時点での入力は主にソースインターフェイスで対応するコードを生成することです。
8)2つのボタンを追加します
コンポーネントでJButtonを選択し、名前を次のように変更します。loginbuttonresetbutton、テキストにloginと入力して、リセットします。
このような単純なログインページが完成しました。
5.メインページをデザインする
このプロセスは、基本的にログインページを設計するプロセスと似ています。
1)新しいメインフレームを作成し、デザインページに入ります
2)フレームサイズを調整してフィットさせます
3)メニューバーを追加します
メニューでJMenuBarを選択します
4)メニューバーに2つのメニューを追加します
2つのJMenuをJMenuBarに追加し、名前を次のように変更します:menu menu_1、システム設定とクラス管理をそれぞれテキストで入力します
5)メニュー項目を追加する
JMenuItemをJMenuに追加します。今回は、システム設定に2つのオプションを追加する必要があります。パスワードを変更してシステムを終了します。クラス管理にオプションを追加します。クラスを追加し、次に名前を変更します。menuItemmenuItem_1 menuItem_2、次に追加します。テキスト内のテキストの説明
6)仮想インターフェイスを追加します
コンテナ にJDesktopPane(マルチドキュメントインターフェイスまたは仮想デスクトップの作成に使用されるコンテナ)を追加します。区別しやすくするために、背景色を追加します
6.パスワード変更ページをデザインする
1)新しいeditpasswordframeを作成し、デザインページに入ります
新規>>その他>> JInternalFrame
2)フレームサイズを調整してフィットさせます
3)次の5つのテキストボックスを追加します。現在のユーザー:元のパスワード:新しいパスワード:パスワードの確認:空白のテキストボックス(現在のユーザーの後ろ:)の名前は次のように変更されます:label_3 label label_1 label_2 currentUserLabel
4)3つのテキスト入力ボックスを追加し、名前を次のように変更します。oldpwdtextFieldnewpwdtextFieldconfirmpwdFieldボタン
5)2つのボタンを追加し、名前を次のように変更します。submitbuttonresetbutton、それぞれテキストResetにconfirmと入力します
これまでのところ、3つの視覚化ウィンドウの設計は基本的に完了しています
7.ランディングページウィンドウを画面の中央に配置します
明らかに、最初に実行する必要があるのは毎回のログインページです。実行すると、[実行]をクリックした後、画面の左上隅にポップアップウィンドウが表示されます。通常はドラッグして視覚的に快適な画面の中央。、デフォルトで画面の中央に表示するにはどうすればよいですか。
loginframe.javaのpublicloginframe(){(setContentPane(contentPane); decentralized)にコード行を追加するだけです。
setLocationRelativeTo(null);//窗口将置于屏幕的中央
この機能を実現できます。
(Public void setLocationRelativeTo(Component c)指定されたコンポーネントに関連するウィンドウを設定します。コンポーネントが現在表示されていないか、cがnullの場合、ウィンドウは画面の中央に配置されます。GraphicsEnvironment.getCenterPointを使用して中点Phiを決定できます。 羊ZheDe Wolf ブログ)
8.ログインページは完全に機能しています
ログインボタンとリセットボタンの実装
JButton loginbutton = new JButton("登录");
loginbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
login();//登录按钮实现的方法,具体实现在下面
}
});
JButton resetbutton = new JButton("重置");
resetbutton.addActionListener(new ActionListener() {//重置按钮的实现
public void actionPerformed(ActionEvent arg0) {
usernametextField.setText("");
passwordtextField.setText("");
usertypecomboBox.setSelectedIndex(0);
}
});
......
protected void login() { //我们把该函数放在整体代码最后来实现整体代码的美观
String name=usernametextField.getText().toString();
String password=passwordtextField.getText().toString();
usertype selectItem=(usertype) usertypecomboBox.getSelectedItem();
if(stringutil.isEmpty(name)){
JOptionPane.showMessageDialog(this, "用户名不能为空"); //弹出提示性对话框
return;
}
if(stringutil.isEmpty(password)){
JOptionPane.showMessageDialog(this, "密码不能为空");
return;
}
Admin admin=null;
if("系统管理员".equals(selectItem.getName())){
Admindao admindao=new Admindao();
Admin admin2=new Admin();
admin2.setName(name);
admin2.setPassword(password);
admin=admindao.login(admin2);
if(admin==null){
JOptionPane.showMessageDialog(this, "用户名或者密码错误");
return;
}
this.dispose();//当前登录界面关闭
new mainframe(selectItem,admin).setVisible(true);//主页面显示
}
現時点では小さな問題がたくさんあります。慌てる必要はありません。まず、問題を1つずつ解決しましょう。
usertype selectItem =(usertype)usertypecomboBox .getSelectedItem();
これは、public loginframe()でusertypecomboBoxを定義し、protected void login()にはその定義が含まれていないため、最も外側のpublic class loginframe extends JFrameで定義するだけで済みます。同時に、定義を変更することを忘れないでください。定義の繰り返しを避けるために、public loginframe()で。
public class loginframe extends JFrame {
private JPanel contentPane;
private JTextField usernametextField;
private JTextField passwordtextField;
private JComboBox usertypecomboBox ; //
...
public loginframe() {
...
usertypecomboBox = new JComboBox();
usertypecomboBox.setModel(new DefaultComboBoxModel(new String[] {"系统管理员", "教师", "学生"}));
次の問題は、要素が定義されていないことです。これは、最終的なコードをリリースしたためです。一部の最適化では、他のパッケージと要素定義が追加されています。ここでは、全員を参加させます。
1)元のビューパッケージに基づいて4つのパッケージを作成します。つまり、dao(データベース関連コードに格納)画像(ストア画像)モデル(ストア列挙など)util(ストア接続と判断条件)
2)モデルに新しい列挙型クラスのユーザータイプを作成します(コードテンプレートを自動的に生成する方法)
モデル>>新規>>列挙型
package model;
public enum usertype {
ADMIN("系统管理员",0),TEACHER("教师",1),STUDENT("学生",2);
private String name;
private int index;
private usertype(String name,int index){
this.name = name;
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public String toString(){
return this.name;
}
}
ログインフレームに列挙クラスを導入する
usertypecomboBox.setModel(new DefaultComboBoxModel(new usertype[] {usertype.ADMIN, usertype.TEACHER, usertype.STUDENT}));//用户类型的值为枚举类中设定的
3)utilでstringutilを生成します
util >> new >> class
package util;
/*
* 判断字符串是否为空
*/
public class stringutil {
public static boolean isEmpty(String str){
if("".equals(str)|| str == null){
return true;
}
return false;
}
}
4)モデルに新しいAdminクラスを作成します
モデル>>新規>>クラス
package model;
public class Admin {
private int id;
private String name;
private String password;
private String createdate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCreatedate() {
return createdate;
}
public void setCreatedate(String createdate) {
this.createdate = createdate;
}
}
5)daoで新しいAdmindaoを作成します
dao >> new >> class
package dao;
import java.sql.SQLException;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import model.Admin;
public class Admindao extends Basedao {
/*管理员登录,查询用户名和密码,是否和数据库中的匹配
*
*/
public Admin login(Admin admin) {
String sql="select * from admin where name=? and password=?";
Admin admin2=null;
try {
PreparedStatement preparedStatement=(PreparedStatement) con.prepareStatement(sql);
preparedStatement.setString(1, admin.getName());
preparedStatement.setString(2, admin.getPassword());
ResultSet resultSet=(ResultSet) preparedStatement.executeQuery();
if(resultSet.next()){
admin2=new Admin();
admin2.setId(resultSet.getInt("id"));
admin2.setName(resultSet.getString("name"));
admin2.setPassword(resultSet.getString("password"));
admin2.setCreatedate(resultSet.getString("createdate"));
}
} catch (SQLException e) {
// TODO: handle exception
}
return admin2;
}
/*
* 修改密码,先查询旧密码,再更新新密码
*/
public String editPassword(Admin admin,String newPassword){
String sql = "select * from admin where id=? and password=?";
PreparedStatement prst = null;
int id = 0;
try {
prst = (PreparedStatement) con.prepareStatement(sql);
prst.setInt(1, admin.getId());
prst.setString(2, admin.getPassword());
ResultSet executeQuery = (ResultSet) prst.executeQuery();
if(!executeQuery.next()){
String retString = "旧密码错误!";
return retString;
}
id = executeQuery.getInt("id");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String retString = "修改失败";
String sqlString = "update admin set password = ? where id = ?";
try {
prst = (PreparedStatement) con.prepareStatement(sqlString);
prst.setString(1, newPassword);
prst.setInt(2, id);
int rst = prst.executeUpdate();
if(rst > 0){
retString = "密码修改成功!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retString;
}
}
6)jarパッケージをインポートします
mysql-connector-java-5.0.4-bin(添付ファイル)
新しいlibフォルダーを作成し、コピーしてjarパッケージに貼り付け、パスをビルドします>> ALL
7)daoパッケージに新しいBasedaoを作成します
dao >> new >> Basedao
package dao;
import java.sql.Connection;
import java.sql.SQLException;
import util.Dbutil;
public class Basedao {
//创建数据库连接对象
public Connection con=new Dbutil().getCon();
public void closeDao() {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
8)utilに新しいDButilを作成します
util >> new >> DButil
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Dbutil {
private String dbUrl="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; // 数据库连接地址
private String dbUserName="root"; // 用户名 此处用户名和密码均为举例,需要实际输入你电脑
// 上的数据库名和密码等
private String dbPassword="root"; // 密码
private String jdbcName="com.mysql.jdbc.Driver"; // 驱动名称
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon(){
try {
Class.forName(jdbcName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = null;
try {
con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
/**
* 关闭数据库连接
* @param con
* @throws Exception
*/
public void closeCon(Connection con)throws Exception{
if(con!=null){
con.close();
}
}
public static void main(String[] args) {
Dbutil dbUtil=new Dbutil();
try {
dbUtil.getCon();
System.out.println("数据库连接成功!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
}
9.mainframe.javaを改善します
1)要素を定義する
public class mainframe extends JFrame {
private JPanel contentPane;
public static usertype usertype;
public static Object userobject;
private JDesktopPane desktopPane;
...
2)元のスタイルをキャンセルする
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// mainframe frame = new mainframe();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
3)
public mainframe(usertype usertype,Object userObject) {
this.usertype=usertype;
this.userobject=userObject;
...
setLocationRelativeTo(null);
....
4)監視イベントを追加してパスワードを変更します
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
editpassword(ae);
}
});
5)出口システムに監視イベントを追加します
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(JOptionPane.showConfirmDialog(mainframe.this, "确定要退出吗?")==JOptionPane.OK_OPTION){
System.exit(0);
}
}
});
6)追加したクラスにリスナーイベントを追加します
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// //addclassframe addclassframe=new addclassframe();
// addclassframe.setVisible(true);
// desktopPane.add(addclassframe);
}
});
7)editpasswordを定義する
protected void editpassword(ActionEvent ae) {
// TODO Auto-generated method stub
editpasswordframe editpasswordframe = new editpasswordframe();
editpasswordframe.setVisible(true);
desktopPane.add(editpasswordframe);
}
10.editpassword.javaを改善します
1)ラベルを定義する
public class editpasswordframe extends JFrame {
private JPanel contentPane;
private JTextField oldpwdtextField;
private JTextField newpwdtextField;
private JTextField confirmpwdField;
private JLabel label_1;
private JLabel label_2;
private JLabel currentUserLabel;
2)元のインターフェースもログオフします
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// editpasswordframe frame = new editpasswordframe();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
3)設定ウィンドウを閉じ、最小化されたウィンドウを設定します
setClosable(true);//设置窗口关闭
setIconifiable(true);//设置最小化的窗口
4)確認ボタンのリスニングイベントを追加します
submitbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
submit(e);//确认按钮的监听实现方法,具体实现在下面
}
});
5)リセットボタンのリスニングイベントを追加します
resetbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
resetvalue(ae);//重置按钮的监听实现方法,具体实现在下面
}
});
6)現在のユーザー名を取得します
/*
* 获取当前登录的用户名字
*/
if("系统管理员".equals(mainframe.usertype.getName())){
Admin admin=(Admin) mainframe.userobject;
currentUserLabel.setText("【系统管理员】"+admin.getName());
}
}
7)確認イベントとリセットイベントを定義します
protected void submit(ActionEvent e) {
// TODO Auto-generated method stub
String oldpwd=oldpwdtextField.getText().toString();
String newpwd=newpwdtextField.getText().toString();
String confirmpwd=confirmpwdtextField.getText().toString();
if(stringutil.isEmpty(oldpwd)){
JOptionPane.showMessageDialog(this, "旧密码不能为空");
return;
}
if(stringutil.isEmpty(newpwd)){
JOptionPane.showMessageDialog(this, "新密码不能为空");
return;
}
if(stringutil.isEmpty(confirmpwd)){
JOptionPane.showMessageDialog(this, "确认密码不能为空");
return;
}
if(!newpwd.equals(confirmpwd)){
JOptionPane.showMessageDialog(this, "两次密码输入不一致");
return;
}
if("系统管理员".equals(mainframe.usertype.getName())){
Admindao adminDao = new Admindao();
Admin adminTmp = new Admin();
Admin admin = (Admin)mainframe.userobject;
adminTmp.setName(admin.getName());
adminTmp.setId(admin.getId());
adminTmp.setPassword(oldpwd);
JOptionPane.showMessageDialog(this, adminDao.editPassword(adminTmp,newpwd));
adminDao.closeDao();
return;
}
}
protected void resetvalue(ActionEvent ae) {
// TODO Auto-generated method stub
oldpwdtextField.setText("");
newpwdtextField.setText("");
confirmpwdField.setText("");
}
すべてのコード:
package dao;
import java.sql.SQLException;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import model.Admin;
public class Admindao extends Basedao {
/*管理员登录,查询用户名和密码,是否和数据库中的匹配
*
*/
public Admin login(Admin admin) {
String sql="select * from admin where name=? and password=?";
Admin admin2=null;
try {
PreparedStatement preparedStatement=(PreparedStatement) con.prepareStatement(sql);
preparedStatement.setString(1, admin.getName());
preparedStatement.setString(2, admin.getPassword());
ResultSet resultSet=(ResultSet) preparedStatement.executeQuery();
if(resultSet.next()){
admin2=new Admin();
admin2.setId(resultSet.getInt("id"));
admin2.setName(resultSet.getString("name"));
admin2.setPassword(resultSet.getString("password"));
admin2.setCreatedate(resultSet.getString("createdate"));
}
} catch (SQLException e) {
// TODO: handle exception
}
return admin2;
}
/*
* 修改密码,先查询旧密码,再更新新密码
*/
public String editPassword(Admin admin,String newPassword){
String sql = "select * from admin where id=? and password=?";
PreparedStatement prst = null;
int id = 0;
try {
prst = (PreparedStatement) con.prepareStatement(sql);
prst.setInt(1, admin.getId());
prst.setString(2, admin.getPassword());
ResultSet executeQuery = (ResultSet) prst.executeQuery();
if(!executeQuery.next()){
String retString = "旧密码错误!";
return retString;
}
id = executeQuery.getInt("id");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String retString = "修改失败";
String sqlString = "update admin set password = ? where id = ?";
try {
prst = (PreparedStatement) con.prepareStatement(sqlString);
prst.setString(1, newPassword);
prst.setInt(2, id);
int rst = prst.executeUpdate();
if(rst > 0){
retString = "密码修改成功!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retString;
}
}
package dao;
import java.sql.Connection;
import java.sql.SQLException;
import util.Dbutil;
public class Basedao {
//创建数据库连接对象
public Connection con=new Dbutil().getCon();
public void closeDao() {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package model;
public class Admin {
private int id;
private String name;
private String password;
private String createdate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCreatedate() {
return createdate;
}
public void setCreatedate(String createdate) {
this.createdate = createdate;
}
}
package model;
public enum usertype {
ADMIN("系统管理员",0),TEACHER("教师",1),STUDENT("学生",2);
private String name;
private int index;
private usertype(String name,int index){
this.name = name;
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public String toString(){
return this.name;
}
}
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Dbutil {
private String dbUrl="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; // 数据库连接地址
private String dbUserName="root"; // 用户名
private String dbPassword="root"; // 密码
private String jdbcName="com.mysql.jdbc.Driver"; // 驱动名称
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon(){
try {
Class.forName(jdbcName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = null;
try {
con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
/**
* 关闭数据库连接
* @param con
* @throws Exception
*/
public void closeCon(Connection con)throws Exception{
if(con!=null){
con.close();
}
}
public static void main(String[] args) {
Dbutil dbUtil=new Dbutil();
try {
dbUtil.getCon();
System.out.println("数据库连接成功!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
}
package util;
/*
* 判断字符串是否为空
*/
public class stringutil {
public static boolean isEmpty(String str){
if("".equals(str)|| str == null){
return true;
}
return false;
}
}
package view;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import dao.Admindao;
import model.Admin;
import util.stringutil;
public class editpasswordframe extends JInternalFrame {
private JPanel contentPane;
private JLabel label_1;
private JLabel currentUserLabel;
private JTextField newpwdtextField;
private JLabel label_2;
private JTextField confirmpwdtextField;
private JTextField oldpwdtextField;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// editpasswordframe frame = new editpasswordframe();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
public editpasswordframe() {
setTitle("\u7BA1\u7406\u5458\u4FEE\u6539\u5BC6\u7801");
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 626, 500);
setClosable(true);//设置窗口关闭
setIconifiable(true);//设置最小化的窗口
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u539F\u5BC6\u7801\uFF1A");
label.setFont(new Font("宋体", Font.PLAIN, 25));
label_1 = new JLabel("\u65B0\u5BC6\u7801\uFF1A");
label_1.setFont(new Font("宋体", Font.PLAIN, 25));
newpwdtextField = new JTextField();
newpwdtextField.setColumns(10);
label_2 = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");
label_2.setFont(new Font("宋体", Font.PLAIN, 25));
confirmpwdtextField = new JTextField();
confirmpwdtextField.setColumns(10);
oldpwdtextField = new JTextField();
oldpwdtextField.setColumns(10);
JButton submitbutton = new JButton("\u786E\u8BA4");
submitbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
submit(e);//确认按钮的监听实现方法,具体实现在下面
}
});
submitbutton.setFont(new Font("宋体", Font.PLAIN, 24));
JButton resetbutton = new JButton("\u91CD\u7F6E");
resetbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
resetvalue(ae);//重置按钮的监听实现方法,具体实现在下面
}
});
resetbutton.setFont(new Font("宋体", Font.PLAIN, 24));
JLabel label_3 = new JLabel("\u5F53\u524D\u7528\u6237\uFF1A");
label_3.setFont(new Font("宋体", Font.PLAIN, 25));
currentUserLabel = new JLabel("");
//currentUserLabel.setEnabled(false);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(submitbutton)
.addGap(83)
.addComponent(resetbutton)
.addGap(36))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(109)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label_1)
.addComponent(label)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
.addComponent(label_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(label_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGap(13)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(1)
.addComponent(confirmpwdtextField, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(newpwdtextField, GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(currentUserLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(oldpwdtextField, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE))))))
.addContainerGap(152, GroupLayout.PREFERRED_SIZE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(37)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
.addComponent(currentUserLabel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
.addGap(34)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(label)
.addComponent(oldpwdtextField, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGap(47)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1)
.addComponent(newpwdtextField, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGap(52)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(label_2)
.addComponent(confirmpwdtextField, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(submitbutton, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
.addComponent(resetbutton))
.addGap(34))
);
contentPane.setLayout(gl_contentPane);
/*
* 获取当前登录的用户名字
*/
if("系统管理员".equals(mainframe.usertype.getName())){
Admin admin=(Admin) mainframe.userobject;
currentUserLabel.setText("【系统管理员】"+admin.getName());
}
}
protected void submit(ActionEvent e) {
// TODO Auto-generated method stub
String oldpwd=oldpwdtextField.getText().toString();
String newpwd=newpwdtextField.getText().toString();
String confirmpwd=confirmpwdtextField.getText().toString();
if(stringutil.isEmpty(oldpwd)){
JOptionPane.showMessageDialog(this, "旧密码不能为空");
return;
}
if(stringutil.isEmpty(newpwd)){
JOptionPane.showMessageDialog(this, "新密码不能为空");
return;
}
if(stringutil.isEmpty(confirmpwd)){
JOptionPane.showMessageDialog(this, "确认密码不能为空");
return;
}
if(!newpwd.equals(confirmpwd)){
JOptionPane.showMessageDialog(this, "两次密码输入不一致");
return;
}
if("系统管理员".equals(mainframe.usertype.getName())){
Admindao adminDao = new Admindao();
Admin adminTmp = new Admin();
Admin admin = (Admin)mainframe.userobject;
adminTmp.setName(admin.getName());
adminTmp.setId(admin.getId());
adminTmp.setPassword(oldpwd);
JOptionPane.showMessageDialog(this, adminDao.editPassword(adminTmp,newpwd));
adminDao.closeDao();
return;
}
}
protected void resetvalue(ActionEvent ae) {
// TODO Auto-generated method stub
oldpwdtextField.setText("");
newpwdtextField.setText("");
confirmpwdtextField.setText("");
}
}
package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import dao.Admindao;
import model.Admin;
import model.usertype;
import util.stringutil;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
public class loginframe extends JFrame {
private JPanel contentPane;
private JTextField usernametextField;
private JTextField passwordtextField;
private JComboBox usertypecomboBox ;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
loginframe frame = new loginframe();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public loginframe() {
setTitle("学生管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 587, 453);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
setLocationRelativeTo(null);//窗口将置于屏幕的中央
JLabel lblNewLabel = new JLabel("学生管理系统");
lblNewLabel.setFont(new Font("华文楷体", Font.PLAIN, 25));
JLabel label = new JLabel("用 户 名:",JLabel.RIGHT);
JLabel lable_1 = new JLabel("密 码:",JLabel.RIGHT);
JLabel lable_2 = new JLabel("用户类型:",JLabel.RIGHT);
usernametextField = new JTextField();
usernametextField.setColumns(10);
passwordtextField = new JTextField();
passwordtextField.setColumns(10);
usertypecomboBox = new JComboBox();
usertypecomboBox.setModel(new DefaultComboBoxModel(new usertype[] {usertype.ADMIN, usertype.TEACHER, usertype.STUDENT}));//用户类型的值为枚举类中设定的
JButton loginbutton = new JButton("登录");
loginbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
login();//登录按钮实现的方法,具体实现在下面
}
});
JButton resetbutton = new JButton("重置");
resetbutton.addActionListener(new ActionListener() {//重置按钮的实现
public void actionPerformed(ActionEvent arg0) {
usernametextField.setText("");
passwordtextField.setText("");
usertypecomboBox.setSelectedIndex(0);
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(210)
.addComponent(lblNewLabel))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(85)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(lable_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lable_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(usertypecomboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(usernametextField)
.addComponent(passwordtextField, GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE))))
.addContainerGap(153, Short.MAX_VALUE))
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addGap(195)
.addComponent(loginbutton, 0, 0, Short.MAX_VALUE)
.addGap(49)
.addComponent(resetbutton, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addGap(183))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(30)
.addComponent(lblNewLabel)
.addGap(43)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(usernametextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(35)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lable_1)
.addComponent(passwordtextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(58)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lable_2)
.addComponent(usertypecomboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(37)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(loginbutton)
.addComponent(resetbutton))
.addGap(15))
);
contentPane.setLayout(gl_contentPane);
}
protected void login() {
String name=usernametextField.getText().toString();
String password=passwordtextField.getText().toString();
usertype selectItem=(usertype) usertypecomboBox.getSelectedItem();
if(stringutil.isEmpty(name)){
JOptionPane.showMessageDialog(this, "用户名不能为空"); //弹出提示性对话框
return;
}
if(stringutil.isEmpty(password)){
JOptionPane.showMessageDialog(this, "密码不能为空");
return;
}
Admin admin=null;
if("系统管理员".equals(selectItem.getName())){
Admindao admindao=new Admindao();
Admin admin2=new Admin();
admin2.setName(name);
admin2.setPassword(password);
admin=admindao.login(admin2);
if(admin==null){
JOptionPane.showMessageDialog(this, "用户名或者密码错误");
return;
}
this.dispose();//当前登录界面关闭
new mainframe(selectItem,admin).setVisible(true);//主页面显示
}
}
}
package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import model.usertype;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JDesktopPane;
import java.awt.Color;
public class mainframe extends JFrame {
private JPanel contentPane;
public static usertype usertype;
public static Object userobject;
private JDesktopPane desktopPane;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// mainframe frame = new mainframe();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
public mainframe(usertype usertype,Object userObject) {
this.usertype=usertype;
this.userobject=userObject;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 925, 707);
setLocationRelativeTo(null);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("系统设置");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("修改密码");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
editpassword(ae);
}
});
menu.add(menuItem);
JMenuItem menuItem_1 = new JMenuItem("退出系统");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(JOptionPane.showConfirmDialog(mainframe.this, "确定要退出吗?")==JOptionPane.OK_OPTION){
System.exit(0);
}
}
});
menu.add(menuItem_1);
JMenu menu_1 = new JMenu("班级管理");
menuBar.add(menu_1);
JMenuItem menuItem_2 = new JMenuItem("添加班级");
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// //addclassframe addclassframe=new addclassframe();
// addclassframe.setVisible(true);
// desktopPane.add(addclassframe);
}
});
menu_1.add(menuItem_2);
contentPane = new JPanel();
contentPane.setBackground(new Color(173, 216, 230));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(Color.CYAN);
contentPane.add(desktopPane, BorderLayout.NORTH);
}
protected void editpassword(ActionEvent ae) {
// TODO Auto-generated method stub
editpasswordframe editpasswordframe = new editpasswordframe();
editpasswordframe.setVisible(true);
desktopPane.add(editpasswordframe);
}
}