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

使用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();

}
}






  • 13
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: 这里提供一个简单的Java代码示例,可以实现登录界面,包括登录账号、密码以及注册用户等信息。 首先,我们需要创建一个登录界面的类(比如叫做"LoginGUI"),然后在其中添加用户名、密码和注册按钮。代码如下: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LoginGUI { public static void main(String[] args) { JFrame frame = new JFrame("登录界面"); JPanel panel = new JPanel(); JLabel userLabel = new JLabel("用户名:"); JLabel passwordLabel = new JLabel("密码:"); JTextField userText = new JTextField(20); JPasswordField passwordText = new JPasswordField(20); JButton loginButton = new JButton("登录"); JButton registerButton = new JButton("注册"); panel.setLayout(new GridLayout(3, 2)); panel.add(userLabel); panel.add(userText); panel.add(passwordLabel); panel.add(passwordText); panel.add(loginButton); panel.add(registerButton); loginButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String user = userText.getText(); String password = String.valueOf(passwordText.getPassword()); // 在此处编写登录逻辑 } }); registerButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 在此处编写注册逻辑 } }); frame.add(panel); frame.setSize(300, 150); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } ``` 这里我们使用了Java Swing库来创建登录界面。代码中包含了一个JFrame窗口、一个JPanel面板,以及用户名、密码登录和注册按钮等组件。当用户点击"登录"按钮时,程序将获取用户输入的用户名和密码,并在"loginButton.addActionListener"方法中编写登录逻辑。当用户点击"注册"按钮时,程序将在"registerButton.addActionListener"方法中编写注册逻辑。 当然,这只是一个非常简单的登录界面示例,实际开发中可能需要更加完善的功能和安全性保障。 ### 回答2: 编写一个登录界面,包含登录账号、密码和注册用户等信息,可以使用Java编程语言进行开发。 首先,我们可以创建一个Login类,作为程序的入口。在该类中,我们可以定义一个main方法作为程序的起点。 在该登录界面中,我们可以使用图形用户界面(GUI)来展示登录界面。Java提供了Swing库,可以方便地创建GUI界面。 在Login类中,我们可以创建一个JFrame对象作为登录界面的窗口。然后,我们可以使用JLabel和JTextField分别用于显示标签和接受输入。此外,我们可以使用JPasswordField用于输入密码,并通过设置其样式来保护密码的安全性。 此外,在登录界面中,我们可以添加一个"登录"按钮和一个"注册新用户"按钮。使用JButton来创建这两个按钮,并为它们添加相应的事件监听器。 当用户点击"登录"按钮时,我们可以获取用户输入的账号和密码,并与预先存储的账号密码进行比对。如果账号密码正确,则登录成功,可以弹出一个对话框提示用户登录成功,并且打开主界面。否则,弹出对话框提示用户账号或密码错误。 当用户点击"注册新用户"按钮时,我们可以打开注册界面,该界面用于输入新用户的账号和密码。在注册界面中,我们可以添加一个"确认注册"按钮,用于确认注册信息,并将新用户的账号密码保存到文件或数据库中。 这只是一种开发登录界面的简单实现方式,具体细节可以根据需求进行适当修改和完善。 ### 回答3: 要编写一个Java登录界面,可以使用SwingJavaFX库来实现。下面是一个简单的示例代码,包含登录账号、密码和注册用户功能: ```java import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFrame extends JFrame { private JTextField accountField; private JPasswordField passwordField; public LoginFrame() { setTitle("登录界面"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 200); initComponents(); initListeners(); setVisible(true); } private void initComponents() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); JLabel accountLabel = new JLabel("账号:"); panel.add(accountLabel); accountField = new JTextField(20); panel.add(accountField); JLabel passwordLabel = new JLabel("密码:"); panel.add(passwordLabel); passwordField = new JPasswordField(20); panel.add(passwordField); JButton loginButton = new JButton("登录"); panel.add(loginButton); JButton registerButton = new JButton("注册用户"); panel.add(registerButton); add(panel); } private void initListeners() { ActionListener loginListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 获取输入的账号和密码 String account = accountField.getText(); String password = String.valueOf(passwordField.getPassword()); // 实现登录逻辑,例如和数据库中的账号密码进行比对 JOptionPane.showMessageDialog(LoginFrame.this, "登录成功!"); } }; JButton loginButton = (JButton) getContentPane().getComponent(0).getComponent(4); loginButton.addActionListener(loginListener); ActionListener registerListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 实现注册用户逻辑 JOptionPane.showMessageDialog(LoginFrame.this, "注册成功!"); } }; JButton registerButton = (JButton) getContentPane().getComponent(0).getComponent(5); registerButton.addActionListener(registerListener); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new LoginFrame(); } }); } } ``` 你可以使用此示例代码作为起点,并根据实际需求进行修改和扩展。例如,可以添加数据库操作来验证登录信息,并对注册用户功能进行实现
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值