📄 mail.java
字号:
package UI;
import SendMail.*;
import DB.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.sql.*;
public class Mail extends JPanel implements ActionListener
{
Properties pro = new Properties();
JLabel re = new JLabel("收信人");
JLabel sub = new JLabel("主题");
JLabel mess = new JLabel("内容");
JTextField receiver = new JTextField();
JTextField title = new JTextField();
JTextArea body = new JTextArea();
DefaultListModel listModel = new DefaultListModel();
JList list;
JButton template = new JButton("载入模板");
JButton saveTemplate = new JButton("保存模板");
JButton send = new JButton("发送");
JButton clear = new JButton("清空");
JComboBox combo;
Info info;
String sReceiver = null;
String sTitle = null;
String sBody = null;
DB db;
ResultSet rs;
Vector v = new Vector();
String sql1 = "select deptName from department";
JFileChooser fc;
File f;
int flag;
public Mail(Info info)
{
db = new DB();
fc = new JFileChooser();
this.setLayout(null);
this.setSize(800,600);
re.setBounds(10,50,60,35);
sub.setBounds(10,100,60,35);
mess.setBounds(10,150,60,35);
receiver.setBounds(70,50,400,35);
title.setBounds(70,100,400,35);
JScrollPane sbody = new JScrollPane(body);
sbody.setBounds(70,150,400,350);
list = new JList(listModel);
JScrollPane sList = new JScrollPane(list);
sList.setBounds(500,100,250,300);
try
{
rs = db.search(sql1);
while(rs.next())
{
v.add(rs.getString("deptName"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
combo = new JComboBox(v);
combo.setBounds(500,50,250,35);
combo.addActionListener(this);
template.setBounds(500,430,90,30);
template.addActionListener(this);
saveTemplate.setBounds(630,430,90,30);
saveTemplate.addActionListener(this);
send.setBounds(500,470,90,30);
send.addActionListener(this);
clear.setBounds(630,470,90,30);
clear.addActionListener(this);
this.add(re); this.add(sub); this.add(mess);
this.add(receiver);
this.add(title);
this.add(sbody);
this.add(sList);
this.add(template);
this.add(saveTemplate);
this.add(send);
this.add(clear);
this.add(combo);
this.info = info;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == clear)
{
receiver.setText("");
title.setText("");
body.setText("");
}
if(e.getSource() == send)
{
sReceiver = receiver.getText();
sTitle = title.getText();
sBody = body.getText();
String [] receiveList = sReceiver.split(";");
new SendMail(sTitle,sBody,receiveList,info);
}
if(e.getSource() == template)
{
try
{
fc.setDialogTitle("Open Templete");
flag=fc.showOpenDialog(this);
}
catch (Exception tempException)
{
tempException.printStackTrace();
}
if(flag==JFileChooser.APPROVE_OPTION)
{
//获得该文件
f = fc.getSelectedFile();
System.out.println("open file----"+f.getName());
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line = null;
while((line = br.readLine())!=null)
{
body.append(line+"\n");
}
br.close();
fr.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if(e.getSource() == saveTemplate)
{
fc.setDialogTitle("load Template");
//String fileName = null;
flag=fc.showSaveDialog(this);
if(flag==JFileChooser.APPROVE_OPTION)
{
f=fc.getSelectedFile();
try {
FileWriter fw = new FileWriter(f);
String saveTemp = null;
saveTemp = body.getText();
String []line = saveTemp.split("\n");
for(int i = 0; i < line.length; i++)
{
fw.write(line[i]+"\r\n");
}
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
//String sqlSave = "updata template set template = '"+saveTemp+"' where
}
if(e.getSource() == combo)
{
receiver.setText("");
String sReceiver = "";
String sDept = (String)combo.getSelectedItem();
String sql ="select * from department a, employee b where a.deptName=b.deptName and b.deptName='"+sDept+"'";
//String sqlEmail ="select email from department a, employee b where a.deptName=b.deptName and b.deptName='"+sDept+"'";
listModel.removeAllElements();
try
{
rs.close();
rs = db.search(sql);
while(rs.next())
{
listModel.addElement(rs.getString("name"));
sReceiver += rs.getString("email") + ";";
}
receiver.setText(sReceiver);
}
catch (Exception sqlException)
{
sqlException.printStackTrace();
}
}
}
public static void main(String args[])
{
JFrame f = new JFrame();
f.add(new Mail(new Info()));
f.setSize(800,600);
f.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -