Java GUI制作简单的管理系统
本文实例为大家分享了Java GUI管理系统的具体代码,供大家参考,具体内容如下
1.先完成主页面MainUI(代码如下)
package com.pag_1; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MainUI extends JFrame implements ActionListener { //定义组件 JButton jb1,jb2,jb3=null; JRadioButton jrb1,jrb2=null; JPanel jp1,jp2,jp3,jp4=null; JTextField jtf=null; JLabel jlb1,jlb2,jlb3=null; JPasswordField jpf=null; ButtonGroup bg=null; //设定用户名和密码 final String stu_name="王小明"; final String stu_pwd="1"; final String stu_num="14140301"; final String tea_name="王老师"; final String tea_pwd="1"; final String tea_num="00001"; public static void main(String[] args) { MainUI mUI=new MainUI(); } public MainUI() { //创建组件 jb1=new JButton("登录"); jb2=new JButton("重置"); jb3=new JButton("退出"); //设置监听 jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jrb1=new JRadioButton("教师"); jrb2=new JRadioButton("学生"); bg=new ButtonGroup(); bg.add(jrb1); bg.add(jrb2); jrb2.setSelected(true); //初始页面默认选择权限为 学生 jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); jp4=new JPanel(); jlb1=new JLabel("用户名:"); jlb2=new JLabel("密 码:"); jlb3=new JLabel("权 限:"); jtf=new JTextField(10); jpf=new JPasswordField(10); //加入到JPanel中 jp1.add(jlb1); jp1.add(jtf); jp2.add(jlb2); jp2.add(jpf); jp3.add(jlb3); //添加标签 jp3.add(jrb1); jp3.add(jrb2); jp4.add(jb1); //添加按钮 jp4.add(jb2); jp4.add(jb3); //加入JFrame中 this.add(jp1); this.add(jp2); this.add(jp3); this.add(jp4); this.setLayout(new GridLayout(4,1)); //选择GridLayout布局管理器 this.setTitle("学生成绩管理系统"); this.setSize(300,200); this.setLocation(400, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置当关闭窗口时,保证JVM也退出 this.setVisible(true); this.setResizable(true); } public void actionPerformed(ActionEvent e) { //事件判断 if(e.getActionCommand()=="登录") { //如果选中教师登录 if(jrb1.isSelected()) { tealogin(); //连接到教师的方法 页面 }else if(jrb2.isSelected()) //学生在登录系统 { stulogin(); //连接到学生的方法 页面 } }else if(e.getActionCommand()=="重置") { clear(); } } //学生登录判断方法 public void stulogin() { if(stu_name.equals(jtf.getText())&&stu_pwd.equals(jpf.getText())) { JOptionPane.showMessageDialog(null,"登录成功!","提示消息",JOptionPane.WARNING_MESSAGE); dispose(); clear(); StdUI ui=new StdUI(); //创建新界面 }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名和密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jtf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else { JOptionPane.showMessageDialog(null,"用户名或者密码错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE); //清空输入框 clear(); } } //教师登录判断方法 public void tealogin() { if(tea_name.equals(jtf.getText())&&tea_pwd.equals(jpf.getText())) { JOptionPane.showMessageDialog(null,"登录成功!","提示消息",JOptionPane.WARNING_MESSAGE); clear(); dispose(); TerUI ui=new TerUI(); //创建一个新界面 }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名和密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jtf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else { JOptionPane.showMessageDialog(null,"用户名或者密码错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE); clear(); //清空输入框 } } //清空文本框和密码框 public void clear() { jtf.setText(""); jpf.setText(""); } }
运行图1
用户名和密码如下:
2.完成学生界面StuUI,这个页面在MainUI界面设置了按钮连接(代码如下)
package com.pag_1; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JButton; import java.awt.color.*; import javax.swing.JOptionPane; import com.pag_1.MainUI; public class StdUI extends JFrame implements ActionListener { //定义组件 JButton jb1=new JButton(); JButton jb2=new JButton(); JPanel jp1,jp2,jp3,jp4=null; JLabel jlb1,jlb2,jlb3,jlb4,jlb5,jlb6=null; public static void main(String[] args) { StdUI ui=new StdUI(); } //****************************事件判断********************** //构造函数 public StdUI() //不能申明为void!!!!!否则弹不出新界面 { //创建组件 jb1=new JButton("课程表"); jb1.setForeground(Color.BLUE); jb2=new JButton("成绩查询"); jb2.setForeground(Color.BLUE); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); jlb1=new JLabel("姓名:"); jlb2=new JLabel("学号:"); jlb3=new JLabel("最新公告:"); jlb3.setForeground(Color.red); jlb4=new JLabel("我院举行学生体测通知"); jlb5=new JLabel(new MainUI().stu_name); jlb6=new JLabel(new MainUI().stu_num); jp1.add(jlb1); jp1.add(jlb5); jp1.add(jlb2); jp1.add(jlb6); jp2.add(jb1); jp2.add(jb2); jp3.add(jlb3); jp3.add(jlb4); this.add(jp1); this.add(jp2); this.add(jp3); //设置布局管理器 this.setLayout(new GridLayout(4,3,50,50)); this.setTitle("学生成绩管理系统"); this.setSize(400,300); this.setLocation(200, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); jb1.addActionListener(this); jb2.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == jb1){ //关闭当前界面 dispose(); new KeChengBiaoUI(); }else if(e.getSource() == jb2){ //关闭当前界面 dispose(); new ChengJiBiaoUI(); } } }
运行MainUI输入用户学生,会出现下面学生界面,其中界面中的 课程表和成绩查询是两个可以点击的按钮,会连接到下个页面再后面步骤。
3.完成教师界面TerUI,这个和学生页面一样,在MainUI页面有设置按钮事件,连接到MainUI页面.(代码如下)
package com.pag_1; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JButton; import com.pag_1.MainUI; public class TerUI extends JFrame implements ActionListener { //定义组件 JButton jb1,jb2=null; JPanel jp1,jp2,jp3,jp4=null; JLabel jlb1,jlb2,jlb3,jlb4,jlb5,jlb6=null; public static void main(String[] args) { TerUI ui=new TerUI(); } public TerUI() { //创建组件 jb1=new JButton("课程管理"); jb1.setForeground(Color.BLUE); jb2=new JButton("学生名单"); jb2.setForeground(Color.BLUE); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); jlb1=new JLabel("姓名:"); jlb2=new JLabel("工号:"); jlb3=new JLabel("最新公告:"); jlb3.setForeground(Color.red); jlb4=new JLabel("我院举行体测的通知"); jlb5=new JLabel(new MainUI().tea_name); jlb6=new JLabel(new MainUI().tea_num); jp1.add(jlb1); jp1.add(jlb5); jp1.add(jlb2); jp1.add(jlb6); jp2.add(jb1); jp2.add(jb2); jp3.add(jlb3); jp3.add(jlb4); this.add(jp1); this.add(jp2); this.add(jp3); this.setLayout(new GridLayout(4,3,50,50)); //设置布局管理器 this.setTitle("学生成绩管理系统"); this.setSize(400,300); this.setLocation(200, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); jb1.addActionListener(this); jb2.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == jb1){ dispose(); new KeChengGuanLiUI(); //创建一个新界面 }else if(e.getSource() == jb2){ dispose(); new XueShengMingDanUI(); //创建一个新界面 } } }
运行MainUI输入用户教师,会出现下面教师界面,其中界面中的 课程管理和学生名单是两个可以点击的按钮,会连接到下个页面再后面步骤。
4.完成学生界面的课程表界面(代码如下)
package com.pag_1; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.TableColumn; public class KeChengBiaoUI extends JFrame { public KeChengBiaoUI() { intiComponent(); } private void intiComponent() { String[] columnNames = { "课节数","星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期日" }; Object[][] obj=new Object[8][8]; for (int i=0;i<8;i++) { for(int j=0;j<8;j++) { switch (j) { case 0: obj[0][0] = "第一节课"; obj[0][1] = "光纤通信"; obj[0][2] = "电磁场"; obj[0][3] = "物联网"; obj[0][4] = "DSP实验"; obj[0][5] = " "; obj[0][6] = "手机维修"; obj[0][7] = " "; break; case 1: obj[1][0] = "第二节课"; obj[1][1] = "光纤通信"; obj[1][2] = "电磁场"; obj[1][3] = "物联网"; obj[1][4] = "DSP实验"; obj[1][5] = " "; obj[1][6] = "手机维修"; obj[1][7] = " "; break; case 2: obj[2][0] = "第三节课"; obj[2][1] = "Android"; obj[2][2] = " "; obj[2][3] = " "; obj[2][4] = "DSP实验"; obj[2][5] = "手机维修"; obj[2][6] = "手机维修"; obj[2][7] = " "; break; case 3: obj[3][0] = "第四节课"; obj[3][1] = "Android"; obj[3][2] = " "; obj[3][3] = " "; obj[3][4] = " "; obj[3][5] = "手机维修"; obj[3][6] = "手机维修"; obj[3][7] = "光纤通信"; break; case 4: obj[4][0] = "第五节课"; obj[4][1] = "Android实验"; obj[4][2] = " "; obj[4][3] = " "; obj[4][4] = " "; obj[4][5] = " "; obj[4][6] = " "; obj[4][7] = " "; break; case 5: obj[5][0] = "第六节课"; obj[5][1] = "Android实验"; obj[5][2] = " "; obj[5][3] = " "; obj[5][4] = " "; obj[5][5] = " "; obj[5][6] = " "; obj[5][7] = " "; break; case 6: obj[6][0] = "第七节课"; obj[6][1] = "Android实验"; obj[6][2] = " "; obj[6][3] = " "; obj[6][4] = " "; obj[6][5] = " "; obj[6][6] = " "; obj[6][7] = " "; break; case 7: obj[7][0] = "第七节课"; obj[7][1] = ""; obj[7][2] = " "; obj[7][3] = " "; obj[7][4] = " "; obj[7][5] = " "; obj[7][6] = " "; obj[7][7] = " "; break; case 8: obj[8][0] = "第八节课"; obj[8][1] = " "; obj[8][2] = " "; obj[8][3] = " "; obj[8][4] = " "; obj[8][5] = " "; obj[8][6] = " "; obj[8][7] = " "; break; } } } JTable table=new JTable(obj, columnNames); TableColumn column=null; int colunms = table.getColumnCount(); for(int i=0;i<colunms;i++) { column = table.getColumnModel().getColumn(i); column.setPreferredWidth(100); } table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); JScrollPane scroll = new JScrollPane(table); scroll.setSize(300, 50); add(scroll); this.setLocation(450, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); } public static void main(String[] args) { new KeChengBiaoUI(); } }
课程表界面
5.完成学生的成绩查询界面(代码如下)
package com.pag_1; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.TableColumn; public class ChengJiBiaoUI extends JFrame { public ChengJiBiaoUI() { intiComponent(); } //初始化窗体组件 private void intiComponent() { String[] columnNames = { "课程数目","课程名称", "学分", "绩点", "分数", "补考" }; //设置JTable的列名 Object[][] obj=new Object[7][7]; for (int i=0;i<7;i++) { for(int j=0;j<7;j++) { switch (j) { case 0: obj[0][0] = " 1"; obj[0][1] = " 光纤通信"; obj[0][2] = " 2"; obj[0][3] = " 1.5"; obj[0][4] = " 75"; obj[0][5] = " 否"; break; case 1: obj[1][0] = " 2"; obj[1][1] = " Android"; obj[1][2] = " 2"; obj[1][3] = " 1.8"; obj[1][4] = " 5"; obj[1][5] = " 否"; break; case 2: obj[2][0] = " 3"; obj[2][1] = " 物联网"; obj[2][2] = " 1.5"; obj[2][3] = " 1.2"; obj[2][4] = " 70"; obj[2][5] = " 否"; break; case 3: obj[3][0] = " 4"; obj[3][1] = " 电磁场"; obj[3][2] = " 2"; obj[3][3] = " 1.6"; obj[3][4] = " 78"; obj[3][5] = " 否"; break; case 4: obj[4][0] = " 5"; obj[4][1] = " 手机维修"; obj[4][2] = " 1.0"; obj[4][3] = " 1.0"; obj[4][4] = " 优秀"; obj[4][5] = " 否"; break; case 5: obj[5][0] = " 6"; obj[5][1] = " DSP"; obj[5][2] = " 1.0"; obj[5][3] = " 1.0"; obj[5][4] = " 良好"; obj[5][5] = " 否"; break; case 6: obj[6][0] = " 7"; obj[6][1] = " 通信原理实验"; obj[6][2] = " 1.0"; obj[6][3] = " 1.0"; obj[6][4] = " 良好"; obj[6][5] = " 否"; break; } } } JTable table=new JTable(obj, columnNames); //JTable的其中一种构造方法 TableColumn column=null; //设置JTable的列默认的宽度和高度 int colunms = table.getColumnCount(); for(int i=0;i<colunms;i++) { column = table.getColumnModel().getColumn(i); column.setPreferredWidth(100); //将每一列的默认宽度设置为100 } table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //设置JTable自动调整列表的状态,此处设置为关闭 JScrollPane scroll = new JScrollPane(table); //用JScrollPane装载JTable,这样超出范围的列就可以通过滚动条来查看 scroll.setSize(300, 50); add(scroll); this.setLocation(450, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); } public static void main(String[] args) { new ChengJiBiaoUI(); } }
学生成绩表界面
6.完成教师的课程管理界面(代码如下)
package com.pag_1; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.TableColumn; public class KeChengGuanLiUI extends JFrame { public KeChengGuanLiUI() { intiComponent(); } private void intiComponent() { String[] columnNames = { "课节数","星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期日" }; Object[][] obj=new Object[8][8]; for (int i=0;i<8;i++) { for(int j=0;j<8;j++) { switch (j) { case 0: obj[0][0] = " 第一节课"; obj[0][1] = " 有课"; obj[0][2] = " "; obj[0][3] = " "; obj[0][4] = " 有课"; obj[0][5] = " "; obj[0][6] = ""; obj[0][7] = " "; break; case 1: obj[1][0] = " 第二节课"; obj[1][1] = " 有课"; obj[1][2] = ""; obj[1][3] = ""; obj[1][4] = " 有课"; obj[1][5] = " "; obj[1][6] = ""; obj[1][7] = " "; break; case 2: obj[2][0] = " 第三节课"; obj[2][1] = ""; obj[2][2] = " "; obj[2][3] = " 有课"; obj[2][4] = ""; obj[2][5] = ""; obj[2][6] = ""; obj[2][7] = " "; break; case 3: obj[3][0] =" 第四节课"; obj[3][1] = ""; obj[3][2] = " "; obj[3][3] = " 有课"; obj[3][4] = " "; obj[3][5] = ""; obj[3][6] = ""; obj[3][7] = ""; break; case 4: obj[4][0] =" 第五节课"; obj[4][1] = ""; obj[4][2] = " "; obj[4][3] = " "; obj[4][4] = " "; obj[4][5] = " "; obj[4][6] = " "; obj[4][7] = " "; break; case 5: obj[5][0] =" 第六节课"; obj[5][1] = ""; obj[5][2] = " 开会"; obj[5][3] = " "; obj[5][4] = " "; obj[5][5] = " "; obj[5][6] = " "; obj[5][7] = " "; break; case 6: obj[6][0] =" 第七节课"; obj[6][1] = ""; obj[6][2] = " 有课"; obj[6][3] = " "; obj[6][4] = " 开会"; obj[6][5] = " "; obj[6][6] = " "; obj[6][7] = " "; break; case 7: obj[7][0] =" 第八节课"; obj[7][1] = ""; obj[7][2] = " 有课 "; obj[7][3] = " "; obj[7][4] = " "; obj[7][5] = " "; obj[7][6] = " "; obj[7][7] = " "; break; } } } JTable table=new JTable(obj, columnNames); TableColumn column=null; int colunms = table.getColumnCount(); for(int i=0;i<colunms;i++) { column = table.getColumnModel().getColumn(i); column.setPreferredWidth(100); } table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); JScrollPane scroll = new JScrollPane(table); scroll.setSize(300, 50); add(scroll); this.setLocation(450, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); } public static void main(String[] args) { new KeChengGuanLiUI(); } }
教师课程管理
7.完成教师页面的学生名单页面(代码如下)
package com.pag_1; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.TableColumn; public class XueShengMingDanUI extends JFrame { public XueShengMingDanUI() { intiComponent(); } //初始化窗体组件 private void intiComponent() { // 设置JTable的列名 String[] columnNames = { "学号","姓名", "专业", "班级"}; Object[][] obj=new Object[7][7]; for (int i=0;i<7;i++) { for(int j=0;j<7;j++) { switch (j) { case 0: obj[0][0] = " 14140301"; obj[0][1] = " 王小明"; obj[0][2] = " 通信工程"; obj[0][3] = " 2"; break; case 1: obj[1][0] = " 14140302"; obj[1][1] = " 王雷"; obj[1][2] = " 通信工程"; obj[1][3] = " 2"; break; case 2: obj[2][0] = " 14140303"; obj[2][1] = " 李芳"; obj[2][2] = " 通信工程"; obj[2][3] = " 2"; break; case 3: obj[3][0] = " 14140304"; obj[3][1] = " 张三"; obj[3][2] = " 通信工程"; obj[3][3] = " 2"; break; case 4: obj[4][0] = " 14140305"; obj[4][1] = " 李四"; obj[4][2] = " 通信工程"; obj[4][3] = " 2"; break; case 5: obj[5][0] = " 14140306"; obj[5][1] = " 文宇"; obj[5][2] = " 通信工程"; obj[5][3] = " 2"; break; case 6: obj[6][0] = " 14140307"; obj[6][1] = " 王飞"; obj[6][2] = " 通信工程"; obj[6][3] = " 2"; break; } } } JTable table=new JTable(obj, columnNames); //JTable的其中一种构造方法 TableColumn column=null; //设置JTable的列默认的宽度和高度 int colunms = table.getColumnCount(); for(int i=0;i<colunms;i++) { column = table.getColumnModel().getColumn(i); column.setPreferredWidth(100); //将每一列的默认宽度设置为100 } table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //设置JTable自动调整列表的状态,此处设置为关闭 JScrollPane scroll = new JScrollPane(table); //用JScrollPane装载JTable,这样超出范围的列就可以通过滚动条来查看 scroll.setSize(300, 50); add(scroll); this.setLocation(450, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); } public static void main(String[] args) { new XueShengMingDanUI(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)