您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Java GUI如何制作管理系統”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“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(); } }
以上是“Java GUI如何制作管理系統”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。