java:logform

 

/**
 * encoding: utf-8
 * 版权所有 2025 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 17
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2025 - 2025/10/21 - 20:58
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : swingdemo
 * File      : LoginWindow.java
 * explain   : 学习  类
 **/

package com.example.interfaces;

import com.example.application.LoginApplicationService;
import com.example.domain.entity.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Optional;

public class LoginWindow extends JFrame {
    private final LoginApplicationService loginService;
    private JTextField usernameField;
    private JPasswordField passwordField;

    public LoginWindow(LoginApplicationService loginService) {
        this.loginService = loginService;
        initializeUI();
    }

    private void initializeUI() {
        setTitle("登录");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 设置全局字体(支持中文)
        Font labelFont = new Font("SimHei", Font.PLAIN, 14);

        JPanel panel = new JPanel(new GridLayout(3, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        JLabel userLabel = new JLabel("用户名:");
        userLabel.setFont(labelFont); // 用户名标签字体
        userLabel.setBounds(50, 50, 200, 30); // 设置位置和大小
        userLabel.setHorizontalAlignment(JLabel.RIGHT);//对齐
        userLabel.setVerticalAlignment(JLabel.CENTER);
        panel.add(userLabel);
        usernameField = new JTextField();
        usernameField.setFont(labelFont); // 输入框字体
        panel.add(usernameField);

        JLabel passLabel = new JLabel("密码:");
        passLabel.setFont(labelFont); // 密码标签字体
        passLabel.setBounds(50, 50, 200, 30); // 设置位置和大小
        passLabel.setHorizontalAlignment(JLabel.RIGHT);
        passLabel.setVerticalAlignment(JLabel.CENTER);
        panel.add(passLabel);
        passwordField = new JPasswordField();
        passwordField.setFont(labelFont); // 密码框字体
        panel.add(passwordField);

        JButton loginButton = new JButton("登录");
        loginButton.setFont(labelFont); // 按钮字体
        loginButton.addActionListener(this::handleLogin);
        panel.add(loginButton);

        JButton cancelButton = new JButton("取消");
        cancelButton.setFont(labelFont); // 按钮字体
        cancelButton.addActionListener(e -> System.exit(0));
        panel.add(cancelButton);

        add(panel);
        setVisible(true);
    }


    private void handleLogin(ActionEvent e) {
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());

        Optional<User> user = loginService.login(username, password);

        if (user.isPresent()) {
            JOptionPane.showMessageDialog(this, "登录成功,欢迎回来," + user.get().getFullName() + "!");
            dispose(); // 关闭登录窗口
            new MainWindow(user.get()); // 打开主窗口
        } else {
            JOptionPane.showMessageDialog(this, "用户名或密码错误", "登录失败", JOptionPane.ERROR_MESSAGE);
        }
    }
}

  

/**
 * encoding: utf-8
 * 版权所有 2025 ©涂聚文有限公司 ®
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:
 * Author    : geovindu,Geovin Du 涂聚文.
 * IDE       : IntelliJ IDEA 2024.3.6 Java 17
 * # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j
 * # OS        : window10
 * Datetime  : 2025 - 2025/10/21 - 20:59
 * User      : geovindu
 * Product   : IntelliJ IDEA
 * Project   : swingdemo
 * File      : MainWindow.java
 * explain   : 学习  类
 **/

package com.example.interfaces;

import com.example.domain.entity.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class MainWindow extends JFrame {
    private final User currentUser;

    public MainWindow(User currentUser) {
        this.currentUser = currentUser;
        initializeUI();
    }

    private void initializeUI() {
        setTitle("主窗口 - 欢迎," + currentUser.getFullName());
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建菜单栏(设置中文字体)
        JMenuBar menuBar = new JMenuBar();
        // 为菜单和菜单项设置字体(支持中文)
        Font menuFont = new Font("SimHei", Font.PLAIN, 14); // 黑体,常规,14号字

        // 文件菜单
        JMenu fileMenu = new JMenu("文件");
        fileMenu.setFont(menuFont); // 设置菜单字体
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.setFont(menuFont); // 设置菜单项字体
        exitItem.addActionListener(e -> System.exit(0));
        fileMenu.add(exitItem);
        menuBar.add(fileMenu);

        // 编辑菜单
        JMenu editMenu = new JMenu("编辑");
        editMenu.setFont(menuFont);
        JMenuItem cutItem = new JMenuItem("剪切");
        cutItem.setFont(menuFont);
        JMenuItem copyItem = new JMenuItem("复制");
        copyItem.setFont(menuFont);
        JMenuItem pasteItem = new JMenuItem("粘贴");
        pasteItem.setFont(menuFont);
        editMenu.add(cutItem);
        editMenu.add(copyItem);
        editMenu.add(pasteItem);
        menuBar.add(editMenu);

        // 帮助菜单
        JMenu helpMenu = new JMenu("帮助");
        helpMenu.setFont(menuFont);
        JMenuItem aboutItem = new JMenuItem("关于");
        aboutItem.setFont(menuFont);
        aboutItem.addActionListener(this::showAboutDialog);
        helpMenu.add(aboutItem);
        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        // 主面板(设置欢迎标签的中文字体)
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        JLabel welcomeLabel = new JLabel("欢迎使用系统," + currentUser.getFullName() + "!", SwingConstants.CENTER);
        // 设置支持中文的字体(黑体,加粗,24号字)
        welcomeLabel.setFont(new Font("SimHei", Font.BOLD, 24));
        mainPanel.add(welcomeLabel, BorderLayout.CENTER);

        add(mainPanel);

        setVisible(true);
    }

    private void showAboutDialog(ActionEvent e) {
        // 为对话框文本设置中文字体
        JLabel aboutLabel = new JLabel("<html>系统版本: 1.0<br>作者: DDD示例</html>");
        aboutLabel.setFont(new Font("SimHei", Font.PLAIN, 14));
        JOptionPane.showMessageDialog(this,
                aboutLabel,
                "关于",
                JOptionPane.INFORMATION_MESSAGE);
    }
}

  

posted @ 2025-10-24 23:25  ®Geovin Du Dream Park™  阅读(6)  评论(0)    收藏  举报