java 窗体,未测试

本文详细介绍了如何设计用户登录和注册界面,包括输入框、按钮的布局与响应逻辑,确保用户操作流畅与安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.file;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class DoWindow extends JFrame {

   
    private static final long serialVersionUID = 158951615131406887L;

    private JLabel userName = new JLabel("用户名:");
    private JTextField userNameField = new JTextField(10);
    private JLabel password = new JLabel("密码:");
    private JPasswordField passwordField = new JPasswordField(10);
    private JButton loginButton = new JButton("登录");
    private JButton register = new JButton("注册");
    private JPanel panel = new JPanel();

    public DoWindow(String name, Point p) {
        super(name);
        setLocation(p);

        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        panel.setLayout(layout);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1.0;
        layout.setConstraints(userName, constraints);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        layout.setConstraints(userNameField, constraints);
        constraints.gridwidth = GridBagConstraints.RELATIVE;
        layout.setConstraints(password, constraints);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        layout.setConstraints(passwordField, constraints);
        panel.add(userName);
        panel.add(userNameField);
        panel.add(password);
        panel.add(passwordField);
        panel.add(loginButton);
        panel.add(register);

        loginButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String username = userNameField.getText();
                char[] passwordStrAry = passwordField.getPassword();
                String password = passwordStrAry != null ? new String(
                        passwordStrAry) : null;
                if (username == null || password == null || "".equals(username)
                        || "".equals(password)) {
                    JOptionPane.showMessageDialog(null, "密码和用户名不能为空", "注意",
                            JOptionPane.ERROR_MESSAGE);
                    if (username == null || "".equals(username))
                        userNameField.grabFocus();
                    else
                        passwordField.grabFocus();
                    return;
                }
                boolean checkResult = UserManager.loginCheck(username, password);
                if (!checkResult) {
                    JOptionPane.showMessageDialog(null, "密码或用户名不正确", "注意",
                            JOptionPane.ERROR_MESSAGE);
                } else {
                    new MessageFrame("登录成功,欢迎光临!", getLocation());
                    dispose();
                }
            }
        });

        register.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new RegisterFrame("注册界面", getLocation());
                dispose();
            }
        });
        setLayout(new FlowLayout());
        add(panel);
        setSize(250, 130);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值