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

📄 login.java

📁 一个基于java的局域网聊天程序
💻 JAVA
字号:
import java.awt.Color;
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;
import javax.swing.border.LineBorder;
import java.sql.*;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
public class Login extends JFrame {

	private JPasswordField passwordField;
	private JTextField textField;
	public Login() {
		super();
		pack();
		getContentPane().setBackground(new Color(240, 248, 255));
		setBackground(Color.WHITE);
		setResizable(false);
		getContentPane().setLayout(null);
		setTitle("用户登录");
		setSize(250, 200);
		this.setLocation(400, 300);
		
		final JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(Color.PINK, 1, false));
		panel.setBackground(Color.CYAN);
		panel.setLayout(null);
		panel.setBounds(25, 15, 189, 100);
		getContentPane().add(panel);

		final JLabel label = new JLabel();
		label.setText("帐   号");
		label.setBounds(10, 21, 57, 18);
		panel.add(label);

		final JLabel label_1 = new JLabel();
		label_1.setText("密   码");
		label_1.setBounds(10, 53, 57, 18);
		panel.add(label_1);

		textField = new JTextField();
		textField.setBounds(73, 19, 106, 22);
		panel.add(textField);

		passwordField = new JPasswordField();
		passwordField.setEchoChar('*');
		passwordField.setBounds(73, 51, 106, 22);
		panel.add(passwordField);

		final JButton button = new JButton();
		button.addActionListener(new Tologin());
	
		button.setText("登录");
		button.setBounds(43, 128, 64, 28);
		getContentPane().add(button);

		final JButton button_1 = new JButton();
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				new Register();
			}
		});
		button_1.setText("注册");
		button_1.setBounds(132, 128, 64, 28);
		getContentPane().add(button_1);
		
		setVisible(true);
	}
	class Tologin implements ActionListener{//连接数据库
		public void actionPerformed(ActionEvent e) {
			String account=textField.getText();
			String pwd=new String(passwordField.getPassword());
			try {
				Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			} catch (ClassNotFoundException e2) {
				e2.printStackTrace();
			} 
			String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xiajing"; 
			String user="sa"; 
			String password=""; 
			try {
				Connection con= DriverManager.getConnection(url,user,password);
				Statement state=con.createStatement();
				String sql = "select * from register where 帐号='" + account + "' and 密码='" +pwd +"'";
				System.out.println(sql);
				ResultSet rs=state.executeQuery(sql);
				if(rs.next()) {
					System.out.println("登录成功");
					dispose();
					new ChatMain(account);//将用户名传入到XX主面板
				}
				else JOptionPane.showMessageDialog(null,"帐号或密码错误","警告",JOptionPane.INFORMATION_MESSAGE);
			} catch (SQLException e1) {
				e1.printStackTrace();
			} 
		}
		
	}
	public static void main(String[] args) {
		new Login();
	}

}

⌨️ 快捷键说明

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