Java实现账号密码登录窗口(包含修改密码等)

来源:互联网 发布:淘宝直通车官网申请 编辑:程序博客网 时间:2024/05/17 09:24

使用JAVA的windowsBuild,可是实现界面制作,本章只做了一个简单的登录界面,并使用文件储存相关信息,代码如下:

package ShoppingWork;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;


public class Account implements Serializable {
 private String ID=null;
 
 private String pwd=null;
 
 ArrayList<Account> lis=new ArrayList<Account>();
 


 
  public Account(String ID,String pwd){        //构造方法写入,传参为账号的用户名及密码
this.ID=ID;
this.pwd=pwd;
 
 }
  public void setlis(){    //调用集合储存写入的账号及密码
Account user=new Account(ID,pwd);
ArrayList<Account> lis=new ArrayList<Account>();
lis.add(user);
this.lis=lis;

}
  public void Olis() throws IOException, ClassNotFoundException{//将集合储存在文件中
  ObjectOutputStream obj=new ObjectOutputStream(new FileOutputStream("D:/超市购物系统/account.txt"));


  obj.writeObject(lis);
  obj.flush();
  obj.close();

  }
  public void Ilis() throws IOException, ClassNotFoundException{//读取文本
  ObjectInputStream obj=new ObjectInputStream(new FileInputStream("D:/超市购物系统/account.txt"));
  lis=(ArrayList<Account>) obj.readObject();

   
  }




public void getlis(){   //查询现有账号及密码
for(int a=0;a<lis.size();a++){
System.out.println(lis.get(a));
}



}
public boolean boolaccount(String name,String pwd){//使用indexOf方法测试输入的账号密码是否与库中的匹配
Account user=new Account(name,pwd);
if(lis.indexOf(user)==-1){
return false;
}else{
return true;
}
}
@Override
public String toString() {
return "Account [ID=" + ID + ", pwd=" + pwd + "]";
}
@Override //重写equals方法
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ID == null) ? 0 : ID.hashCode());
result = prime * result + ((lis == null) ? 0 : lis.hashCode());
result = prime * result + ((pwd == null) ? 0 : pwd.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (ID == null) {
if (other.ID != null)
return false;
} else if (!ID.equals(other.ID))
return false;
if (lis == null) {
if (other.lis != null)
return false;
} else if (!lis.equals(other.lis))
return false;
if (pwd == null) {
if (other.pwd != null)
return false;
} else if (!pwd.equals(other.pwd))
return false;
return true;
}


}


package ShoppingWork;


import java.awt.BorderLayout;
import java.awt.EventQueue;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;


public class NewAccount extends JFrame {


private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private JPasswordField passwordField_1;


/**
* Launch the application.
*/



/**
* Create the frame.
*/
public NewAccount() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

JLabel lblNewLabel = new JLabel("\u65B0\u8D26\u6237\u540D");

JLabel label = new JLabel("\u65B0\u5BC6\u7801");

textField = new JTextField();
textField.setColumns(10);

JButton btnNewButton = new JButton("\u786E\u8BA4");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Account acc=new Account(textField.getText(),passwordField_1.getText());
if(passwordField.getText().equals(passwordField_1.getText())){
acc.setlis();
try {
acc.Olis();
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "修改成功!!", "成功", JOptionPane.OK_CANCEL_OPTION);
hide();

}else{
JOptionPane.showMessageDialog(null, "两次密码输入不一致,修改失败!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});

passwordField = new JPasswordField();

JLabel lblNewLabel_1 = new JLabel("\u8BBE\u7F6E\u65B0\u7684\u8D26\u6237\u540D\u53CA\u5BC6\u7801");

passwordField_1 = new JPasswordField();

JLabel label_1 = new JLabel("\u518D\u6B21\u8F93\u5165\u65B0\u5BC6\u7801");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(81)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(lblNewLabel)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED))
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(label_1)
.addGap(15)))
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
.addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, 201, GroupLayout.PREFERRED_SIZE)))
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addGap(45)
.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 276, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(90, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(20)
.addComponent(lblNewLabel_1)
.addGap(31)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1))
.addPreferredGap(ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
.addComponent(btnNewButton)
.addGap(32))
);
contentPane.setLayout(gl_contentPane);
}
}


package ShoppingWork;


import java.awt.BorderLayout;
import java.awt.EventQueue;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;


public class Loading extends JFrame {


private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
NewAccount newac=new NewAccount();
Menu men=new Menu();



/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Loading frame = new Loading();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


/**
* Create the frame.
*/
public Loading() {
setResizable(false);
setTitle("\u8D85\u5E02\u7BA1\u7406\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

JLabel lblNewLabel = new JLabel("\u8D26\u53F7");

JLabel label = new JLabel("\u5BC6\u7801");

textField = new JTextField();
textField.setColumns(10);

JLabel label_1 = new JLabel("\u8D26\u6237\u7C7B\u578B");

JComboBox comboBox = new JComboBox();
comboBox.addItem("管理员");
comboBox.addItem("收银员");

JButton btnNewButton = new JButton("\u767B\u5F55");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Account acc=new Account(textField.getText(), passwordField.getText());
try {
acc.Ilis();
if(acc.boolaccount(textField.getText(), passwordField.getText())){
men.setVisible(true);
hide();
}else{
JOptionPane.showMessageDialog(null, "账户名或密码错误!", "错误", JOptionPane.ERROR_MESSAGE);
}

} catch (ClassNotFoundException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
});

JButton btnNewButton_1 = new JButton("\u4FEE\u6539\u8D26\u6237\u540D\u5BC6\u7801");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Account acc=new Account(textField.getText(), passwordField.getText());
try {
acc.Ilis();
if(acc.boolaccount(textField.getText(), passwordField.getText())){
newac.setVisible(true);
}else{
JOptionPane.showMessageDialog(null, "账户名或密码错误!", "错误", JOptionPane.ERROR_MESSAGE);
}

} catch (ClassNotFoundException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

passwordField = new JPasswordField();

JRadioButton rdbtnNewRadioButton = new JRadioButton("\u8BB0\u4F4F\u8D26\u53F7");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(rdbtnNewRadioButton))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(75)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblNewLabel)
.addGroup(gl_contentPane.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)))
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(btnNewButton_1))))))
.addGap(86))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(69)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1)
.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(btnNewButton_1))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(rdbtnNewRadioButton)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnNewButton)
.addContainerGap(42, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);

}
public String getname(){
return textField.getText();

}
}






阅读全文
0 0