⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 login.java

📁 联系方式 lovely2000@sina.com
💻 JAVA
字号:
/*
 * Login.java
 *美化后的HappyChat登入界面
 */

package happychat;
import happychat.jdbc_login;
import java.awt.*;
import java.sql.SQLException;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * HAPPYCHAT聊天系统登入界面
 * @author 范方青
 */
public class Login extends JFrame implements ActionListener{
 
    JPanel pnlLogin;
    JButton btnLogin,btnRegister,btnExit;
    JLabel lblServer,lblUserName,lblPassword,lblLogo;
    JTextField txtUserName,txtServer;
    JPasswordField pwdPassword;
    //用于将窗口定位
    Dimension scrnsize;
    Toolkit toolkit=Toolkit.getDefaultToolkit();
    /** Creates a new instance of Longin */

    public Login() {
        super("登入[HappyChat]聊天室");//
        pnlLogin = new JPanel();
        this.getContentPane().add(pnlLogin);
        //初始化组件
        lblServer = new JLabel(" 服务器(S):");
        lblUserName = new JLabel(" 用户名(U):");
        lblPassword = new JLabel(" 口  令(P):");
        txtServer = new JTextField();
        txtServer.setText("127.0.0.1");
        txtUserName = new JTextField(20);
        pwdPassword = new JPasswordField(20);
        btnLogin = new JButton("登入(L)");
        btnLogin.setToolTipText("登入到服务器");
        btnLogin.setMnemonic('L');
        btnRegister = new JButton("注册(R)");
        btnRegister.setToolTipText("注册新用户");
        btnRegister.setMnemonic('R');
        btnExit = new JButton("退出(X)");
        btnExit.setToolTipText("退出系统");
        btnExit.setMnemonic('X');
        //设置面板背景颜色
        pnlLogin.setLayout(null);
        pnlLogin.setBackground(new Color(52,130,203));
        lblServer.setBounds(50,100,100,30);
        txtServer.setBounds(150,105,120,20);
        lblUserName.setBounds(50,130,100,30);
        txtUserName.setBounds(150,135,120,20);
        lblPassword.setBounds(50,160,100,30);
        pwdPassword.setBounds(150,165,120,20);
        btnLogin.setBounds(50,200,80,25);
        btnRegister.setBounds(130,200,80,25);
        btnExit.setBounds(210,200,80,25);
        
        //设置组件字体
        Font fontstr = new Font("宋体",Font.PLAIN,12);
        lblServer.setFont(fontstr);
        txtServer.setFont(fontstr); 
        lblUserName.setFont(fontstr); 
        txtUserName.setFont(fontstr); 
        lblPassword.setFont(fontstr); 
        pwdPassword.setFont(fontstr); 
        btnLogin.setFont(fontstr); 
        btnRegister.setFont(fontstr); 
        btnExit.setFont(fontstr); 
        //设置组件背景颜色
        lblUserName.setForeground(Color.BLACK);
        lblPassword.setForeground(Color.BLACK);
        btnLogin.setBackground(Color.ORANGE);
        btnRegister.setBackground(Color.ORANGE);
        btnExit.setBackground(Color.ORANGE);
        //设置背景图片
        Icon logo1 = new ImageIcon("D:\\MyCode\\java\\HappyChat\\build\\classes\\happychat\\login.gif");
        //JLabel.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\范方青\\桌面\\logo2.jpg"));
        //lblLogo =jLabel.setText("JLabel");
        lblLogo = new JLabel(logo1);
        lblLogo.setBounds(0,0,340,100);
        pnlLogin.add(lblLogo);
        //添加组件到面板
        pnlLogin.add(lblLogo);
        pnlLogin.add(lblServer);
        pnlLogin.add(txtServer);
        pnlLogin.add(lblUserName);
        pnlLogin.add(txtUserName);
        pnlLogin.add(lblPassword);
        pnlLogin.add(pwdPassword);
        pnlLogin.add(btnLogin);
        btnLogin.addActionListener(this);
        pnlLogin.add(btnRegister);
        btnRegister.addActionListener(this);
        pnlLogin.add(btnExit);
        btnExit.setActionCommand("quit");
        btnExit.addActionListener(this);
     
        //设置登入窗口
        setResizable(false);
        setSize(340,260);
        setVisible(true);
        scrnsize = toolkit.getScreenSize();
        setLocation(scrnsize.width/2-this.getWidth()/2,scrnsize.height/2-this.getHeight()/2);
        Image img = toolkit.getImage(this.getClass().getResource("1.jpg").toString().substring(6));
        this.setIconImage(img);
    }
    public void actionPerformed(ActionEvent e) {        
        if(e.getSource() == btnLogin){
            //判断用户密码是否为空**与服务器连接**数据库查询账户是否可用**************************
            if(txtUserName.getText().equals("")||pwdPassword.getPassword().length==0)
            {               
                JOptionPane.showMessageDialog(null,"用户、密码不能为空");                                   
            }
            else{
                //登入聊天界面
                //strServerIP = txtServer.getText();
                // Login();
                try {
                String n = this.txtUserName.getText();
                String p = String.valueOf(this.pwdPassword.getPassword());
               // DBUtility p11 = new DBUtility();
                //System.out.println(p11.x(n,p));
                DataBase database = new DataBase();    
                //jdbc_login p11 = new jdbc_login();
                    if(database.isLogin(n,p)){
                        //System.out.println("1");
                        JFrame p2 = new chatdemo();
                        p2.setVisible(true);
                        this.setVisible(false);
                        //database.closeConnection();
                    }
                    else{
                        //System.out.println("0");
                        JOptionPane.showMessageDialog(null,"用户不存在、或密码错误");
                    }
                }catch (Exception exx){
                    exx.printStackTrace();
                }               
            }
        }
        if(e.getSource() == btnRegister){
            //strServer IP = txtServer.getText();
           // this.dispose();
            JFrame p1 = new Register_Customer();
           // p1.getContentPane().setBackground(new Color(52,130,203));
            p1.setVisible(true);
            this.setVisible(false);
            //进入注册界面****************************
        }
        if(e.getSource() == btnExit){
            System.exit(0);
        }
        
    }
 
    public static void main(String []args){
        new Login();        
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -