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

📄 newmail.java

📁 这是我做的一个收发邮件的软件
💻 JAVA
字号:
package source;

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.swtdesigner.SWTResourceManager;

public class NewMail {

	private Text text_3;
	private Text text_2;
	private Text text_1;
	private Text text;
	private String r_addr="";
	private String title="";
	private String fujian="";
	private String content="";
	private boolean check=false;
	protected   Shell   shell;   
	private   Shell   parent;   
	private   Display   display;  
	private long id=0;
	private int type=0;
	private String sql="";

	
	public NewMail(Shell parent,Display display,long id,int type) {
		  this.parent   =   parent;   
		  this.display   =   display;   
		  this.id=id;
		  this.type=type;
		  
		  this.open();   
	}
	public NewMail(Shell parent,Display display) {
		  type=0;
		  this.parent   =   parent;   
		  this.display   =   display;   
		  System.out.print(id);
		  this.open();   

	}
	public void open()
	{
		
		shell=new Shell();
		shell.setText("撰写新邮件");
		shell.setSize(770, 625);
	     int width,height;
	        width=770;
	        height=625;
	        shell.setSize(width, height);
	        shell.setCapture(true);
	        shell.setLayout(null);
	        Toolkit kit=Toolkit.getDefaultToolkit();
	        Dimension screenSize=kit.getScreenSize();
	        shell.setBounds((screenSize.width-width)/2, (screenSize.height-height)/2, width,height);
		
		if(id!=0&&type!=0)
		{

			if(type==1)
			{
				try {
					DataBase db=new DataBase();
					db.connectToDB();
					sql="select * from mail where id="+id;
					ResultSet rs=null;
					rs=db.executeQuery(sql);
					rs.next();
					r_addr=rs.getString("s_addr");
					title="回复:"+rs.getString("title");
					fujian="";
					db.close();
					
				} catch (SQLException e1) {
					// TODO 自动生成 catch 块
					e1.printStackTrace();
				}
			} 
			else
			{
				try {
					DataBase db=new DataBase();
					db.connectToDB();
					sql="select * from mail where id="+id;
					ResultSet rs=null;
					rs=db.executeQuery(sql);
					rs.next();
					r_addr="";
					title=rs.getString("title");
					fujian=rs.getString("fujian_addr").trim();
					content=rs.getString("content");
					db.close();
				} catch (SQLException e1) {
					// TODO 自动生成 catch 块
					e1.printStackTrace();
				}
			}	
			
		}


		final Label label = new Label(shell, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("", 14, SWT.BOLD));
		label.setText("收件人地址:");
		label.setBounds(20, 20, 114, 20);

		text = new Text(shell, SWT.BORDER);
		text.setBounds(185, 18, 490, 20);
		text.setText(r_addr);
		final Label label_1 = new Label(shell, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("", 14, SWT.BOLD));
		label_1.setText("主     题:");
		label_1.setBounds(20, 50, 114, 20);

		text_1 = new Text(shell, SWT.BORDER);
		text_1.setText(title);
		text_1.setBounds(185, 50, 490, 20);

		text_2 = new Text(shell, SWT.BORDER);
		fujian.trim();
		text_2.setText(fujian);
		text_2.setBounds(185, 83, 320, 20);
		final Label label_2 = new Label(shell, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("", 14, SWT.BOLD));
		label_2.setText("附     件:");
		label_2.setBounds(20, 80, 114, 20);

		final Button button = new Button(shell, SWT.NONE);
		button.setText("添加附件");
		button.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				String dir=DirectoryDialog1.getDirectory();
				text_2.setText(dir);
			}
		});
		button.setBounds(515, 80, 80, 22);

		text_3 = new Text(shell, SWT.MULTI | SWT.BORDER);
		text_3.setBounds(18, 127, 721, 394);
		text_3.setText(content);
		final Button button_4 = new Button(shell, SWT.CHECK);
		button_4.setText("是否存档");
		button_4.setBounds(610, 85, 93, 16);
		
		final Button button_1 = new Button(shell, SWT.NONE);
		button_1.setText("发送");
		button_1.addMouseListener(new MouseAdapter(){   
	      	  public   void   mouseUp(MouseEvent   e){   
	      	   r_addr=text.getText();
	      	   title=text_1.getText();
	      	   fujian=text_2.getText();
	      	   content=text_3.getText();
	      	  Send send=new Send(r_addr,title,content,fujian);
	      	  if(button_4.getSelection())
	      	  {
		      		DataBase db=new DataBase();
		      		db.connectToDB();
					ResultSet rs=null;
					String sql;
					sql="select top 1 * from config";
					String s_addr="";
					String s_name="";
					Date date=new Date();
					SimpleDateFormat sdf  =   new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); 
					String datestr=sdf.format(date);
					try {
					rs=db.executeQuery(sql);
					if(!rs.next())
					{
						MessageDialog.openError(null, "发邮件错误", "读取配置失败,请检查配置!");
						db.close();
						System.exit(0);
						rs.close();
						rs=null;
					}
	
						s_addr=rs.getString("s_addr");
						s_name=rs.getString("s_name");
						db.close();
						rs=null;
					} catch (SQLException e1) {
						// TODO 自动生成 catch 块
						e1.printStackTrace();
						db.close();
						rs=null;
					}
					db.connectToDB();
		      		 sql="insert into mail(title,content,r_addr,fujian_addr,flag,read,s_addr,s_time,r_time,s_name) " +
					 " values('"+title+"','"+content+"','"+r_addr+"','"+fujian+"',1,true,'"+s_addr+"','"+datestr+"','"+datestr+"','"+s_name+"')";
		      		 db.executeInsert(sql);
		      		 try {
						db.conn.close();
						db.stmt.close();
					} catch (SQLException e1) {
						// TODO 自动生成 catch 块
						e1.printStackTrace();
					}
		      		 
	      	  }
	      	  shell.close();
	     }   
	     });  
		button_1.setBounds(235, 540, 80, 22);

		final Button button_2 = new Button(shell, SWT.NONE);
		button_2.setText("存稿");
		button_2.addMouseListener(new MouseAdapter(){   
	      	  public   void   mouseUp(MouseEvent   e){   
	      	   r_addr=text.getText();
	      	   title=text_1.getText();
	      	   fujian=text_2.getText();
	      	   content=text_3.getText();
		       DataBase db=new DataBase();
		       db.connectToDB();
				ResultSet rs=null;
				String sql;
				sql="select top 1 * from config";
				String s_addr="";
				String s_name="";
				Date date=new Date();
				SimpleDateFormat sdf  =   new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); 
				String datestr=sdf.format(date);
				try {
				rs=db.executeQuery(sql);
				if(!rs.next())
				{
					MessageDialog.openError(null, "发邮件错误", "读取配置失败,请检查配置!");
					db.close();
					System.exit(0);
					rs.close();
					rs=null;
				}
	
				s_addr=rs.getString("s_addr");
				s_name=rs.getString("s_name");
				db.close();
				rs=null;
				} catch (SQLException e1) {
					// TODO 自动生成 catch 块
					e1.printStackTrace();
					db.close();
					rs=null;
				}
				db.connectToDB();
	      		 sql="insert into mail(title,content,r_addr,fujian_addr,flag,read,s_addr,s_time,r_time,s_name) " +
				 " values('"+title+"','"+content+"','"+r_addr+"','"+fujian+"',2,true,'"+s_addr+"','"+datestr+"','"+datestr+"','"+s_name+"')";
	      		 db.executeInsert(sql);
	      		 try {
					db.conn.close();
					db.stmt.close();
				} catch (SQLException e1) {
					// TODO 自动生成 catch 块
					e1.printStackTrace();
				}
				MessageDialog.openInformation(null, "系统提示!", "已成功保存至草稿箱!");
				shell.close();
		      		 
	     }   
	     });   
		button_2.setBounds(335, 540, 74, 22);

		final Button button_3 = new Button(shell, SWT.NONE);
		button_3.setText("放弃");
		button_3.addMouseListener(new MouseAdapter(){   
	      	  public   void   mouseUp(MouseEvent   e){   
	      		if(MessageDialog.openConfirm(shell, "请确认", "确实要放弃对此邮件作的修改吗?"))
	      		{
	      			shell.close();
	      		}
	     }   
	     }); 
		button_3.setBounds(425, 540, 80, 20);


		shell.open();   
		shell.layout(); 
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}

⌨️ 快捷键说明

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