武汉理工大学面向对象与多线程综合实验——GUI设计版本

第二次验收:面向对象与多线程综合实验之GUI设计版本

面向对象与多线程综合实验是一次大项目,总共分为4个部分进行验收,我将分成四个部分展示4个版本的项目工程。希望看到本文章的你,对你有所收获。


档案管理系统简介

档案管理系统:用于规范档案文件管理,构建档案资源信息共享服务平台,为用户提供完整的档案管理和网络查询功能。
• 系统是一个基于C/S的GUI应用程序
• 使用Java SE开发
• 综合运用面向对象编程的相关知识

系统环境

 系统开发环境:JavaSE-12
 集成开发工具:Eclipse Java 2019-06
 GUI开发插件:Window Builder

系统功能

在这里插入图片描述

Swing GUI编程步骤

 创建应用程序框架,选定顶层容器
 在顶层容器中创建中间容器,将组件加入中间容器;或者将组件直接加入顶层容器中,设定控件属性
 选择布局管理器,进行合理布局
 实现事件处理程序,与用户交互


以下是本篇文章正文内容,下面案例可供参考

具体实现

Msystem包

Administrator.java

Browser.java

DataProcessing.java

Doc.java

Main.java

Operator.java

User.java

以上代码同第一个版本类似,可参考我的上一篇文章:
面向对象与多线程综合实验——控制台输出版本

Msystem.GUI包

Filemanagement.java

代码如下(示例):

扫描二维码关注公众号,回复: 12373257 查看本文章
package MSystem.GUI;

import java.awt.EventQueue;
import java.awt.FileDialog;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import MSystem.DataProcessing;
import MSystem.Doc;

import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.awt.event.ActionEvent;

public class Filemanagement {
    
    

	public static JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTable table;
	public static int d;
	String uploadpath="..\\ManagementSystem-v2\\uploadfile\\";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
    
    
		EventQueue.invokeLater(new Runnable() {
    
    
			public void run() {
    
    
				try {
    
    
					Filemanagement window = new Filemanagement();
					window.Fileframe(d);
					Filemanagement.frame.setVisible(true);
				} catch (Exception e) {
    
    
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @return 
	 */
	public void Fileframe(int d) {
    
    
		initialize(d);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize(int a) {
    
    
		frame = new JFrame();
		frame.setTitle("\u6587\u4EF6\u7BA1\u7406\u754C\u9762");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setVisible(true);
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setToolTipText("");
		tabbedPane.setBounds(10, 10, 416, 243);
		frame.getContentPane().add(tabbedPane);
		String[] columnName={
    
    "档案号","作者","时间","文件名","描述"};
		String[][] rowData=new String[50][5];
		Enumeration<Doc> e=null;
		try {
    
    
			e = DataProcessing.getAllDocs();
		} 
		catch (SQLException e1) {
    
    
			e1.printStackTrace();;
		}
		Doc doc;
		String[] nameData=new String[50];
		int i=0;
		while(e.hasMoreElements()) {
    
    
			doc=e.nextElement();
			nameData[i]=rowData[i][0]=doc.getID();
			rowData[i][1]=doc.getCreator();
			rowData[i][2]=doc.getTimestamp().toString();
			rowData[i][3]=doc.getFilename();
			rowData[i][4]=doc.getDescription();
			i++;
		}
		String name=MainGUI.name;
		JPanel panel = new JPanel();
		tabbedPane.addTab("文件上传", null, panel, null);
		panel.setLayout(null);
		try {
    
    
			if(DataProcessing.searchUser(name).getRole().equals("operator")) {
    
    
			    tabbedPane.setEnabledAt(0, true);
			}
			else tabbedPane.setEnabledAt(0, false);
		} catch (SQLException e1) {
    
    
			e1.printStackTrace();
		}
		JLabel label = new JLabel("\u6863\u6848\u53F7");
		label.setBounds(44, 10, 58, 15);
		panel.add(label);
		
		JLabel label_1 = new JLabel("\u6863\u6848\u63CF\u8FF0");
		label_1.setBounds(44, 51, 58, 15);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel("\u6863\u6848\u540D");
		label_2.setBounds(44, 126, 58, 15);
		panel.add(label_2);
		
		textField = new JTextField();
		textField.setBounds(112, 7, 184, 21);
		panel.add(textField);
		textField.setColumns(10);
		
		JTextArea textArea = new JTextArea();
		textArea.setBounds(112, 46, 184, 69);
		panel.add(textArea);
		
		textField_1 = new JTextField();
		textField_1.setBounds(112, 123, 184, 21);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton = new JButton("\u6253\u5F00");
		btnNewButton.addActionListener(new ActionListener() {
    
    
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
    
    
				FileDialog fd1=new FileDialog(frame,"打开文件",FileDialog.LOAD);
				fd1.show();
    			textField_1.setText(fd1.getDirectory()+fd1.getFile());
			}
		});
		btnNewButton.setBounds(306, 122, 78, 23);
		panel.add(btnNewButton);
		
		JButton button = new JButton("\u4E0A\u4F20");
		button.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				int op=JOptionPane.showConfirmDialog(null, "确认上传", "温馨提示", JOptionPane.YES_NO_OPTION);
				if(op==JOptionPane.YES_OPTION) {
    
    
				String ID=textField.getText();
    			String description=textArea.getText();
    			String filepath=textField_1.getText();
    			byte[] buffer=new byte[1024];
    	   	    Timestamp timestamp=new Timestamp(System.currentTimeMillis());
    	   	    File tempFile=new File(filepath.trim());
    	   	    String filename=tempFile.getName();
    	   	    try {
    
    
    				DataProcessing.insertDoc(ID,name,timestamp,description,filename);
						try {
    
    
							DataProcessing.xuliehua();
						} catch (IOException e1) {
    
    
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					
    			} 
    			catch (SQLException e1) {
    
    
    				e1.printStackTrace();
    			}
    	   	    BufferedInputStream infile = null;
    		    try {
    
    
					infile = new BufferedInputStream(new FileInputStream(filepath));
				} catch (FileNotFoundException e1) {
    
    
					e1.printStackTrace();
				}
    	   	    BufferedOutputStream targetfile = null;
    			try {
    
    
					targetfile = new BufferedOutputStream(new FileOutputStream(new File(uploadpath+filename)));
				} catch (FileNotFoundException e1) {
    
    
					e1.printStackTrace();
				}
    	   	    while(true) {
    
    
    	   		    int byteRead = 0;
    			    try {
    
    
						byteRead = infile.read(buffer);
					} catch (IOException e1) {
    
    
						e1.printStackTrace();
					}
    	   		    if(byteRead==-1)
    	   		        break;
    			    try {
    
    
						targetfile.write(buffer,0,byteRead);
					} catch (IOException e1) {
    
    
						e1.printStackTrace();
					}
    	   	    }
    			try {
    
    
					infile.close();
				} catch (IOException e1) {
    
    
					e1.printStackTrace();
				}
    			try {
    
    
					targetfile.close();
				} catch (IOException e1) {
    
    
					e1.printStackTrace();
				}
    			JOptionPane.showMessageDialog(null, "上传成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
    			frame.dispose();
			}
			}
		});
		button.setBounds(72, 170, 97, 23);
		panel.add(button);
		
		JButton button_1 = new JButton("\u53D6\u6D88");
		button_1.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				frame.dispose();
			}
		});
		button_1.setBounds(199, 170, 97, 23);
		panel.add(button_1);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("文件下载", null, panel_1, null);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 391, 120);
		panel_1.add(scrollPane);
		
		table = new JTable(rowData,columnName);
		scrollPane.setViewportView(table);
		
		JButton button_2 = new JButton("\u4E0B\u8F7D");
		button_2.addActionListener(new ActionListener() {
    
    
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
    
    
				int op=JOptionPane.showConfirmDialog(null, "确认下载", "温馨提示", JOptionPane.YES_NO_OPTION);
		    	if(op==JOptionPane.YES_OPTION) {
    
    
			    	if(table.getSelectedRow()<0); 
			    	else {
    
    
			    		FileDialog fd2=new FileDialog(frame,"下载文件",FileDialog.SAVE);
						fd2.show();
						String filepath=fd2.getDirectory();
			    		String id=(String)table.getValueAt(table.getSelectedRow(), 0);
			    		byte[] buffer=new byte[1024];
			       	    Doc doc=null;
			   	    	 try {
    
    
			   	    		 doc = DataProcessing.searchDoc(id);
			   	    	 } 
			      		 catch (SQLException e1) {
    
    
			   	    		 e1.printStackTrace();
			      		 }
			        	 File tempFile=new File(uploadpath+doc.getFilename());
			        	 String filename=doc.getFilename();
			        	 BufferedInputStream infile = null;
						try {
    
    
							infile = new BufferedInputStream(new FileInputStream(tempFile));
						} catch (FileNotFoundException e1) {
    
    
							e1.printStackTrace();
						}
			         	 BufferedOutputStream targetfile = null;
						try {
    
    
							targetfile = new BufferedOutputStream(new FileOutputStream(new File(filepath+filename)));
						} catch (FileNotFoundException e1) {
    
    
							e1.printStackTrace();
						}
			        	 while(true) {
    
    
			        		 int byteRead = 0;
							try {
    
    
								byteRead = infile.read(buffer);
							} catch (IOException e1) {
    
    
								e1.printStackTrace();
							}
			        		 if(byteRead==-1)
			       	 		 break;
			       	    	 try {
    
    
								targetfile.write(buffer,0,byteRead);
							} catch (IOException e1) {
    
    
								e1.printStackTrace();
							}
			        	 }
			       	     try {
    
    
							infile.close();
						} catch (IOException e1) {
    
    
							e1.printStackTrace();
						}
			        	 try {
    
    
							targetfile.close();
						} catch (IOException e1) {
    
    
							e1.printStackTrace();
						}
			        	JOptionPane.showMessageDialog(null, "下载成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
			        	
			    	}
		    	}
			}
		});
		button_2.setBounds(84, 158, 97, 23);
		panel_1.add(button_2);
		
		JButton button_3 = new JButton("\u53D6\u6D88");
		button_3.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				frame.dispose();
			}
		});
		button_3.setBounds(201, 158, 97, 23);
		panel_1.add(button_3);
		switch(a) {
    
    
		case 0:
			tabbedPane.setSelectedIndex(0);break;
		case 1:
			tabbedPane.setSelectedIndex(1);break;
			default:break;
		}
	}
}

MainGUI.java

package MSystem.GUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import javax.swing.JTextField;

import MSystem.DataProcessing;
import MSystem.Doc;
import MSystem.User;

import javax.swing.JButton;
import java.sql.SQLException;
import java.util.Hashtable;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;

public class MainGUI {
    
    

