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

📄 loginpanel.java

📁 java编的销售系统
💻 JAVA
字号:



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Menu;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class LoginPanel extends JDialog { 

	private static final long serialVersionUID = 1L; 

	private JLabel l_Id = new JLabel("NAME", JLabel.CENTER); 
	private JLabel l_pw = new JLabel("KEY", JLabel.CENTER); 
	private JTextField t_Id = new JTextField(10); 
	private JPasswordField t_pw = new JPasswordField(10); 
	private JButton btnLogin; 
	private JButton btnClose; 

	public LoginPanel() { 
	super(); 
	setResizable(false); 
	getContentPane().setBackground(new Color(225, 225, 225)); 
	getContentPane().setLayout(null); 
	initialize(); 
	} 

	protected void initialize() { 
	setTitle("Welcome to our pizza store!"); 

	l_Id.setBounds(48, 43, 53, 25); 
	t_Id.setBounds(110, 43, 150, 25); 
	l_pw.setBounds(48, 93, 53, 25); 
	t_pw.setBounds(110, 93, 150, 25); 

	getContentPane().add(l_Id); 
	getContentPane().add(l_pw); 
	getContentPane().add(t_Id); 
	getContentPane().add(t_pw); 

	btnLogin = new JButton(); 

	btnLogin.setText("Login"); 
	btnLogin.setBounds(70, 142, 85, 28); 
	btnLogin.addActionListener(new ActionListener() { 
	public void actionPerformed(ActionEvent e) { 
	addBtnLoginActionListener(); 
	} 
	}); 

	getContentPane().add(btnLogin); 

	btnClose = new JButton(); 

	btnClose.setText("close"); 
	btnClose.setBounds(175, 142, 85, 28); 
	btnClose.addActionListener(new ActionListener() { 
	public void actionPerformed(ActionEvent e) { 
	dispose(); 
	System.exit(-1); 
	} 
	}); 
	getContentPane().add(btnClose); 
	} 

	private void addBtnLoginActionListener() { 
	String user = t_Id.getText(); 
	String password =t_pw.getText(); 

	if (user.equals("")) { 
	JOptionPane.showMessageDialog(this, "user name can't be null!", "Caution", 
	JOptionPane.WARNING_MESSAGE); 
	return; 
	} 

	if (password.equals("")) { 
	JOptionPane.showMessageDialog(this, "password can not be null!", "Caution", 
	JOptionPane.WARNING_MESSAGE); 
	return; 
	} 
	try{
	      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	      }
	      catch(ClassNotFoundException ex) {
	      ex.printStackTrace();
	      System.exit(-1);
	      }    
	try{ 
		 Connection con = DriverManager.getConnection("jdbc:odbc:JAVA","123","123");
	     Statement stmt=con.createStatement(); 
	     String sql = "select * FROM login WHERE name = '" + user + "' and          password = '" + password + "'"; 
	  ResultSet rs=stmt.executeQuery(sql);



//	 TODO:数据校验 success = executeQuery(sql); 

	if (rs.next()) { 
//	 TODO: 如果数据校验成功 显示主界面 并关闭登录界面 

	JOptionPane.showMessageDialog(this, "successful", "hint", 
	JOptionPane.INFORMATION_MESSAGE); 
	this.dispose(); 
	Menu frame=new Menu();
	
	
	BakeJobFrame bakeJobFrame = new BakeJobFrame();
	PizzaStore pizzaStore = new PizzaStore();
	PizzaStoreFrame pizzaMaker = new PizzaStoreFrame(bakeJobFrame, pizzaStore);
	pizzaMaker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
    pizzaMaker.show(); 
		setVisible(false);
	
	} else { 
	JOptionPane.showMessageDialog(this, "your name or password is wrong!!", "warn", 
	JOptionPane.WARNING_MESSAGE); 
	t_pw.requestFocus(); // 密码框选中 
	} 
	}
	catch(SQLException ex){
	   ex.printStackTrace(); 
	    }
	} 
	public Dimension getPreferredSize() { 
	return new Dimension(320, 170); 
	} 

	public void show() { 
	Toolkit tk = Toolkit.getDefaultToolkit(); 
	Dimension screen = tk.getScreenSize(); 
	Dimension d = getSize(); 
	this.setLocation((screen.width - d.width) / 2, 
	(screen.height - d.height) / 2); 

//	 输入密码后回车相当于点击了登录按钮 
	getRootPane().setDefaultButton(btnLogin); 
	t_pw.requestFocus(); 
	setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
	setSize(300, 220); 
	super.show(); 
	} 
	
	

	
	} 



⌨️ 快捷键说明

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