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

📄 loginwindow.java

📁 用hibernata连接数据库的java聊天室 有服务器端可踢人
💻 JAVA
字号:
package cr;

import hibernatedao.Loginuser;
import hibernatedao.LoginuserDAO;
import hibernatedao.LoginuserId;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

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.JTextField;
import javax.swing.UIManager;

class LoginWindow extends JDialog implements ActionListener {
	JPanel p1 = new JPanel(); //定义并建立面板

	JPanel p2 = new JPanel();

	JPanel p3 = new JPanel();

	JPanel p4 = new JPanel();

	JPanel p5 = new JPanel();

	JTextField txtUserName = new JTextField(15); //用户名文本框

	JPasswordField txtPwd = new JPasswordField(15);//密码框

	JButton ok = new JButton("确定");

	JButton cancel = new JButton("取消");

	public LoginWindow() {
		setModal(true); //设置模态
		setBackground(Color.LIGHT_GRAY);//设置背景色
		Container contentPane = this.getContentPane();//取出内容面板
		contentPane.setLayout(new GridLayout(5, 1)); //设置布局为5行1列
		p2.add(new JLabel("用户名:"));
		p2.add(txtUserName); //将组件添加到中间容器
		p3.add(new JLabel("密     码:"));
		p3.add(txtPwd);
		p4.add(ok);
		p4.add(cancel);
		ok.addActionListener(this);
		cancel.addActionListener(this);
		txtUserName.addActionListener(this);
		txtPwd.addActionListener(this);
		contentPane.add(p1); //将面板添加到内容面板
		contentPane.add(p2);
		contentPane.add(p3);
		contentPane.add(p4);
		contentPane.add(p5);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置自动关闭窗口
		setSize(300, 220);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screen.width - 300) / 2, (screen.height - 220) / 2);
		setTitle("登录窗口");
		setResizable(false); //不让用户改变窗口大小
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		//	private Statement stmt=null;
		//	private ResultSet rs=null;
		//	private final String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Loginin";
		//	DBConn dbconn = new DBConn();
		//	Connection conn=dbconn.getConn();
		//	ResultSet rs = dbconn.execQuery("select name from login");
		//	ResultSet rp = dbconn.execQuery("select password from login");

		if (e.getSource() == ok || e.getSource() == txtPwd) //单击确定按钮后
		{
			LoginuserDAO dao=new LoginuserDAO();
			LoginuserId id=new LoginuserId(txtUserName.getText().trim(),txtPwd.getText().trim());
			if(null==dao.findById(id)){
			}
			dao.save(new Loginuser(id));
			String temp;
			if(null!=(temp=dao.findById(id).getId().getUsernickname())&&!"".equals(temp)){
				dispose(); //关闭登录窗口

				new ClientMain(); //调出主窗口
			}
			else {
				JOptionPane.showMessageDialog(null, "用户名或密码错误!");
				txtUserName.requestFocus(); //设置焦点
				txtUserName.setSelectionStart(0); //设置选中文本开始位置
				txtUserName.setSelectionEnd(txtUserName.getText().length());
			}
		} else if (e.getSource() == cancel) //单击取消
		{
			dispose();
			System.exit(0);
		} else if (e.getSource() == txtUserName) //在用户名文本框按回车移动焦点到密码框
		{
			txtPwd.requestFocus();
		}
	}

	public static void main(String[] args) {
		JDialog.setDefaultLookAndFeelDecorated(true);
		Font font = new Font("JFrame", Font.PLAIN, 14);
		Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			if (UIManager.get(key) instanceof Font)
				UIManager.put(key, font);
		}
		new LoginWindow();
	}
}

⌨️ 快捷键说明

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