📄 pwfinder.java
字号:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Toolkit;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InvalidClassException;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.net.Socket;
// 定义找回密码类PwFinder,生成密码提示界面
public class PwFinder extends JFrame {
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JLabel pictureLabel = null;
private JLabel questionLabel1 = null;
private JLabel answerLabel = null;
private JComboBox questionsComboBox = null;
private JTextField answerTextField = null;
private JTextField showPwTextField = null;
private JButton findButton = null;
private JTextField user_nameTextField = null;
private JLabel user_nameLabel = null;
private String password = ""; // 用来存储从服务器返回的密码
private JPanel getJPanel() {
if (jPanel == null) {
answerLabel = new JLabel();
answerLabel.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 16));
answerLabel.setForeground(new java.awt.Color(153,0,153));
answerLabel.setLocation(new java.awt.Point(9,50));
answerLabel.setSize(new java.awt.Dimension(114,26));
answerLabel.setText("提示问题答案:");
questionLabel1 = new JLabel();
questionLabel1.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 16));
questionLabel1.setForeground(new java.awt.Color(153,0,153));
questionLabel1.setSize(new java.awt.Dimension(114,26));
questionLabel1.setLocation(new java.awt.Point(9,12));
questionLabel1.setText("密码提示问题:");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
jPanel.setLocation(new java.awt.Point(6,103));
jPanel.setSize(new java.awt.Dimension(337,88));
jPanel.setBackground(new java.awt.Color(236,236,244));
jPanel.add(questionLabel1, null);
jPanel.add(answerLabel, null);
jPanel.add(getQuestionsComboBox(), null);
jPanel.add(getAnswerTextField(), null);
}
return jPanel;
}
private JComboBox getQuestionsComboBox() {
if (questionsComboBox == null) {
questionsComboBox = new JComboBox();
questionsComboBox.setSize(new java.awt.Dimension(191,23));
questionsComboBox.setEditable(true);
questionsComboBox.setBackground(new java.awt.Color(255,255,204));
questionsComboBox.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 14));
questionsComboBox.setMaximumRowCount(4);
questionsComboBox.addItem("我是谁?");
questionsComboBox.addItem("我是男还是女?");
questionsComboBox.addItem("我最爱的人是谁?");
questionsComboBox.addItem("我最爱的颜色?");
questionsComboBox.addItem("我最爱的电影?");
questionsComboBox.addItem("我最爱的食物?");
questionsComboBox.setToolTipText("请输入您的问题或从列表中选择");
questionsComboBox.setSelectedIndex(-1);
questionsComboBox.setLocation(new java.awt.Point(137,14));
}
return questionsComboBox;
}
private JTextField getAnswerTextField() {
if (answerTextField == null) {
answerTextField = new JTextField();
answerTextField.setLocation(new java.awt.Point(137,52));
answerTextField.setSize(new java.awt.Dimension(191,23));
}
return answerTextField;
}
private JTextField getShowPwTextField() {
if (showPwTextField == null) {
showPwTextField = new JTextField();
showPwTextField.setBounds(new java.awt.Rectangle(99,196,243,26));
showPwTextField.setText("密码显示区");
showPwTextField.setEditable(false);
showPwTextField.setEnabled(true);
showPwTextField.setBackground(new java.awt.Color(204,255,255));
showPwTextField.setForeground(java.awt.Color.blue);
showPwTextField.setToolTipText("");
showPwTextField.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 20));
}
return showPwTextField;
}
private JButton getFindButton() {
if (findButton == null) {
findButton = new JButton();
findButton.setLocation(new java.awt.Point(9,196));
findButton.setIcon(new ImageIcon(getClass().getResource("/pictures/find.JPG")));
findButton.setToolTipText("提交信息,并显示密码");
findButton.setBackground(java.awt.Color.white);
findButton.setSize(new java.awt.Dimension(77,28));
findButton.addActionListener(new ButtonListener());
}
return findButton;
}
private JTextField getUser_nameTextField() {
if (user_nameTextField == null) {
user_nameTextField = new JTextField();
user_nameTextField.setBounds(new java.awt.Rectangle(168,70,176,27));
}
return user_nameTextField;
}
// 定义主函数main()
public static void main(String[] args)
{
new PwFinder();
}
// 定义构造函数
public PwFinder() {
super();
initialize();
}
// 定义初始化函数
private void initialize()
{
this.setSize(360, 255);
this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/pictures/logo1.jpg")));
this.setBackground(new java.awt.Color(188,222,255));
this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.setContentPane(getJContentPane());
this.setTitle("Fetion密码提示");
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
user_nameLabel = new JLabel();
user_nameLabel.setBounds(new java.awt.Rectangle(8,69,200,28));
user_nameLabel.setFont(new java.awt.Font("楷体_GB2312", java.awt.Font.PLAIN, 18));
user_nameLabel.setForeground(java.awt.Color.blue);
user_nameLabel.setText("请首先输入用户名:");
pictureLabel = new JLabel();
pictureLabel.setText("");
pictureLabel.setSize(new java.awt.Dimension(352,67));
pictureLabel.setIcon(new ImageIcon(getClass().getResource("/pictures/PwFind.JPG")));
pictureLabel.setLocation(new java.awt.Point(1,2));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setBackground(new java.awt.Color(245,222,245));
jContentPane.add(getJPanel(), null);
jContentPane.add(pictureLabel, null);
jContentPane.add(getShowPwTextField(), null);
jContentPane.add(getFindButton(), null);
jContentPane.add(getUser_nameTextField(), null);
jContentPane.add(user_nameLabel, null);
}
return jContentPane;
}
// 用户自定义类和函数
// 定义"找回密码"按钮监听器
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
JButton obj = (JButton)evt.getSource();
if(obj == findButton)
{
// msgFromServer用来存储从服务器返回的信息
String msgFromServer = "";
// 调用CheckInfo()检测密码安全信息
String msgFromCheck = (String)checkInfo();
// 如果通过检测,则连接服务器,发送套接字给服务器
if(msgFromCheck.compareTo("") == 0)
{
PwSafetyInfo data = (PwSafetyInfo)getPwSafetyInfo();
// tData用来存储发送到服务器的信息
TransData tData = new TransData();
tData.flag = TProtocal.PW_SAFETY_INFO;
tData.dataObj = (PwSafetyInfo)data;
try
{
// 创建套接字,连接服务器
Socket toServer;
toServer = new Socket(ServerIP.ADDRESS,ServerIP.PORT);
// 创建输出流,将接收的密码安全信息发送到服务器
ObjectOutputStream streamToServer = new ObjectOutputStream(toServer.getOutputStream());
streamToServer.writeObject((TransData)tData);
// 创建输入流,将接收服务器返回的消息
BufferedReader streamFromServer = new BufferedReader(new InputStreamReader(toServer.getInputStream()));
msgFromServer = (String)streamFromServer.readLine();
// 关闭数据流
streamToServer.close();
streamFromServer.close();
toServer.close();
}
catch(InvalidClassException e)
{
System.out.println("无效类LoginInfo" + e);
return;
}
catch(NotSerializableException e)
{
System.out.println("对象未序列化" + e);
return;
}
catch(IOException e)
{
System.out.println("传输用户登录信息到服务器失败" + e);
JOptionPane.showMessageDialog(findButton,"连接服务器失败!");
return;
}
// 根据返回消息给用户相应的提示信息
if(msgFromServer.compareTo("NoRegister") == 0)
{
showPwTextField.setText("");
JOptionPane.showMessageDialog(findButton,"此用户名不存在!");
}
else if(msgFromServer.compareTo("NoSafety") == 0)
{
showPwTextField.setText("");
JOptionPane.showMessageDialog(findButton,"您没有进行密码安全设置,无法找回密码!");
}
else if(msgFromServer.compareTo("Error") == 0)
{
showPwTextField.setText("");
questionsComboBox.setSelectedItem(null);
answerTextField.setText("");
JOptionPane.showMessageDialog(findButton,"您提交的信息有误,请重新输入!");
}
else if(msgFromServer.compareTo("Fail") == 0)
{
showPwTextField.setText("");
JOptionPane.showMessageDialog(findButton,"密码找回失败!");
}
else
{
password = msgFromServer;
showPwTextField.setText(password);
}
}
else
{
JOptionPane.showMessageDialog(findButton,msgFromCheck);
}
}
}
}
// 定义checkInfo()函数,在客户端对输入的注册信息进行检测
public String checkInfo()
{
String msgReturn = "";
PwSafetyInfo data = (PwSafetyInfo)getPwSafetyInfo();
if(data.cUserID.compareTo("") == 0)
{
msgReturn = "请输入用户名!";
}
else if(data.vQuestion.compareTo("") == 0 || data.vAnswer.compareTo("") == 0)
{
msgReturn = "密码提示问题和答案不能为空!";
}
return msgReturn;
}
//定义getPwSafetyInfo()函数,用来从密码提示界面获取输入信息
public PwSafetyInfo getPwSafetyInfo()
{
PwSafetyInfo temp = new PwSafetyInfo();
temp.cUserID = user_nameTextField.getText().trim();
temp.vQuestion = String.valueOf(questionsComboBox.getSelectedItem()).trim();
temp.vAnswer = answerTextField.getText().trim();
return temp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -