📄 useraccountsetui.java
字号:
package mail.ui;import java.awt.Color;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;import javax.swing.BorderFactory;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 UserAccountSetUI extends JFrame implements ActionListener{ JLabel labAccount = new JLabel(); JTextField tfAccount = new JTextField(); JLabel labPsw = new JLabel(); JLabel labPsw2 = new JLabel(); JTextField tfSMTP = new JTextField(); JLabel labSMTP = new JLabel(); JTextField tfPOP3 = new JTextField(); JLabel labPOP3 = new JLabel(); JButton bEnter = new JButton(); JButton bCancel = new JButton(); JPasswordField tfPsw = new JPasswordField(); JPasswordField tfPsw2 = new JPasswordField(); /** * 邮件客户端 */ private MailClientUI mailClient; public UserAccountSetUI(MailClientUI mailClient) { initUI(); this.mailClient = mailClient; } private void initUI() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JFrame.setDefaultLookAndFeelDecorated(true); labAccount.setBorder(BorderFactory.createLineBorder(Color.black)); labAccount.setText(" 您的邮箱帐号"); labAccount.setBounds(new Rectangle(51, 40, 86, 34)); this.getContentPane().setBackground(Color.lightGray); this.getContentPane().setLayout(null); tfAccount.setBorder(BorderFactory.createLineBorder(Color.black)); tfAccount.setBounds(new Rectangle(153, 40, 232, 34)); labPsw.setBorder(BorderFactory.createLineBorder(Color.black)); labPsw.setText(" 邮箱密码"); labPsw.setBounds(new Rectangle(51, 93, 86, 34)); labPsw2.setBounds(new Rectangle(51, 146, 86, 34)); labPsw2.setText(" 邮箱密码确认"); labPsw2.setBorder(BorderFactory.createLineBorder(Color.black)); tfSMTP.setBounds(new Rectangle(153, 198, 232, 34)); tfSMTP.setBorder(BorderFactory.createLineBorder(Color.black)); labSMTP.setBounds(new Rectangle(51, 198, 86, 34)); labSMTP.setText(" SMTP服务器"); labSMTP.setBorder(BorderFactory.createLineBorder(Color.black)); tfPOP3.setBounds(new Rectangle(153, 251, 232, 34)); tfPOP3.setBorder(BorderFactory.createLineBorder(Color.black)); labPOP3.setBounds(new Rectangle(51, 251, 86, 34)); labPOP3.setText(" POP3服务器"); labPOP3.setBorder(BorderFactory.createLineBorder(Color.black)); bEnter.setBackground(Color.white); bEnter.setBorder(BorderFactory.createLineBorder(Color.black)); bEnter.setText("确定"); bEnter.setBounds(new Rectangle(82, 301, 113, 38)); bCancel.setBackground(Color.white); bCancel.setBorder(BorderFactory.createLineBorder(Color.black)); bCancel.setText("取消"); bCancel.setBounds(new Rectangle(214, 301, 113, 38)); tfPsw.setBorder(BorderFactory.createLineBorder(Color.black)); tfPsw.setBounds(new Rectangle(153, 93, 232, 34)); tfPsw2.setBounds(new Rectangle(153, 146, 232, 34)); tfPsw2.setBorder(BorderFactory.createLineBorder(Color.black)); this.getContentPane().add(labAccount, null); this.getContentPane().add(labPsw, null); this.getContentPane().add(tfAccount, null); this.getContentPane().add(labPsw2, null); this.getContentPane().add(labSMTP, null); this.getContentPane().add(tfSMTP, null); this.getContentPane().add(labPOP3, null); this.getContentPane().add(tfPOP3, null); this.getContentPane().add(bEnter, null); this.getContentPane().add(bCancel, null); this.getContentPane().add(tfPsw, null); this.getContentPane().add(tfPsw2, null); this.bCancel.addActionListener(this); this.bEnter.addActionListener(this); this.setSize(450, 400); this.setLocation(100, 50); this.setTitle("设置邮箱帐户窗口"); } public static void main(String[] args) { new UserAccountSetUI(new MailClientUI()).setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(this.bEnter)) { saveSetting(); } else if (e.getSource().equals(this.bCancel)) { cancel(); } } /** * 保存设置 */ private void saveSetting() { if (checkSeting() == 1) { mailClient.setEmailName(this.tfAccount.getText().trim()); mailClient.setEmailPsw(new String(tfPsw.getPassword())); mailClient.setPop3Server(this.tfPOP3.getText().trim()); mailClient.setSmtpServer(this.tfSMTP.getText().trim()); save(); } } private void save() { try { File nowdir = new File(".").getAbsoluteFile(); String dirString = nowdir.toString(); String subs = dirString.substring(0, dirString.length() - 1); File fileInfo = new File(subs + "info/"); if (!fileInfo.exists()) fileInfo.mkdir(); File serverInfo = new File(fileInfo, "mail.ini"); FileWriter fw = new FileWriter(serverInfo); writeOneLine(fw, mailClient.getEmailName()); writeOneLine(fw, mailClient.getEmailPsw()); writeOneLine(fw, mailClient.getSmtpServer()); writeOneLine(fw, mailClient.getPop3Server()); fw.close(); JOptionPane.showMessageDialog(this, "您的设置信息已经被成功保存"); this.setVisible(false); mailClient.getReceiveUI().setVisible(true); } catch (FileNotFoundException fnfe) { error("无法找到文件:" + fnfe.getMessage()); } catch (IOException ioe) { error("打开文件过程中发生IO错误:" + ioe.getMessage()); } catch (Exception e) { error("非预知错误:" + e.getMessage()); } } private void writeOneLine(FileWriter fw, String line) throws IOException { fw.write(line+"\r\n"); } /** * 检查设置 */ private int checkSeting() { if (this.tfAccount.getText().trim().equals("")) { error("错误设置提示:您必须输入email帐号"); return 0; } if (this.tfAccount.getText().trim().indexOf("@") < 0) { error("错误设置提示:您必须输入正确的email帐号"); return 0; } if (this.tfPsw.getPassword().length==0) { error("错误设置提示:您必须输入email密码"); return 0; } String psw1=new String(tfPsw.getPassword()); String psw2=new String(tfPsw2.getPassword()); if (!psw1.equals(psw2)) { error("错误设置提示:您两次输入的密码不一致"); return 0; } if (this.tfSMTP.getText().trim().equals("")) { error("错误设置提示:您必须输入SMTP服务器(IP或服务器名)"); return 0; } if (this.tfPOP3.getText().trim().equals("")) { error("错误设置提示:您必须输入POP3服务器(IP或服务器名)"); return 0; } return 1; } /** * 取消设置 */ private void cancel() { this.setVisible(false); mailClient.getReceiveUI().setVisible(true); } public void setVisibleTrue() { this.setVisible(true); if(mailClient.getEmailName()!=null) this.tfAccount.setText(mailClient.getEmailName()); if(mailClient.getSmtpServer()!=null) this.tfSMTP.setText(mailClient.getSmtpServer()); if(mailClient.getPop3Server()!=null) this.tfPOP3.setText(mailClient.getPop3Server()); } /** * 显示错误信息 * * @param errorInfo */ private synchronized void error(String errorInfo) { if (mailClient != null) mailClient.errorLog(errorInfo); JOptionPane.showMessageDialog(this, errorInfo); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -