Java面向对象与多线程综合实验(四)之GUI设计

了解Java图形界面程序的基本结构;掌握Java布局管理和常用组件的使用;掌握Java事件处理机制。

实验内容
编写程序,将前面课程所编写的档案管理系统改编为图形用户界面。要求程序界面选用合适的布局,综合使用菜单、按钮、文本框、密码框、下拉列表、文件对话框等组件,实现良好的人机接口。

本次代码沿用第三次中的User.java Administrator.java Operator.java Browser.java DataProcessing.java和Doc.java
所以,在此仅对新的文件进行整理和发布。

  • WinMain.java

需要运行的主程序

public class WinMain {
	public static void main(String args[]) {
		LoginWindow window=new LoginWindow();
		window.loginFrame();
	}
}

  • LoginWindow.java

登录窗口界面

import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class LoginWindow extends JFrame{
	JTabbedPane tabbedPane;
	public void loginFrame() {
		JFrame frame=new JFrame();
		frame.setTitle("系统登录");
		frame.setSize(380,300);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setLayout(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JLabel L1=new JLabel("账号:");
		JTextField te1=new JTextField(25);
		JLabel L2=new JLabel("密码:");
		JPasswordField te2=new JPasswordField(25);
		te2.setEchoChar('*');
		L1.setFont(new Font("黑体",Font.PLAIN,18));
		L1.setSize(50,30);
		L1.setBounds(5+30,50,50,30);
		te1.setFont(new Font("黑体",Font.PLAIN,18));
		te1.setSize(250,30);
		te1.setBounds(5+80,50,250,30);
		L2.setFont(new Font("黑体",Font.PLAIN,18));
		L2.setSize(50,30);
		L2.setBounds(5+30,100,50,30);
		te2.setFont(new Font("黑体",Font.PLAIN,18));
		te2.setSize(250,30);
		te2.setBounds(5+80,100,250,30);
		frame.add(L1);
		frame.add(te1);
		frame.add(L2);
		frame.add(te2);
		JButton B1=new JButton("登录");
		JButton B2=new JButton("退出");
		B1.setSize(100,50);
		B1.setBounds((380-100-5)/2-60,(300-50-30)/2+50,100,50);
		B2.setSize(100,50);
		B2.setBounds((380-100-5)/2+60,(300-50-30)/2+50,100,50);
		frame.add(B1);
		frame.add(B2);
		B1.addActionListener(new ButtonHandler(frame,te1,te2));
		B2.addActionListener(new ButtonHandler(frame));
		frame.setVisible(true);
	}
	public class ButtonHandler implements ActionListener{
		public JTextField te1=new JTextField();
	    public JPasswordField te2=new JPasswordField();
	    public JFrame frame=new JFrame();
	    ButtonHandler(JFrame frame){
	    	this.frame=frame;
	    }
	    ButtonHandler(JFrame frame,JTextField te1,JPasswordField te2) {
	    	this.frame=frame;
			this.te1=te1;
			this.te2=te2;
		}
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand()=="登录") {
				String name=te1.getText();
				String password=String.valueOf(te2.getPassword());
				try {
					if(DataProcessing.searchUser(name)!=null) {
						if(DataProcessing.search(name ,password)!=null) {
							frame.dispose();
							MenuWindow menuWindow=new MenuWindow();
							menuWindow.showMenu(name);
							addWindowListener(new WindowAdapter() {
								@SuppressWarnings("unused")
								public void WindowClosing(WindowEvent e2) {
									DataProcessing.disconnectFromDatabase();
								}
							});
						}
						else {
							JOptionPane.showMessageDialog(null, "密码错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
						}
					}
					else {
						JOptionPane.showMessageDialog(null, "账号不存在", "温馨提示", JOptionPane.ERROR_MESSAGE);
					}
				} catch (HeadlessException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			else {
				frame.dispose();
			}	
		}
	}
}


  • MenuWindow.java

菜单界面

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MenuWindow {
    public void showMenu(String name) {
    	JFrame frame=new JFrame();
		frame.setTitle("菜单界面");
		frame.setSize(1600,1200);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setLayout(null);
		JMenuBar menu=new JMenuBar();
		JMenu menu1=new JMenu("用户管理");
		JMenu menu2=new JMenu("文件管理");
		JMenu menu3=new JMenu("密码管理");
		JMenuItem item1=new JMenuItem("添加用户");
		JMenuItem item2=new JMenuItem("修改用户");
		JMenuItem item3=new JMenuItem("删除用户");
		JMenuItem item4=new JMenuItem("上传文件");
		JMenuItem item5=new JMenuItem("下载文件");
		JMenuItem item6=new JMenuItem("修改密码");
		String role = null;
		try {
			role=DataProcessing.searchUser(name).getRole();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(role.equals("administrator")) {
			menu1.setEnabled(true);
			menu2.setEnabled(true);
			menu3.setEnabled(true);
			item1.setEnabled(true);
			item2.setEnabled(true);
			item3.setEnabled(true);
			item4.setEnabled(false);
			item5.setEnabled(true);
			item6.setEnabled(true);
		}
		else if(role.equals("operator")){
			menu1.setEnabled(false);
			menu2.setEnabled(true);
			menu3.setEnabled(true);
			item1.setEnabled(false);
			item2.setEnabled(false);
			item3.setEnabled(false);
			item4.setEnabled(true);
			item5.setEnabled(true);
			item6.setEnabled(true);
		}
		else if(role.equals("browser")){
			menu1.setEnabled(false);
			menu2.setEnabled(true);
			menu3.setEnabled(true);
			item1.setEnabled(false);
			item2.setEnabled(false);
			item3.setEnabled(false);
			item4.setEnabled(false);
			item5.setEnabled(true);
			item6.setEnabled(true);
		}
		menu1.add(item1);
		menu1.add(item2);
		menu1.add(item3);
		menu2.add(item4);
		menu2.add(item5);
		menu3.add(item6);
		menu.add(menu1);
		menu.add(menu2);
		menu.add(menu3);
		menu.setSize(195,30);
		menu.setBounds(0,0,195,30);
		menu.setVisible(true);
		frame.add(menu);
		item1.addActionListener(new MenuAction(name));
		item2.addActionListener(new MenuAction(name));
		item3.addActionListener(new MenuAction(name));
		item4.addActionListener(new MenuAction(name));
		item5.addActionListener(new MenuAction(name));
		item6.addActionListener(new MenuAction(name));
		frame.setVisible(true);
    }
    class MenuAction implements ActionListener{
    	String name;
    	MenuAction(String name){
    		this.name=name;
    	}
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand()=="添加用户") {
				UserWindow userWindow=new UserWindow();
				userWindow.showMenu(name,0);
			}
			else if(e.getActionCommand()=="修改用户") {
				UserWindow userWindow=new UserWindow();
				userWindow.showMenu(name,1);
			}
			else if(e.getActionCommand()=="删除用户") {
				UserWindow userWindow=new UserWindow();
				userWindow.showMenu(name,2);
			}
			else if(e.getActionCommand()=="上传文件") {
				UpDownloadWindow updownloadWindow=new UpDownloadWindow();
				updownloadWindow.showMenu(name,1);
			}
			else if(e.getActionCommand()=="下载文件") {
				UpDownloadWindow updownloadWindow=new UpDownloadWindow();
				updownloadWindow.showMenu(name,0);
			}
			else if(e.getActionCommand()=="修改密码") {
				PasswordWindow passwordWindow=new PasswordWindow();
				passwordWindow.showMenu(name);
			}
		}
    }
}

  • UserWindow.java

用户更改界面

import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Enumeration;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class UserWindow{
	String admin_name;
	public void showMenu(String name,int index) {
		admin_name=name;
		JFrame frame=new JFrame();
		frame.setTitle("用户管理界面");
		frame.setSize(380,300);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		JPanel panel_add=new JPanel();
		panel_add.setLayout(null);
		tabbedPane.addTab("添加用户", panel_add);
		JLabel L1=new JLabel("账号:");
		JTextField te1=new JTextField(25);
		JLabel L2=new JLabel("密码:");
		JPasswordField te2=new JPasswordField(25);
		JLabel L3=new JLabel("角色:");
		String role[]= {"administrator","operator","browser"};
		JComboBox box=new JComboBox(role);
		te2.setEchoChar('*');
		L1.setFont(new Font("黑体",Font.PLAIN,18));
		L1.setSize(50,30);
		L1.setBounds(5+30,30,50,30);
		te1.setFont(new Font("黑体",Font.PLAIN,18));
		te1.setSize(250,30);
		te1.setBounds(5+80,30,250,30);
		L2.setFont(new Font("黑体",Font.PLAIN,18));
		L2.setSize(50,30);
		L2.setBounds(5+30,80,50,30);
		te2.setFont(new Font("黑体",Font.PLAIN,18));
		te2.setSize(250,30);
		te2.setBounds(5+80,80,250,30);
		L3.setFont(new Font("黑体",Font.PLAIN,18));
		L3.setSize(50,30);
		L3.setBounds(5+30,130,50,30);
		box.setFont(new Font("黑体",Font.PLAIN,18));
		box.setSize(200,30);
		box.setBounds(5+80,130,200,30);
		panel_add.add(L1);
		panel_add.add(te1);
		panel_add.add(L2);
		panel_add.add(te2);
		panel_add.add(L3);
		panel_add.add(box);
		JButton B1=new JButton("添加");
		JButton B2=new JButton("退出");
		B1.setSize(80,40);
		B1.setBounds((380-80-5)/2-60,(300-40-30)/2+50,80,40);
		B2.setSize(80,40);
		B2.setBounds((380-80-5)/2+60,(300-40-30)/2+50,80,40);
		panel_add.add(B1);
		panel_add.add(B2);
		B1.addActionListener(new ButtonHandler(frame,te1,te2,box));
		B2.addActionListener(new ButtonHandler(frame));
	
		String[] columnName={"用户名","密码","角色"};
		String[][] rowData=new String[50][3];
		Enumeration e=null;
		try {
			e = DataProcessing.getAllUser();
		} 
		catch (SQLException e1) {
			e1.printStackTrace();;
		}
		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_mod=new JPanel();
		panel_mod.setLayout(null);
		tabbedPane.addTab("修改用户", panel_mod);
		JLabel L4=new JLabel("账号:");
		JComboBox box_name=new JComboBox(nameData);
		JLabel L5=new JLabel("密码:");
		JPasswordField te4=new JPasswordField(25);
		JLabel L6=new JLabel("角色:");
		JComboBox box2=new JComboBox(role);
		te4.setEchoChar('*');
		L4.setFont(new Font("黑体",Font.PLAIN,18));
		L4.setSize(50,30);
		L4.setBounds(5+30,30,50,30);
		box_name.setFont(new Font("黑体",Font.PLAIN,18));
		box_name.setSize(250,30);
		box_name.setBounds(5+80,30,250,30);
		L5.setFont(new Font("黑体",Font.PLAIN,18));
		L5.setSize(50,30);
		L5.setBounds(5+30,80,50,30);
		te4.setFont(new Font("黑体",Font.PLAIN,18));
		te4.setSize(250,30);
		te4.setBounds(5+80,80,250,30);
		L6.setFont(new Font("黑体",Font.PLAIN,18));
		L6.setSize(50,30);
		L6.setBounds(5+30,130,50,30);
		box2.setFont(new Font("黑体",Font.PLAIN,18));
		box2.setSize(200,30);
		box2.setBounds(5+80,130,200,30);
		panel_mod.add(L4);
		panel_mod.add(box_name);
		panel_mod.add(L5);
		panel_mod.add(te4);
		panel_mod.add(L6);
		panel_mod.add(box2);
		JButton B3=new JButton("修改");
		JButton B4=new JButton("退出");
		B3.setSize(80,40);
		B3.setBounds((380-80-5)/2-60,(300-40-30)/2+50,80,40);
		B4.setSize(80,40);
		B4.setBounds((380-80-5)/2+60,(300-40-30)/2+50,80,40);
		panel_mod.add(B3);
		panel_mod.add(B4);
		B3.addActionListener(new ButtonHandler(frame,box_name,te4,box2));
		B4.addActionListener(new ButtonHandler(frame));
		
		JPanel panel_del=new JPanel();
		panel_del.setLayout(null);
		tabbedPane.addTab("删除用户", panel_del);
		@SuppressWarnings("serial")
		JTable table=new JTable(rowData,columnName) {
			public boolean isCellEditable(int rowIndex,int ColIndex) {
				return false;
			}
		};
		table.setFont(new Font("黑体",Font.PLAIN,18));
		table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scroll=new JScrollPane(table);
		scroll.setVisible(true);
		scroll.setSize(380,160);
		scroll.setBounds(0,0,380,160);
		panel_del.add(scroll);
		JButton B5=new JButton("删除");
		JButton B6=new JButton("退出");
		B5.setSize(80,40);
		B5.setBounds((380-80-5)/2-60,(300-40-30)/2+50,80,40);
		B6.setSize(80,40);
		B6.setBounds((380-80-5)/2+60,(300-40-30)/2+50,80,40);
		panel_del.add(B5);
		panel_del.add(B6);
		B5.addActionListener(new ButtonHandler(frame,table));
		B6.addActionListener(new ButtonHandler(frame));
		tabbedPane.setVisible(true);
		tabbedPane.setSelectedIndex(index);
		frame.add(tabbedPane);
		frame.setVisible(true);		
	}
	public class ButtonHandler implements ActionListener{
		public JTextField te1=new JTextField();
	    public JPasswordField te2=new JPasswordField();
	    public JFrame frame=new JFrame();
	    public JComboBox box=new JComboBox();
	    public JComboBox box_name=new JComboBox();
	    public JTable table;
	    public String role;
	    ButtonHandler(JFrame frame){
	    	this.frame=frame;
	    }
	    ButtonHandler(JFrame frame,JTextField te1,JPasswordField te2,JComboBox box){
	    	this.frame=frame;
			this.te1=te1;
			this.te2=te2;
			this.box=box;
	    }
	    ButtonHandler(JFrame frame,JComboBox box_name,JPasswordField te2,JComboBox box) {
	    	this.frame=frame;
			this.box_name=box_name;
			this.te2=te2;
			this.box=box;
		}
	    ButtonHandler(JFrame frame,JTable table) {
	    	this.frame=frame;
			this.table=table;
		}
		public void actionPerformed(ActionEvent e) {
			String name=te1.getText();
			String password=String.valueOf(te2.getPassword());
			if(e.getActionCommand().equals("修改")) {
				try {
					name=(String)box_name.getSelectedItem();
					if(DataProcessing.searchUser(name)!=null) {
						role=(String)box.getSelectedItem();
						DataProcessing.update(name, password, role);
						JOptionPane.showMessageDialog(null, "修改成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
						frame.dispose();
						UserWindow userWindow=new UserWindow();
						userWindow.showMenu(admin_name,1);
					}
					else {
						JOptionPane.showMessageDialog(null, "账号不存在", "温馨提示", JOptionPane.ERROR_MESSAGE);
					}
				} catch (HeadlessException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			else if(e.getActionCommand().equals("添加")){
				role=(String)box.getSelectedItem();
				try {
					DataProcessing.insert(name, password, role);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				JOptionPane.showMessageDialog(null, "添加成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
				frame.dispose();
				UserWindow userWindow=new UserWindow();
				userWindow.showMenu(admin_name,0);
			}	
			else if(e.getActionCommand().equals("删除")){
				if(table.getSelectedRow()<0) ; 
				else {
					String name_del=(String) table.getValueAt(table.getSelectedRow(), 0);
					if(name_del.equals(admin_name)) {
						JOptionPane.showMessageDialog(null, "无法删除", "温馨提示", JOptionPane.ERROR_MESSAGE);
					}
					else {
						try {
							if(DataProcessing.delete(name_del)) {
								JOptionPane.showMessageDialog(null, "删除成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
					
							}
							else {
								JOptionPane.showMessageDialog(null, "账号不存在", "温馨提示", JOptionPane.ERROR_MESSAGE);
							}
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
						frame.dispose();
						UserWindow userWindow=new UserWindow();
						userWindow.showMenu(admin_name,2);
					}
					
				}
			}	
			else if(e.getActionCommand().equals("退出")) {
				frame.dispose();
			}
		}
	}
}

  • UpDownloadWindow.java

上传下载界面

import java.awt.Font;
import java.awt.event.ActionEvent;
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 javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class UpDownloadWindow {
    String user_name;
    String uploadpath="C:\\uploadfile\\";
    String downloadpath="C:\\downloadfile\\";
    public void showMenu(String name,int index) {
        user_name=name;
    	JFrame frame=new JFrame();
        frame.setTitle("文件管理界面");
		frame.setSize(460,400);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		JPanel panel_download=new JPanel();
		panel_download.setLayout(null);
		tabbedPane.addTab("文件下载", panel_download);
		String[] columnName={"ID","Creator","Time","FileName","Description"};
		String[][] rowData=new String[50][5];
		Enumeration 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++;
		}
		@SuppressWarnings("serial")
		JTable table=new JTable(rowData,columnName) {
			public boolean isCellEditable(int rowIndex,int ColIndex) {
				return false;
			}
		};
		table.setFont(new Font("黑体",Font.PLAIN,18));
		table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scroll=new JScrollPane(table);
		scroll.setVisible(true);
		scroll.setSize(460,160);
		scroll.setBounds(0,0,460,160);
		//scroll.setHorizontalScrollBarPolicy(scroll.HORIZONTAL_SCROLLBAR_ALWAYS);
		panel_download.add(scroll);
		JButton B1=new JButton("下载");
		JButton B2=new JButton("退出");
		B1.setSize(80,40);
		B1.setBounds((460-80-5)/2-60,(400-40-30)/2+50,80,40);
		B2.setSize(80,40);
		B2.setBounds((460-80-5)/2+60,(400-40-30)/2+50,80,40);
		B1.addActionListener(new ButtonHandler(frame,table));
		B2.addActionListener(new ButtonHandler(frame));
		panel_download.add(B1);
		panel_download.add(B2);
		
		JPanel panel_upload=new JPanel();
		panel_upload.setLayout(null);
		tabbedPane.addTab("文件上传", panel_upload);
		JLabel L1=new JLabel("档案号:");
		JTextField te1=new JTextField(25);
		JLabel L2=new JLabel("档案描述:");
		JTextArea te2=new JTextArea(6,25);
		JLabel L3=new JLabel("档案文件名:");
		JTextField te3=new JTextField(25);
		L1.setFont(new Font("黑体",Font.PLAIN,18));
		L1.setSize(100,30);
		L1.setBounds(5,0,100,30);
		te1.setFont(new Font("黑体",Font.PLAIN,18));
		te1.setSize(250,30);
		te1.setBounds(5+100,0,250,30);
		L2.setFont(new Font("黑体",Font.PLAIN,18));
		L2.setSize(100,30);
		L2.setBounds(5,50,100,30);
		te2.setFont(new Font("黑体",Font.PLAIN,18));
		te2.setSize(250,150);
		te2.setBounds(5+100,50,250,150);
		L3.setFont(new Font("黑体",Font.PLAIN,18));
		L3.setSize(100,30);
		L3.setBounds(5,250,100,30);
		te3.setFont(new Font("黑体",Font.PLAIN,18));
		te3.setSize(250,30);
		te3.setBounds(5+100,250,250,30);
		panel_upload.add(L1);
		panel_upload.add(L2);
		panel_upload.add(L3);
		panel_upload.add(te1);
		panel_upload.add(te2);
		panel_upload.add(te3);
		JButton B3=new JButton("打开");
		JButton B4=new JButton("上传");
		JButton B5=new JButton("取消");
		B3.setSize(80,40);
		B3.setBounds(5+350,250,80,40);
		B4.setSize(80,40);
		B4.setBounds((460-80-5)/2-60,290,80,40);
		B5.setSize(80,40);
		B5.setBounds((460-80-5)/2+60,290,80,40);
		B3.addActionListener(new ButtonHandler(te3));
		B4.addActionListener(new ButtonHandler(frame,te1,te2,te3));
		B5.addActionListener(new ButtonHandler(frame));
		panel_upload.add(B3);
		panel_upload.add(B4);
		panel_upload.add(B5);
		tabbedPane.setVisible(true);
		tabbedPane.setSelectedIndex(index);
		try {
			if(DataProcessing.searchUser(user_name).getRole().equals("operator")) {
			    tabbedPane.setEnabledAt(1, true);
			}
			else tabbedPane.setEnabledAt(1, false);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		frame.add(tabbedPane);
        frame.setVisible(true);  
    }
    public class ButtonHandler implements ActionListener{
    	JTextField te1;
    	JTextArea te2;
    	JTextField te3;
    	JFrame frame;
		JTable table=new JTable();
    	ButtonHandler(JTextField te3){
    		this.te3=te3;
    	}
    	ButtonHandler(JFrame frame,JTextField te1,JTextArea te2,JTextField te3){
    		this.frame=frame;
    		this.te1=te1;
    		this.te2=te2;
    		this.te3=te3;
    	}
    	ButtonHandler(JFrame frame){
    		this.frame=frame;
    	}
		ButtonHandler(JFrame frame,JTable table) {
	    	this.frame=frame;
			this.table=table;
		}
    	public void actionPerformed(ActionEvent e) {
	    	JFileChooser fileChooser=new JFileChooser();
		    if(e.getActionCommand()=="下载") {
		    	int op=JOptionPane.showConfirmDialog(null, "确认下载", "温馨提示", JOptionPane.YES_NO_OPTION);
		    	if(op==JOptionPane.YES_OPTION) {
			    	if(table.getSelectedRow()<0) ; 
			    	else {
			    		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=tempFile.getName();
			        	 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(downloadpath+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);
			    	}
		    	}
		    }
		    else if(e.getActionCommand()=="退出") {
		    	frame.dispose();
		    }
		    else if(e.getActionCommand()=="打开") {
    			fileChooser.setFileSelectionMode(0);
    			int state=fileChooser.showOpenDialog(null);
    			if(state==1) {
    				return ;
    			}
    			else {
    				File file=fileChooser.getSelectedFile();
    				te3.setText(file.getAbsolutePath());
    			}
    		}
    		else if(e.getActionCommand()=="上传"){
    			int op=JOptionPane.showConfirmDialog(null, "确认上传", "温馨提示", JOptionPane.YES_NO_OPTION);
    			if(op==JOptionPane.YES_OPTION) {
    				String ID=te1.getText();
        			String description=te2.getText();
        			String filename=te3.getText();
        			byte[] buffer=new byte[1024];
        	   	    Timestamp timestamp=new Timestamp(System.currentTimeMillis());
        	   	    File tempFile=new File(filename.trim());
        	        String fileName=tempFile.getName();
        	   	    try {
        				if(DataProcessing.insertDoc(ID,user_name,timestamp,description,fileName)) ;
        				else JOptionPane.showMessageDialog(null, "存入数据库失败", "温馨提示", JOptionPane.ERROR_MESSAGE);
        			} 
        			catch (SQLException e1) {
        				e1.printStackTrace();
        			}
        	   	    BufferedInputStream infile = null;
        		    try {
    					infile = new BufferedInputStream(new FileInputStream(filename));
    				} 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();
    				UpDownloadWindow updownloadWindow=new UpDownloadWindow();
    				updownloadWindow.showMenu(user_name,1);
    			}
    			else if(op==JOptionPane.NO_OPTION) {
    				;
    			}
    		}
    		else if(e.getActionCommand()=="取消") {
    			frame.dispose();
    		}
    	}
    }
}

  • PasswordWindow.java

密码修改界面

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

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

public class PasswordWindow {
    String user_name;
    String user_role;
    public void showMenu(String name) {
        user_name=name;
        try {
			user_role=DataProcessing.searchUser(user_name).getRole();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
        JFrame frame=new JFrame();
		frame.setTitle("密码修改界面");
		frame.setSize(380,400);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setLayout(null);
		
		JLabel L1=new JLabel("账号:");
		JTextField te1=new JTextField(25);
		JLabel L2=new JLabel("旧密码:");
		JPasswordField te2=new JPasswordField(25);
		te2.setEchoChar('*');
		JLabel L3=new JLabel("新密码:");
		JPasswordField te3=new JPasswordField(25);
		te3.setEchoChar('*');
		JLabel L4=new JLabel("重复密码:");
		JPasswordField te4=new JPasswordField(25);
		te4.setEchoChar('*');
		JLabel L5=new JLabel("角色:");
		JTextField te5=new JTextField(25);
		L1.setFont(new Font("黑体",Font.PLAIN,18));
		L1.setSize(100,30);
		L1.setBounds(5,30,100,30);
		te1.setFont(new Font("黑体",Font.PLAIN,18));
		te1.setSize(250,30);
		te1.setBounds(5+100,30,250,30);
		L2.setFont(new Font("黑体",Font.PLAIN,18));
		L2.setSize(100,30);
		L2.setBounds(5,80,100,30);
		te2.setFont(new Font("黑体",Font.PLAIN,18));
		te2.setSize(250,30);
		te2.setBounds(5+100,80,250,30);
		L3.setFont(new Font("黑体",Font.PLAIN,18));
		L3.setSize(100,30);
		L3.setBounds(5,130,100,30);
		te3.setFont(new Font("黑体",Font.PLAIN,18));
		te3.setSize(250,30);
		te3.setBounds(5+100,130,250,30);
		L4.setFont(new Font("黑体",Font.PLAIN,18));
		L4.setSize(100,30);
		L4.setBounds(5,180,100,30);
		te4.setFont(new Font("黑体",Font.PLAIN,18));
		te4.setSize(250,30);
		te4.setBounds(5+100,180,250,30);
		L5.setFont(new Font("黑体",Font.PLAIN,18));
		L5.setSize(100,30);
		L5.setBounds(5,230,100,30);
		te5.setFont(new Font("黑体",Font.PLAIN,18));
		te5.setSize(250,30);
		te5.setBounds(5+100,230,250,30);
		frame.add(L1);
		frame.add(L2);
		frame.add(L3);
		frame.add(L4);
		frame.add(L5);
		frame.add(te1);
		frame.add(te2);
		frame.add(te3);
		frame.add(te4);
		frame.add(te5);
		te1.setText(user_name);
		te1.setEnabled(false);
		te5.setText(user_role);
		te5.setEnabled(false);
		JButton B1=new JButton("确认");
		JButton B2=new JButton("退出");
		B1.setSize(80,40);
		B1.setBounds((380-80-5)/2-60,300,80,40);
		B2.setSize(80,40);
		B2.setBounds((380-80-5)/2+60,300,80,40);
		B1.addActionListener(new ButtonHandler(frame,te2,te3,te4));
		frame.add(B1);
		frame.add(B2);
		frame.setVisible(true);
    }
    public class ButtonHandler implements ActionListener{
    	JFrame frame;
    	JTextField te2;
    	JTextField te3;
    	JTextField te4;
    	ButtonHandler(JFrame frame,JTextField te2,JTextField te3,JTextField te4){
    		this.frame=frame;
    		this.te2=te2;
    		this.te3=te3;
    		this.te4=te4;
    	}
		public void actionPerformed(ActionEvent e) {
			String old_password=te2.getText();
			String new_password=te3.getText();
			String new_password2=te4.getText();
			if(e.getActionCommand()=="确认") {
				try {
					if(DataProcessing.search(user_name, old_password)!=null) {
						if(new_password.equals(new_password2)) {
							DataProcessing.update(user_name, new_password, user_role);
							frame.dispose();
							JOptionPane.showMessageDialog(null, "修改成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
						}
						else {
							JOptionPane.showMessageDialog(null, "两次输入的新密码不一致", "温馨提示", JOptionPane.ERROR_MESSAGE);
						}
					}
					else {
						JOptionPane.showMessageDialog(null, "密码错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			else if(e.getActionCommand()=="退出"){
				frame.dispose();
			}
		}	
    }
}

以下是需要完成的结果展示:Java面向对象与多线程综合实验(四)之GUI设计

你可能感兴趣的:(Java面向对象与多线程综合实验(四)之GUI设计)