	private JFrame frame;
	JTextField textField;
	private JPasswordField passwordField;
	public static String role;
	public static String name;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
    
    
		EventQueue.invokeLater(new Runnable() {
    
    
			public void run() {
    
    
				MainGUI window = new MainGUI();
					window.frame.setVisible(true);
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainGUI() {
    
    
		initialize();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
    
    
		frame = new JFrame();
		frame.setTitle("\u7CFB\u7EDF\u767B\u5F55");
		frame.setBounds(100, 100, 658, 388);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801");
		lblNewLabel_1.setBounds(103, 150, 76, 38);
		frame.getContentPane().add(lblNewLabel_1);
		
		JLabel label = new JLabel("\u7528\u6237\u540D");
		label.setBounds(103, 112, 58, 15);
		frame.getContentPane().add(label);
		
		textField = new JTextField();
		textField.setBounds(178, 109, 222, 21);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
	
		JButton btnNewButton = new JButton("\u786E\u5B9A");
		btnNewButton.addActionListener(new ActionListener() {
    
    
			@SuppressWarnings("unchecked")
			public void actionPerformed(ActionEvent e) {
    
    	
			//反序列化:
		FileInputStream fi = null;
		try {
    
    
			fi = new FileInputStream("..\\ManagementSystem-v2\\xuliehua\\user.bin\\");
		} catch (FileNotFoundException e2) {
    
    
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		ObjectInputStream si = null;
		try {
    
    
			si = new ObjectInputStream(fi);
		} catch (IOException e3) {
    
    
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		FileInputStream fi2 = null;
		try {
    
    
			fi2 = new FileInputStream("..\\ManagementSystem-v2\\xuliehua\\doc.bin\\");
		} catch (FileNotFoundException e2) {
    
    
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		ObjectInputStream si2 = null;
		try {
    
    
			si2 = new ObjectInputStream(fi2);
		} catch (IOException e2) {
    
    
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
			try
			{
    
    
				DataProcessing.users = (Hashtable<String, User>)si.readObject();
			} catch (ClassNotFoundException e1)
			{
    
    
				// TODO Auto-generated catch block
				System.out.println("反序列化错误"+e1);
			} catch (IOException e1) {
    
    
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try
			{
    
    
				DataProcessing.docs = (Hashtable<String, Doc>)si2.readObject();
			} catch (ClassNotFoundException e1)
			{
    
    
				// TODO Auto-generated catch block
				System.out.println("反序列化错误"+e1);
			} catch (IOException e1) {
    
    
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
    
    
				si.close();
			} catch (IOException e2) {
    
    
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			try {
    
    
				si2.close();
			} catch (IOException e2) {
    
    
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			// TODO Auto-generated catch block*/
			
				name=textField.getText();
				String password1=String.valueOf(passwordField.getPassword());
				try {
    
    
					User user = null;
					try {
    
    
						user = DataProcessing.search(name, password1);
						role=DataProcessing.getrole(name);
					if(user==null)
					JOptionPane.showMessageDialog(null, "密码错误\n请重新输入密码", "错误提示",JOptionPane.PLAIN_MESSAGE);
					else {
    
    MenuGUI.main(null);
					frame.dispose();}
					} catch (IOException e1) {
    
    
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				} catch (SQLException e1) {
    
    
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		btnNewButton.setBounds(126, 220, 103, 32);
		frame.getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
		btnNewButton_1.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				frame.dispose();
			}
		});
		btnNewButton_1.setBounds(255, 220, 103, 32);
		frame.getContentPane().add(btnNewButton_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(178, 159, 222, 21);
		frame.getContentPane().add(passwordField);
	}
}

MenuGUI.java

package MSystem.GUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MenuGUI {
    
    

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
    
    
		EventQueue.invokeLater(new Runnable() {
    
    
			public void run() {
    
    
				try {
    
    
					MenuGUI window = new MenuGUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
    
    
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MenuGUI() {
    
    
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
    
    
		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(100, 100, 714, 393);
		frame.dispose();
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("\u7528\u6237\u7BA1\u7406");
		menuBar.add(menu);
		
		JMenuItem menuItem = new JMenuItem("\u4FEE\u6539\u7528\u6237");
		menuItem.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				Usermanagement.select=0;
				Usermanagement.main(null);			
			}
		});
		menu.add(menuItem);
		
		JMenuItem menuItem_2 = new JMenuItem("\u5220\u9664\u7528\u6237");
		menuItem_2.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				Usermanagement.select=1;
					Usermanagement.main(null);
			}
		});
		menu.add(menuItem_2);
		
		JMenuItem menuItem_1 = new JMenuItem("\u65B0\u589E\u7528\u6237");
		menuItem_1.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				Usermanagement.select=2;
				Usermanagement.main(null);
			}
		});
		menu.add(menuItem_1);
		
		JMenu mnNewMenu = new JMenu("\u6863\u6848\u7BA1\u7406");
		menuBar.add(mnNewMenu);
		
		JMenuItem menuItem_3 = new JMenuItem("\u6863\u6848\u4E0A\u4F20");
		menuItem_3.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				Filemanagement.d=0;
				Filemanagement.main(null);
			}
		});
		mnNewMenu.add(menuItem_3);
		
		JMenuItem menuItem_4 = new JMenuItem("\u6863\u6848\u4E0B\u8F7D");
		menuItem_4.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				Filemanagement.d=1;
				Filemanagement.main(null);
			}
		});
		mnNewMenu.add(menuItem_4);
		
		JMenu mnNewMenu_1 = new JMenu("\u4E2A\u4EBA\u4FE1\u606F\u7BA1\u7406");
		menuBar.add(mnNewMenu_1);
		
		JMenuItem menuItem_5 = new JMenuItem("\u4FE1\u606F\u4FEE\u6539");
		menuItem_5.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				UserGUI.main(null);
			}
		});
		mnNewMenu_1.add(menuItem_5);
		String role=MainGUI.role;
		switch(role) {
    
    
		case "administrator":
			frame.setTitle("系统管理员界面");
			mnNewMenu.getItem(0).setEnabled(false);
			break;
		case "operator":
			frame.setTitle("档案录入员界面");
			menu.setEnabled(false);
			break;
		case "browser":
			frame.setTitle("档案浏览员界面");
			menu.setEnabled(false);
			mnNewMenu.getItem(0).setEnabled(false);
			break;
		default:
			break;
		}
	}
}

