📄 mailclient.java
字号:
/*
此类是创建邮件代理界面并调用邮件发送
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
class Platform extends JDialog implements ActionListener{
private static final int WINDOW_WIDTH=300;
private static final int WINDOW_HEIGHT=350;
private static final int FIELD_WIDTH=25;
private static final int AREA_WIDTH=8;
private static final int AREA_HEIGHT=25;
private static final int PORT_NUMBER=25;
private static final Point LOCATION=new Point(200,150);
private JLabel fromTag=new JLabel("From: ");
private JTextField fromText=new JTextField(FIELD_WIDTH);
private JLabel toTag=new JLabel("To: ");
private JTextField toText=new JTextField(FIELD_WIDTH);
private JLabel subjectTag=new JLabel("Subject:");
private JTextField subjectText=new JTextField(FIELD_WIDTH);
private JLabel messageTag= new JLabel("Message:");
private JTextArea messageArea=new JTextArea(AREA_WIDTH,AREA_HEIGHT);
private JButton sendButton=new JButton("Send");
private JButton clearButton=new JButton("Clear");
private JButton quitButton=new JButton("Quit");
//初始化邮件代理界面
public Platform(JFrame jf){
super(jf,"Java MailClient",true);
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
messageArea.setWrapStyleWord(true);
messageArea.setLineWrap(true);
messageArea.setBackground(Color.white);
sendButton.addActionListener(this);
clearButton.addActionListener(this);
quitButton.addActionListener(this);
messageArea.setLineWrap(true);
Panel tag=new Panel();
Panel text=new Panel();
Panel area=new Panel();
Panel up=new Panel();
Panel button=new Panel();
Panel whole=new Panel();
up.setLayout(new BorderLayout());
tag.setLayout(new GridLayout(3,1));
text.setLayout(new GridLayout(3,1));
tag.add(fromTag);
tag.add(toTag);
tag.add(subjectTag);
text.add(fromText);
text.add(toText);
text.add(subjectText);
up.add(tag,BorderLayout.WEST);
up.add(text,BorderLayout.CENTER);
area.setLayout(new BorderLayout());
area.add(messageTag,BorderLayout.NORTH);
area.add(messageArea,BorderLayout.CENTER);
button.setLayout(new GridLayout(1,3));
button.add(sendButton);
button.add(clearButton);
button.add(quitButton);
whole.setLayout(new BorderLayout());
whole.add(up,BorderLayout.NORTH);
whole.add(area,BorderLayout.CENTER);
whole.add(button,BorderLayout.SOUTH);
add(whole);
setLocation(LOCATION);
setResizable(false);
show();
}
//侦听事件接口
public void actionPerformed(ActionEvent e){
try{
if (e.getActionCommand()=="Send"){
Login login=new Login(new JFrame());
SendMail mail=new SendMail();
mail.setFrom(fromText.getText().trim());
mail.setTo(toText.getText().trim());
mail.setSubject(subjectText.getText().trim());
mail.setData(messageArea.getText()) ;
mail.createSocket(mail.getservername(),PORT_NUMBER);
if(mail.transmit(login.getUserName(),login.getUserPassword())==1){
System.out.println("The mai has been sent!");
}
else{
System.out.println("It's fail to sent the mail!");
}
mail.close();
}
else if(e.getActionCommand()=="Clear"){
messageArea.setText(null);
}
else{
System.exit(1);
}
}
catch(Exception f){
System.out.println("There is a Exception.");
}
}
}
public class MailClient{
public static void main(String[] args) throws Exception{
Platform a=new Platform(new JFrame());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -