swing界面设计之登录注册界面


开发环境:Eclipse Neon.3 Release (4.6.3)
我用的为javaee版本的,为了便于设计界面,需要安装windowbuilder插件,可以直接在marketplace里搜索到。
说明:参考凯哥视频同步学习的

界面效果

登录界面
这里写图片描述


注册界面
这里写图片描述

关键技术

该项目用到了java swing外观插件
用到了图片(点击src右键文件夹取名res并复制图片login.jpg)

源码

com.lsh.view.LoginDialog

package com.lsh.view;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ImageIcon;
import javax.swing.border.TitledBorder;

import com.lsh.util.ImageScale;

@SuppressWarnings("serial")
public class LoginDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;

    private static final int DIALOG_WIDTH=414;
    private static final int DIALOG_HEIGHT=340;
    private static final int DIALOG_HEIGHT_EXTEND=573;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try
        {
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        }
        catch(Exception e)
        {
            //TODO exception
            System.out.println("加载炫彩皮肤失败!");
        }
        try {
            LoginDialog dialog = new LoginDialog();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public LoginDialog() {
        setAlwaysOnTop(true);
        setResizable(false);
        setBounds(400, 100, DIALOG_WIDTH,DIALOG_HEIGHT);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);

        //设置居中
        //setLocation(WindowXY.getXY(LoginDialog.this.getSize()));

        JButton btnNewButton = new JButton("注 册");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(LoginDialog.this.getHeight()==DIALOG_HEIGHT_EXTEND){
                    LoginDialog.this.setSize(DIALOG_WIDTH,DIALOG_HEIGHT);
                }
                else{
                    LoginDialog.this.setSize(DIALOG_WIDTH,DIALOG_HEIGHT_EXTEND);
                }
            }
        });
        btnNewButton.setBounds(53, 224, 93, 23);
        contentPanel.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("登 录");
        btnNewButton_1.setBounds(190, 224, 93, 23);
        contentPanel.add(btnNewButton_1);

        textField = new JTextField();
        textField.setBounds(133, 147, 150, 25);
        contentPanel.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(133, 182, 150, 25);
        contentPanel.add(textField_1);
        textField_1.setColumns(10);

        JLabel lblNewLabel = new JLabel("邮 箱");
        lblNewLabel.setBounds(53, 151, 54, 15);
        contentPanel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("密 码");
        lblNewLabel_1.setBounds(53, 194, 54, 15);
        contentPanel.add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("New label");     
        lblNewLabel_2.setBounds(0, 0, 360, 136);
        ImageIcon icon=new ImageIcon(LoginDialog.class.getResource("/res/login.jpg"));
        icon=ImageScale.getImage(icon, lblNewLabel_2.getWidth(), lblNewLabel_2.getHeight());
        lblNewLabel_2.setIcon((icon));
        contentPanel.add(lblNewLabel_2);

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "\u6CE8\u518C\u7528\u6237", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel.setBounds(12, 259, 336, 221);
        contentPanel.add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel_3 = new JLabel("邮 箱");
        lblNewLabel_3.setBounds(41, 29, 55, 18);
        panel.add(lblNewLabel_3);

        JLabel lblNewLabel_4 = new JLabel("验证码");
        lblNewLabel_4.setBounds(41, 85, 55, 18);
        panel.add(lblNewLabel_4);

        JLabel lblNewLabel_5 = new JLabel("密 码");
        lblNewLabel_5.setBounds(41, 115, 55, 18);
        panel.add(lblNewLabel_5);

        JLabel label = new JLabel("确认密码");
        label.setBounds(41, 145, 55, 18);
        panel.add(label);

        textField_2 = new JTextField();
        textField_2.setBounds(123, 22, 150, 25);
        panel.add(textField_2);
        textField_2.setColumns(10);

        textField_3 = new JTextField();
        textField_3.setBounds(123, 80, 150, 25);
        panel.add(textField_3);
        textField_3.setColumns(10);

        textField_4 = new JTextField();
        textField_4.setBounds(123, 113, 150, 25);
        panel.add(textField_4);
        textField_4.setColumns(10);

        textField_5 = new JTextField();
        textField_5.setBounds(123, 145, 150, 25);
        panel.add(textField_5);
        textField_5.setColumns(10);

        JButton btnNewButton_2 = new JButton("发送验证码");
        btnNewButton_2.setBounds(123, 52, 83, 23);
        panel.add(btnNewButton_2);

        JButton btnNewButton_3 = new JButton("取 消");
        btnNewButton_3.setBounds(51, 182, 83, 27);
        panel.add(btnNewButton_3);

        JButton btnNewButton_4 = new JButton("确 认");
        btnNewButton_4.setBounds(190, 182, 83, 27);
        panel.add(btnNewButton_4);
    }
}



