⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 receiveview.java

📁 基于UDP的可靠邮件系统
💻 JAVA
字号:
package org.hwmhere.email.view;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collection;

import org.hwmhere.util.swing.Console;
import org.hwmhere.email.client.*;
import org.hwmhere.email.impl.Email;
import org.hwmhere.email.impl.util.EmailUtil;
import org.hwmhere.email.mailet.MailAddress;

public class ReceiveView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextField addressField = new JTextField(50);

	private JTextField passwordField = new JTextField(50);

	private JLabel addressLable = new JLabel("Your mail address:");

	private JLabel passwordLable = new JLabel("Your mail password:");

	private JButton getButton = new JButton("Get Emails");

	private DefaultListModel lItems = new DefaultListModel();

	private JList lst = new JList(lItems);
	
	private ArrayList emailList = new ArrayList();

	private MailAddress user;

	String receivePath = "D:/test/receive/";

	public ReceiveView() {

		this.setTitle("EmailBetterByRUDP_By_1040310515_HWM");
		
		giveLayout();

		giveAction();

		try {

			receivePath = "D:/test/receive/";
			user = new MailAddress("hwmhere@localhost");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void giveLayout() {
		Container cp = this.getContentPane();
		cp.setLayout(null);
		cp.add(addressLable);
		cp.add(passwordLable);
		cp.add(addressField);
		cp.add(passwordField);
		cp.add(getButton);
		cp.add(lst);

		// x,y,width,hight
		addressLable.setBounds(0, 4, 200, 30);
		passwordLable.setBounds(0, 40, 200, 30);
		addressField.setBounds(220, 4, 200, 30);
		passwordField.setBounds(220, 40, 200, 30);
		getButton.setBounds(150, 100, 100, 30);
		lst.setBounds(20, 200, 800, 500);

	}

	private void giveAction() {

		getButton.addActionListener(bl);
		lst.addListSelectionListener(ll);
	}

	private ActionListener bl = new ActionListener() {
		public void actionPerformed(ActionEvent e) {

			try {

				user = new MailAddress(addressField.getText());

				String userPath = receivePath + user.toString() + "/";

				FilenameFilter mailFileFilter = new FilenameFilter() {
					public boolean accept(File file, String name) {
						if (name.endsWith(".email")) {
							return true;
						} else {
							return false;
						}
					}
				};

				File mailsDir = new File(userPath);
				// 先将list清空
				lItems.clear();
				emailList.clear();

				File[] files = mailsDir.listFiles(mailFileFilter);
				for (int x = 0; x < files.length; x++) {
					File aFile = files[x];
					String filename = userPath + aFile.getName();

					// 添加到列表当中
					
					
					
					Email tempMail = EmailUtil.getEmail(filename);
					
					emailList.add(tempMail);
					lItems.addElement("Title"+
							tempMail.getAttribute("title")+"        \tFrom:"+tempMail.getSender()+"        Date:"+
							tempMail.getLastUpdated()
							);
			}

			} catch (Exception e1) {
				// TODO Auto-generated catch block
				JOptionPane.showMessageDialog(null, "Error", "Hey!",
						JOptionPane.ERROR_MESSAGE);
			}

		}
	};

	private ListSelectionListener ll = new ListSelectionListener() {
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting())
				return;

			int index = lst.getMinSelectionIndex();
			Email tempEmail = (Email)emailList.get(index);
			String message = new String(tempEmail.getMessage());
			String info = "Titile : "+tempEmail.getAttribute("title") +"\nDate : "+tempEmail.getLastUpdated() + "\n" +
			"--------------------------------------------------------------------------------\n";
			
			String title = "From :" + tempEmail.getSender().toString() + "  At :" + tempEmail.getLastUpdated();
			JOptionPane.showMessageDialog(null,info + message,title,
					JOptionPane.INFORMATION_MESSAGE);
			
			// TODO:
		}
	};

	public static void main(String[] args) {
		Console.run(new ReceiveView(), 1000, 750);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -