Java GUI实现学生成绩管理系统
程序员文章站
2023-12-10 14:47:10
学习java有一年多了,一直在做web方面的练习,在一个项目中发现需要用到gui的相关知识,结果没法做出来,网上这方面的文章又不是很多,所有只好自己硬着头皮从头再学一遍了,...
学习java有一年多了,一直在做web方面的练习,在一个项目中发现需要用到gui的相关知识,结果没法做出来,网上这方面的文章又不是很多,所有只好自己硬着头皮从头再学一遍了,不过学习后发现,其实gui是非常有趣的,他并不像web程序一样依赖互联网,而且还有许多布局和android相差不是很大,这才发现自己竟又爱上gui的开发了,不多说了,直接上代码吧,相信有过android或相关界面开发的都明白这其中的道理。
先看看效果吧
1.登录主界面
package edu.gzu.stumanager; import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import java.awt.toolkit; import javax.swing.jtextfield; import edu.gzu.stumanager.dao.userloginvalid; import edu.gzu.stumanager.domain.studentinfo; import edu.gzu.stumanager.view.studentmainview; import java.awt.choice; import java.awt.font; import java.awt.button; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; public class mainui { private jframe frame; private jtextfield textfield; private jtextfield textfield_1; /** * launch the application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { mainui window = new mainui(); window.frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create the application. */ public mainui() { initialize(); } /** * initialize the contents of the frame. */ private void initialize() { frame = new jframe(); frame.settitle("\u6210\u7ee9\u7ba1\u7406\u7cfb\u7edf\uff08\u767b\u5f55\uff09"); frame.seticonimage(toolkit.getdefaulttoolkit().getimage(mainui.class.getresource("/image/func_list7_privmana.png"))); frame.setbounds(400, 250, 450, 300); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(null); jlabel lblnewlabel = new jlabel("\u5b66\u751f\u6210\u7ee9\u7ba1\u7406\u7cfb\u7edf\u7528\u6237\u767b\u5f55\uff01"); lblnewlabel.setfont(new font("宋体", font.plain, 16)); lblnewlabel.setbounds(111, 17, 287, 15); frame.getcontentpane().add(lblnewlabel); jlabel lblnewlabel_1 = new jlabel("\u7528\u6237\u540d\uff1a"); lblnewlabel_1.setfont(new font("宋体", font.plain, 14)); lblnewlabel_1.setbounds(87, 67, 67, 15); frame.getcontentpane().add(lblnewlabel_1); textfield = new jtextfield(); textfield.setbounds(154, 64, 141, 21); frame.getcontentpane().add(textfield); textfield.setcolumns(10); jlabel label = new jlabel("\u5bc6 \u7801\uff1a"); label.setfont(new font("宋体", font.plain, 14)); label.setbounds(87, 108, 67, 15); frame.getcontentpane().add(label); textfield_1 = new jtextfield(); textfield_1.setcolumns(10); textfield_1.setbounds(154, 103, 141, 21); frame.getcontentpane().add(textfield_1); jlabel lblnewlabel_2 = new jlabel("\u6211\u7684\u8eab\u4efd\u662f\uff1a"); lblnewlabel_2.setfont(new font("宋体", font.plain, 14)); lblnewlabel_2.setbounds(105, 150, 97, 15); frame.getcontentpane().add(lblnewlabel_2); final choice choice = new choice(); choice.setbounds(210, 147, 74, 21); choice.add("学生"); choice.add("教师"); choice.add("系统管理员"); frame.getcontentpane().add(choice); button button = new button("\u767b\u5f55"); button.setbounds(87, 194, 76, 23); button.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { string user=textfield.gettext(); string password=textfield_1.gettext(); string shenfen=choice.getselecteditem(); if(user.equals("")||user==null){ joptionpane.showmessagedialog(frame, shenfen+":您好,帐号不能为空!"); return; }else if(password.equals("")||password==null){ joptionpane.showmessagedialog(frame, shenfen+":您好,密码不能为空!"); return; }else{ studentinfo stu=new studentinfo(integer.parseint(user), integer.parseint(password),shenfen); userloginvalid dao=new userloginvalid(); string result=dao.isvalid(stu); if("登录成功!".equals(result)){ joptionpane.showmessagedialog(frame,result); studentmainview index=new studentmainview(stu); jframe frame2=index.getframe(); frame2.setvisible(true); frame.dispose(); }else{ joptionpane.showmessagedialog(frame,result); } } } }); frame.getcontentpane().add(button); button button_1 = new button("\u53d6\u6d88"); button_1.setbounds(219, 194, 76, 23); frame.getcontentpane().add(button_1); } }
2.登录验证逻辑
package edu.gzu.stumanager.dao; import edu.gzu.stumanager.domain.studentinfo; public class userloginvalid { public string isvalid(studentinfo stu){ int idnum=stu.getidnum(); int password=stu.getpassword(); string idntify=stu.getidentify(); string result=null; if("学生".equals(idntify)){ if(idnum==1207010209&&password==123){ stu.setname("刘明胜 同学"); result="登录成功!"; }else{ result="学生帐号中不存在此用户,请确认身份后再次登录!"; } }else if("教师".equals(idntify)){ if(idnum==1174386356&&password==123){ stu.setname("刘明胜 老师"); result="登录成功!"; }else{ result="教师帐号中不存在此用户,请确认身份后再次登录!"; } }else if("系统管理员".equals(idntify)){ if(idnum==999999&&password==123){ stu.setname("系统管理员"); result="登录成功!"; }else{ result="系统管理员帐号中不存在此用户,请确认身份后再次登录!"; } } return result; } }
3.用户对象(这是一个简单bean)
package edu.gzu.stumanager.domain; public class studentinfo { private int idnum; private string name; private int password; private string identify; public studentinfo(int idnum,int password, string identify) { super(); this.idnum = idnum; this.password = password; this.identify = identify; } public int getidnum() { return idnum; } public void setidnum(int idnum) { this.idnum = idnum; } public string getname() { return name; } public void setname(string name) { this.name = name; } public int getpassword() { return password; } public void setpassword(int password) { this.password = password; } public string getidentify() { return identify; } public void setidentify(string identify) { this.identify = identify; } }
4.登录成功后的主界面
package edu.gzu.stumanager.view; import java.awt.button; import java.awt.canvas; import java.awt.choice; import java.awt.color; import java.awt.toolkit; import java.awt.event.itemevent; import java.awt.event.itemlistener; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield; import edu.gzu.stumanager.domain.studentinfo; public class studentmainview{ private jframe frame; private jtextfield textfield; private jtextfield textfield_1; private jtextfield textfield_2; private jtextfield textfield_3; private studentinfo info; /** * create the frame. * @wbp.parser.entrypoint */ public studentmainview(studentinfo info) { this.info=info; } public jframe getframe(){ initialize(); return frame; } /** * initialize the contents of the frame. * @wbp.parser.entrypoint */ public void initialize() { frame = new jframe(); frame.settitle("\u6210\u7ee9\u7ba1\u7406\u7cfb\u7edf\uff08\u5b66\u751f\u7248\uff09"); frame.seticonimage(toolkit.getdefaulttoolkit().getimage(studentmainview.class.getresource("/image/func_list7_privmana.png"))); frame.setbounds(300,150, 550, 300); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(null); jlabel lblnewlabel = new jlabel("欢迎【"+info.getname()+"】登录学生成绩管理系统!"); lblnewlabel.setbounds(54, 10, 322, 15); frame.getcontentpane().add(lblnewlabel); jpanel panel = new jpanel(); panel.setbounds(29, 52, 250, 180); frame.getcontentpane().add(panel); panel.setlayout(null); jlabel lblnewlabel_1 = new jlabel("\u6210\u7ee9\u6570\u636e"); lblnewlabel_1.setbounds(94, 10, 65, 15); panel.add(lblnewlabel_1); jlabel lblnewlabel_2 = new jlabel("\u5b66\u53f7\uff1a"); lblnewlabel_2.setbounds(22, 37, 40, 15); panel.add(lblnewlabel_2); textfield = new jtextfield(); textfield.setbounds(72, 35, 154, 21); textfield.settext(info.getidnum()+""); panel.add(textfield); textfield.setcolumns(10); jlabel lblnewlabel_3 = new jlabel("\u59d3\u540d\uff1a"); lblnewlabel_3.setbounds(22, 67, 44, 15); panel.add(lblnewlabel_3); textfield_1 = new jtextfield(); textfield_1.setbounds(72, 66, 154, 21); textfield_1.settext(info.getname()); panel.add(textfield_1); textfield_1.setcolumns(10); canvas canvas = new canvas(); canvas.setbackground(color.blue); canvas.setbounds(22, 100, 205, 1); panel.add(canvas); jlabel lblnewlabel_4 = new jlabel("\u8bfe\u7a0b\u540d"); lblnewlabel_4.setbounds(22, 116, 47, 15); panel.add(lblnewlabel_4); jlabel lblnewlabel_5 = new jlabel("\u6210\u7ee9"); lblnewlabel_5.setbounds(160, 116, 43, 15); panel.add(lblnewlabel_5); textfield_2 = new jtextfield(); textfield_2.setbounds(22, 140, 123, 21); panel.add(textfield_2); textfield_2.setcolumns(10); textfield_3 = new jtextfield(); textfield_3.setbounds(159, 140, 66, 21); panel.add(textfield_3); textfield_3.setcolumns(10); jpanel panel_1 = new jpanel(); panel_1.setbounds(317, 52, 110, 180); frame.getcontentpane().add(panel_1); panel_1.setlayout(null); jlabel lblnewlabel_6 = new jlabel("\u64cd\u4f5c\u83dc\u5355"); lblnewlabel_6.setbounds(15, 10, 54, 15); panel_1.add(lblnewlabel_6); button button = new button("\u7b2c\u4e00\u95e8\u8bfe\u7a0b"); button.setbounds(10, 31, 76, 23); panel_1.add(button); button button_1 = new button("\u4e0b\u4e00\u95e8\u8bfe\u7a0b"); button_1.setbounds(10, 61, 76, 23); panel_1.add(button_1); button button_2 = new button("\u4e0a\u4e00\u95e8\u8bfe\u7a0b"); button_2.setactioncommand("\u4e0a\u4e00\u95e8\u8bfe\u7a0b"); button_2.setbounds(10, 90, 76, 23); panel_1.add(button_2); button button_3 = new button("\u6700\u540e\u4e00\u95e8\u8bfe"); button_3.setbounds(10, 117, 76, 23); panel_1.add(button_3); choice choice = new choice(); choice.setbounds(10, 149, 76, 21); choice.add("选择课程"); choice.add("高等数学"); choice.add("大学英语"); choice.add("马列主义"); choice.add("*思想"); choice.add("计算机图形学"); choice.additemlistener(new itemlistener() { @override public void itemstatechanged(itemevent e) { object[] objs=e.getitemselectable().getselectedobjects(); for(object ob:objs){ // joptionpane.showmessagedialog(frame, ob.tostring()); if("高等数学".equals(ob.tostring())){ textfield_2.settext("高等数学"); textfield_3.settext("98"); }else if("大学英语".equals(ob.tostring())){ textfield_2.settext("大学英语"); textfield_3.settext("87"); }else if("马列主义".equals(ob.tostring())){ textfield_2.settext("马列主义"); textfield_3.settext("88"); }else if("*思想".equals(ob.tostring())){ textfield_2.settext("*思想"); textfield_3.settext("73"); }else if("计算机图形学".equals(ob.tostring())){ textfield_2.settext("计算机图形学"); textfield_3.settext("97"); } } } }); panel_1.add(choice); } }
这样就能轻松实现登录验证了,本打算从数据库读取数据的,由于时间关系,这里就简单的直接验证了,后续有时间的话再做其他的部分。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。