工具类  com.lsh.util.ImageScale

package com.lsh.util;

import java.awt.Image;

import javax.swing.ImageIcon;

public class ImageScale {
    public static ImageIcon getImage(ImageIcon icon,int width,int height){
        Image image=icon.getImage().getScaledInstance(width, height,Image.SCALE_REPLICATE);
        icon.setImage(image);
        return icon;
    }
}

工具类 com.lsh.util.WindowXY

package com.lsh.util;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

public class WindowXY {
    public static Point getXY(int w,int h){
        Toolkit toolkit=Toolkit.getDefaultToolkit();
        int width=toolkit.getScreenSize().width;
        int height=toolkit.getScreenSize().height;
        return new Point((width-w)/2,(height-h)/2);
    }
    public static Point getXY(Dimension dimension) {
        return getXY(dimension.width, dimension.height);
    }
}
package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }
以下是Java Swing登录注册界面的设计与代码示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LoginAndRegister extends JFrame implements ActionListener { // 登录界面组件 JLabel loginLabel = new JLabel("用户名:"); JTextField loginText = new JTextField(20); JLabel passwordLabel = new JLabel("密码:"); JPasswordField passwordText = new JPasswordField(20); JButton loginButton = new JButton("登录"); JButton registerButton = new JButton("注册"); JPanel loginPanel = new JPanel(); // 注册界面组件 JLabel registerLabel = new JLabel("请填写注册信息:"); JLabel registerUserLabel = new JLabel("用户名:"); JTextField registerUserText = new JTextField(20); JLabel registerPasswordLabel = new JLabel("密码:"); JPasswordField registerPasswordText = new JPasswordField(20); JLabel registerConfirmPasswordLabel = new JLabel("确认密码:"); JPasswordField registerConfirmPasswordText = new JPasswordField(20); JButton confirmButton = new JButton("确认"); JButton cancelButton = new JButton("取消"); JPanel registerPanel = new JPanel(); public LoginAndRegister() { super("登录/注册"); // 设置组件大小和布局 setSize(400, 250); loginPanel.setLayout(new GridLayout(3, 2)); registerPanel.setLayout(new GridLayout(5, 2)); // 添加登录界面组件 loginPanel.add(loginLabel); loginPanel.add(loginText); loginPanel.add(passwordLabel); loginPanel.add(passwordText); loginPanel.add(loginButton); loginPanel.add(registerButton); add(loginPanel); // 添加注册界面组件 registerPanel.add(registerLabel); registerPanel.add(new JLabel("")); registerPanel.add(registerUserLabel); registerPanel.add(registerUserText); registerPanel.add(registerPasswordLabel); registerPanel.add(registerPasswordText); registerPanel.add(registerConfirmPasswordLabel); registerPanel.add(registerConfirmPasswordText); registerPanel.add(confirmButton); registerPanel.add(cancelButton); // 监听按钮事件 loginButton.addActionListener(this); registerButton.addActionListener(this); confirmButton.addActionListener(this); cancelButton.addActionListener(this); // 设置窗口居中显示 setLocationRelativeTo(null); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { // 处理登录按钮事件 String username = loginText.getText(); String password = new String(passwordText.getPassword()); // TODO: 处理登录逻辑 } else if (e.getSource() == registerButton) { // 切换到注册界面 getContentPane().remove(loginPanel); getContentPane().add(registerPanel); getContentPane().validate(); } else if (e.getSource() == confirmButton) { // 处理注册按钮事件 String username = registerUserText.getText(); String password = new String(registerPasswordText.getPassword()); String confirmPassword = new String(registerConfirmPasswordText.getPassword()); // TODO: 处理注册逻辑 } else if (e.getSource() == cancelButton) { // 切换到登录界面 getContentPane().remove(registerPanel); getContentPane().add(loginPanel); getContentPane().validate(); } } public static void main(String[] args) { new LoginAndRegister(); } } ``` 以上代码中,`LoginAndRegister` 类继承自 `JFrame`,实现了 `ActionListener` 接口,用于监听按钮事件。登录界面和注册界面都用了 `JPanel` 来布局,然后根据按钮事件来切换界面。登录按钮和注册按钮的事件处理逻辑需要根据实际情况来编写。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值