java-登录界面

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class MainWindow extends JFrame implements ActionListener {
    private JLabel nameLabel;
    private JTextField nameField;
    private JButton submitButton;
    private JPanel inputPanel;

    public MainWindow() {
        // 初始化窗口设置
        setTitle("Welcome!");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建输入面板和组件
        createInputPanel();

        // 添加输入面板到窗口中
        add(inputPanel, BorderLayout.CENTER);
    }

    private void createInputPanel() {
        // 创建输入面板和组件
        inputPanel = new JPanel(new FlowLayout());
        nameLabel = new JLabel("Enter your name: ");
        nameField = new JTextField(20);
        submitButton = new JButton("Login");

        // 添加组件和事件监听器到输入面板中
        inputPanel.add(nameLabel);
        inputPanel.add(nameField);
        inputPanel.add(submitButton);
        submitButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 处理按钮点击事件
        String name = nameField.getText();

        // 关闭当前窗口
        dispose();

        // 打开登录窗口
        LoginWindow loginWindow = new LoginWindow(name);
        loginWindow.setVisible(true);
    }

    public static void main(String[] args) {
        // 创建窗口并显示
        MainWindow window = new MainWindow();
        window.setVisible(true);
    }
}


class LoginWindow extends JFrame implements ActionListener {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JTextField newPasswordField;

    private String fileName = "password.txt";

    public LoginWindow(String name) {
        // 初始化窗口设置
        setTitle("Login");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建欢迎信息面板
        JLabel welcomeLabel = new JLabel("Welcome, " + name + "! Please login.");
        add(welcomeLabel, BorderLayout.NORTH);

        // 创建输入面板和组件
        JPanel inputPanel = new JPanel(new GridLayout(3, 2));

        JLabel usernameLabel = new JLabel("Username:");
        JLabel passwordLabel = new JLabel("Password:");
        usernameField = new JTextField(20);
        passwordField = new JPasswordField(20);

        JLabel newPasswordLabel = new JLabel("New password:");
        newPasswordField = new JTextField(20);

        // 将组件添加到输入面板中
        inputPanel.add(usernameLabel);
        inputPanel.add(usernameField);
        inputPanel.add(passwordLabel);
        inputPanel.add(passwordField);
        inputPanel.add(newPasswordLabel);
        inputPanel.add(newPasswordField);

        // 创建登录按钮
        loginButton = new JButton("Login");
        loginButton.addActionListener(this);

        // 将输入面板和登录按钮添加到窗口中
        add(inputPanel, BorderLayout.CENTER);
        add(loginButton, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 处理按钮点击事件
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());

        // 读取密码文件
        String savedPassword = readPassword();

        if (savedPassword.isEmpty()) {
            // 初始状态,文件中没有密码,设置新密码
            String newPassword = newPasswordField.getText();
            if (newPassword.isEmpty()) {
                // 新密码为空,不能设置
                JOptionPane.showMessageDialog(this, "Please set a new password first!");
            } else {
                writePassword(newPassword);
                JOptionPane.showMessageDialog(this, "Password set successfully!");
            }

        } else {
            // 文件中已有密码,进行验证
            if (username.equals("admin") && password.equals(savedPassword)) {
                JOptionPane.showMessageDialog(this, "Login success!");
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Username or password is incorrect!");
            }
        }
    }

    private String readPassword() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line = reader.readLine();
            reader.close();
            return line;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private void writePassword(String newPassword) {
        try {
            FileWriter writer = new FileWriter(fileName);
            writer.write(newPassword);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 创建窗口并显示
        LoginWindow window = new LoginWindow("");
        window.setVisible(true);
    }
}

LoginWindow中,我们添加了一个“New password:”标签和一个新密码输入文本框。如果用户在文件中没有设置密码,则程序会提示它们首先设置新密码。如果用户在文件中已经设置了密码,则程序会加载并验证密码。如果验证成功,程序会打开“登录成功”对话框并关闭登录窗口。如果验证失败,程序会打开“用户名或密码不正确”的对话框。如果用户在登录窗口中输入了新密码,则程序会将新密码存储在文件中。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值