UserGUI.java

package MSystem.GUI;

import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import MSystem.DataProcessing;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import javax.swing.JPasswordField;

public class UserGUI extends JFrame{
    
    

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	private  JFrame frame;
	private  JTextField textField;
	private  JTextField textField_4;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	private JPasswordField passwordField_2;

	/**
	 * Launch the application.
	 * @wbp.parser.entryPoint
	 */
	public static void main(String[] args) {
    
    
		EventQueue.invokeLater(new Runnable() {
    
    
			public void run() {
    
    
				try {
    
    
					UserGUI window = new UserGUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
    
    
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public UserGUI() {
    
    
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	public  void initialize() {
    
    
		frame = new JFrame();
		frame.setTitle("\u4E2A\u4EBA\u4FE1\u606F\u7BA1\u7406");
		frame.setBounds(100, 100, 628, 376);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D");
		lblNewLabel.setBounds(95, 31, 91, 30);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u539F\u53E3\u4EE4");
		lblNewLabel_1.setBounds(95, 71, 91, 30);
		frame.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("\u65B0\u53E3\u4EE4");
		lblNewLabel_2.setBounds(95, 111, 91, 30);
		frame.getContentPane().add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("\u786E\u8BA4\u65B0\u53E3\u4EE4");
		lblNewLabel_3.setBounds(95, 151, 91, 30);
		frame.getContentPane().add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("\u89D2\u8272");
		lblNewLabel_4.setBounds(95, 191, 91, 30);
		frame.getContentPane().add(lblNewLabel_4);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(155, 36, 228, 25);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		textField_4 = new JTextField();
		textField_4.setEditable(false);
		textField_4.setBounds(155, 196, 228, 25);
		frame.getContentPane().add(textField_4);
		textField_4.setColumns(10);
		String name=MainGUI.name;
		String role=MainGUI.role;
		textField.setText(name);
		textField_4.setText(role);
		JButton btnNewButton = new JButton("\u4FEE\u6539");
		btnNewButton.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				String password=String.valueOf(passwordField.getPassword());
				String password_1=String.valueOf(passwordField_1.getPassword());
				String password_2=String.valueOf(passwordField_2.getPassword());
					try {
    
    
						try {
    
    
							if(DataProcessing.search(name, password)!=null) {
    
    
							if(!password_1.equals(password_2))
								JOptionPane.showMessageDialog(null, "两次密码输入错误\n请重新输入密码", "错误提示",JOptionPane.PLAIN_MESSAGE);
							else {
    
    
								DataProcessing.updateUser(name, password_1, role);
								DataProcessing.xuliehua();
									frame.dispose();
								JOptionPane.showMessageDialog(null, "修改成功", "温馨提示",JOptionPane.PLAIN_MESSAGE);
							}
							}
							else 
								JOptionPane.showMessageDialog(null, "密码错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
						} catch (IOException e1) {
    
    
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					} catch (HeadlessException e1) {
    
    
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
    
    
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} 
				}
			
		});
		btnNewButton.setBounds(113, 250, 103, 35);
		frame.getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u8FD4\u56DE");
		btnNewButton_1.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				frame.dispose();
			}
		});
		
		btnNewButton_1.setBounds(239, 250, 103, 35);
		frame.getContentPane().add(btnNewButton_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(155, 76, 229, 25);
		frame.getContentPane().add(passwordField);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(155, 116, 228, 25);
		frame.getContentPane().add(passwordField_1);
		
		passwordField_2 = new JPasswordField();
		passwordField_2.setBounds(155, 156, 228, 25);
		frame.getContentPane().add(passwordField_2);
	}
}

Usermanagement.java

package MSystem.GUI;

import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.JTextField;

import MSystem.DataProcessing;
import MSystem.User;

import javax.swing.JTable;
import javax.swing.JScrollPane;

public class Usermanagement {
    
    

	JFrame frame;
	public static int select;
	private JPasswordField passwordField;
	private JTable table;
	private JTextField textField;
	private JPasswordField passwordField_1;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
    
    
		EventQueue.invokeLater(new Runnable() {
    
    
			public void run() {
    
    
				try {
    
    
					Usermanagement window = new Usermanagement();
					window.Userframe(select);
					window.frame.setVisible(true);
				} catch (Exception e) {
    
    
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	
	
	public void Userframe(int select) {
    
    
		initialize(select);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize(int j) {
    
    
		frame = new JFrame();
		frame.setTitle("\u7528\u6237\u7BA1\u7406\u754C\u9762");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 10, 416, 243);
		frame.getContentPane().add(tabbedPane);
			String[] columnName={
    
    "用户名","密码","角色"};
		String[][] rowData=new String[50][3];
		Enumeration<User> e=null;
		try {
    
    
			e = DataProcessing.getAllUser();
		} 
		catch (SQLException e1) {
    
    
			System.out.println(e1.getMessage());
		}
		User user;
		String[] nameData=new String[50];
		int i=0;
		while(e.hasMoreElements()) {
    
    
			user=e.nextElement();
			nameData[i]=rowData[i][0]=user.getName();
			rowData[i][1]=user.getPassword();
			rowData[i][2]=user.getRole();
			i++;
		}
		JPanel panel = new JPanel();
		tabbedPane.addTab("修改用户", null, panel, null);
		panel.setLayout(null);
		
		JLabel label = new JLabel("\u7528\u6237\u540D");
		label.setBounds(48, 10, 58, 15);
		panel.add(label);
		
		JLabel label_1 = new JLabel("\u53E3\u4EE4");
		label_1.setBounds(48, 61, 58, 15);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel("\u89D2\u8272");
		label_2.setBounds(48, 110, 58, 15);
		panel.add(label_2);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(116, 58, 181, 21);
		panel.add(passwordField);
		String role[]= {
    
    "administrator","operator","browser"};
		JComboBox<?> comboBox = new JComboBox<Object>(role);
		comboBox.setBounds(116, 106, 181, 23);
		panel.add(comboBox);
		
		JComboBox<?> comboBox_1 = new JComboBox<Object>(nameData);
		comboBox_1.setBounds(116, 10, 181, 23);
		panel.add(comboBox_1);
		
		JButton button = new JButton("\u4FEE\u6539");
		button.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				String name=(String)comboBox_1.getSelectedItem();
				String password=String.valueOf(passwordField.getPassword());
				try {
    
    
					if(DataProcessing.searchUser(name)!=null) {
    
    
					String role=(String)comboBox.getSelectedItem();
					DataProcessing.updateUser(name, password, role);
					try {
    
    
						DataProcessing.xuliehua();
					} catch (IOException e1) {
    
    
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, "修改成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
					}
				} catch (SQLException e1) {
    
    
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				frame.dispose();
			}
		});
		button.setBounds(70, 152, 97, 23);
		panel.add(button);
		
		JButton button_1 = new JButton("\u53D6\u6D88");
		button_1.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				frame.dispose();
			}
		});
		button_1.setBounds(177, 152, 97, 23);
		panel.add(button_1);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("删除用户", null, panel_1, null);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 391, 118);
		panel_1.add(scrollPane);
	
		table = new JTable(rowData,columnName);
		scrollPane.setViewportView(table);
		
		JButton button_2 = new JButton("\u5220\u9664");
		button_2.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				if(table.getSelectedRow()<0) ; 
				else {
    
    
					String name_del=(String) table.getValueAt(table.getSelectedRow(), 0);
						try {
    
    
							if(DataProcessing.deleteUser(name_del)) {
    
    
								try {
    
    
									DataProcessing.xuliehua();
								} catch (IOException e1) {
    
    
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								JOptionPane.showMessageDialog(null, "删除成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);}
							else 
								JOptionPane.showMessageDialog(null, "账号不存在", "温馨提示", JOptionPane.ERROR_MESSAGE);
						} catch (HeadlessException e1) {
    
    
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SQLException e1) {
    
    
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						frame.dispose();
					
			}
			}
		});
		button_2.setBounds(75, 157, 97, 23);
		panel_1.add(button_2);
		
		JButton button_3 = new JButton("\u53D6\u6D88");
		button_3.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				frame.dispose();
			}
		});
		button_3.setBounds(203, 157, 97, 23);
		panel_1.add(button_3);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("新增用户", null, panel_2, null);
		panel_2.setLayout(null);
		
		JLabel label_3 = new JLabel("\u7528\u6237\u540D");
		label_3.setBounds(58, 10, 58, 15);
		panel_2.add(label_3);
		
		JLabel label_4 = new JLabel("\u53E3\u4EE4");
		label_4.setBounds(58, 53, 58, 15);
		panel_2.add(label_4);
		
		JLabel label_5 = new JLabel("\u89D2\u8272");
		label_5.setBounds(58, 105, 58, 15);
		panel_2.add(label_5);
		
		textField = new JTextField();
		textField.setBounds(126, 7, 178, 21);
		panel_2.add(textField);
		textField.setColumns(10);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(126, 50, 178, 21);
		panel_2.add(passwordField_1);
		
		JComboBox<?> comboBox_2 = new JComboBox<Object>(role);
		comboBox_2.setBounds(126, 101, 178, 23);
		panel_2.add(comboBox_2);
		
		JButton button_4 = new JButton("\u589E\u52A0");
		button_4.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				String name=textField.getText();
				String password=String.valueOf(passwordField_1.getPassword());
				String role=(String)comboBox_2.getSelectedItem();
				try {
    
    
					DataProcessing.insertUser(name, password, role);
					try {
    
    
						DataProcessing.xuliehua();
					} catch (IOException e1) {
    
    
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, "添加成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
					frame.dispose();
				} catch (SQLException e1) {
    
    
					// TODO Auto-generated catch block
					e1.printStackTrace();
					
				}
			
			}
		});
		button_4.setBounds(69, 155, 97, 23);
		panel_2.add(button_4);
		
		JButton button_5 = new JButton("\u53D6\u6D88");
		button_5.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				frame.dispose();
			}
		});
		button_5.setBounds(190, 155, 97, 23);
		panel_2.add(button_5);
		switch(j) {
    
    
		case 0:
			tabbedPane.setSelectedIndex(0);
			break;
		case 1:
			tabbedPane.setSelectedIndex(1);
			break;
		case 2:
			tabbedPane.setSelectedIndex(2);
			break;
			default:break;
		}
		}
	}



总结

GUI设计并没有改变程序的整体结构,只是多了个图形交互界面,为用户提供界面友好的操作环境。

猜你喜欢

转载自blog.csdn.net/mo_zhe/article/details/112559028