/**
* LogIn.java
* Created on 08,07,2004
* @author mhl
* @version v0.1
*/
package com.jlu.mhl;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
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.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.MatteBorder;
import javax.swing.plaf.metal.MetalLookAndFeel;
public class LogIn extends JFrame
{
private JLabel UserJLabel,PassJLabel;
private JTextField UserNameJTField;
private JPasswordField PasswordJPField;
private JButton OkJButton,ExitJButton;
private JPanel UserNameJPanel,PasswordJPanel,ButtonJPanel;
private Action ExitAction,OkAction;
private Connection con;
private PreparedStatement sqlFind;
private ResultSet rs;
private String s_driver,s_url,s_User,s_Pass;
private static boolean JPBSS=true;
private ImageIcon icon_LogIn;
//init splash screen
private SplashScreen splashScreen;
private MdiMain newMdi;
private PropertiesRead newA;
private AllInformation conInfo;
public LogIn() {
super("LogIn...");
// init component.
InitialComponent();
// read attribute file
newA.Read();
// connect database
connect();
//display the splash screen
showJProgressBarSplashScreen();
showSplashScreen();
// WaitSplashScreen();
// set image for login frame.
icon_LogIn= new ImageIcon(getClass().getResource("./images/surfing.gif"));
setIconImage(icon_LogIn.getImage());
setResizable(false);
//Enter,Esc event
UserNameJTField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyChar()==KeyEvent.VK_ENTER) {
s_Pass=PasswordJPField.getText();
s_User=UserNameJTField.getText();
checkPopedom();
} else if (e.getKeyChar()==KeyEvent.VK_ESCAPE) {
int option=JOptionPane.showConfirmDialog(null,
" yes or no",
"Question", JOptionPane.YES_NO_OPTION);
// int option=MySetDialog.mySetDialog("e・ス・ス",20,"・スヌキ・ス・スヒウ・ス・ス・ス・?","マオヘウムッ・ス・ス");
if(option==0) {
System.exit(0);
}
}
}
});
//
PasswordJPField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
s_Pass=(String)PasswordJPField.getText();
s_User=UserNameJTField.getText();
checkPopedom();
//e.VK_ESCAPE==KeyEvent.VK_ESCAPE
} else if(e.getKeyChar()==KeyEvent.VK_ESCAPE) {
int option=JOptionPane.showConfirmDialog(null,
"yes or no",
"マオヘウQuestion", JOptionPane.YES_NO_OPTION);
if(option==0) {
System.exit(0);
}
}
}
});
// select all UserNameJTfield
UserNameJTField.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
UserNameJTField.selectAll();
}
});
// select all the PasswordJPField
PasswordJPField.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
PasswordJPField.select(0,20);
}
});
//
OkAction=new AbstractAction() {
public void actionPerformed(ActionEvent event) {
s_Pass=PasswordJPField.getText();
s_User=UserNameJTField.getText();
checkPopedom();
}
};
OkAction.putValue(Action.NAME,"Enter");
OkAction.putValue(Action.SMALL_ICON,
new ImageIcon(getClass().getResource("./images/ok.gif")));
OkAction.putValue(Action.SHORT_DESCRIPTION,"Enter");
OkAction.putValue(Action.MNEMONIC_KEY,new Integer('O'));
OkJButton=new JButton(OkAction);
//
OkJButton.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.VK_ENTER==KeyEvent.VK_ENTER) {
s_Pass=PasswordJPField.getText();
s_User=UserNameJTField.getText();
checkPopedom();
}
}
});
//
ExitAction=new AbstractAction() {
public void actionPerformed(ActionEvent event) {
int option=JOptionPane.showConfirmDialog(null,
"Are you sure to exit ?",
"Question", JOptionPane.YES_NO_OPTION);
if(option==0) {
System.exit(0);
}
}
};//end AbstracAction
ExitAction.putValue(Action.NAME,"Exit");
ExitAction.putValue(Action.SMALL_ICON,
new ImageIcon(getClass().getResource("./images/bb.gif")));
ExitAction.putValue(Action.SHORT_DESCRIPTION,"Exit");
ExitAction.putValue(Action.MNEMONIC_KEY,new Integer('E'));
ExitJButton=new JButton(ExitAction);
//
ExitJButton.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.VK_ESCAPE==KeyEvent.VK_ESCAPE) {
System.exit(1);
}
}
});
//
UserNameJPanel.add(UserJLabel);
UserNameJPanel.add(UserNameJTField);
PasswordJPanel.add(PassJLabel);
PasswordJPanel.add(PasswordJPField);
ButtonJPanel.add(OkJButton);
ButtonJPanel.add(ExitJButton);
//
Container c=getContentPane();
c.add(UserNameJPanel,BorderLayout.NORTH);
c.add(PasswordJPanel);
c.add(ButtonJPanel,BorderLayout.SOUTH);
setSize(240,130);
CenterOnScreen();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//End construct method
/**
* init login jlable,jtfiled,jpanel.
*/
public void InitialComponent() {
UserJLabel=new JLabel("UserName:");
PassJLabel=new JLabel("PassWord:");
UserNameJTField=new JTextField(10);
UserNameJTField.setText(System.getProperty("user.name"));
PasswordJPField=new JPasswordField(10);
UserNameJPanel=new JPanel();
PasswordJPanel=new JPanel();
ButtonJPanel=new JPanel();
newA=new PropertiesRead();
}
/**
* Connect database
*/
public void connect() {
try {
Class.forName(newA.s_Db_Driver);
//con=DriverManager.getConnection(newA.s_Db_Url);
con=DriverManager.getConnection(newA.s_Db_Url,newA.s_Db_LogName,newA.s_Db_LogPasswrd);
} catch (SQLException sqlE) {
JOptionPane.showMessageDialog(null,sqlE.getMessage()+"User or Pass Error",
"Info",JOptionPane.ERROR_MESSAGE);
showConInfo();
} catch (ClassNotFoundException cnfE) {
JOptionPane.showMessageDialog(null,cnfE.getMessage(),
"ClassNotFound",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
/**
* select
*/
public void checkPopedom() {
try {
sqlFind=con.prepareStatement("select UserFName,UserLName from app_user "
+ "where UserFName = ? and UserLName = ? ");
sqlFind.setString(1,s_User);
sqlFind.setString(2,s_Pass);
rs=sqlFind.executeQuery();
if(!rs.next()) {
JOptionPane.showMessageDialog(null,
"no result !","Error",JOptionPane.ERROR_MESSAGE);
} else {
setVisible(false);
//init MDI frame
newMdi=new MdiMain();
sqlFind.close();
con.close();
dispose();
}
} catch (SQLException sqlE) {
JOptionPane.showMessageDialog(null,sqlE.getMessage(),
"Find Error",JOptionPane.ERROR_MESSAGE);
}
}
/**
* show JProgressBarSplashScreen
*/
public void showJProgressBarSplashScreen() {
JProgressBarSplashScreen splashScreen;
Icon logoIcon = new ImageIcon(getClass().getResource("./images/logo1.jpg"));
JLabel logoLabel = new JLabel(logoIcon);
logoLabel.setBackground(Color.gray);
logoLabel.setOpaque(true);
logoLabel.setBorder(new MatteBorder(5,5,5,5, Color.gray));
logoLabel.setOpaque(true);
splashScreen = new JProgressBarSplashScreen(logoLabel);
splashScreen.iterate();
}
/**
* SplashScreen
*/
public void showSplashScreen() {
Icon logoIcon = new ImageIcon(getClass().getResource("./images/logo.jpg"));
JLabel logoLabel = new JLabel(logoIcon);
logoLabel.setBackground(Color.blue);
logoLabel.setOpaque(true);
logoLabel.setBorder(new MatteBorder(5,5,5,5, Color.gray));
logoLabel.setOpaque(true);
splashScreen = new SplashScreen(logoLabel);
splashScreen.showSplash();
}
/**
* wait splashScreen 10s?
*/
public void WaitSplashScreen() {
while (splashScreen.isVisible()) {
try {
Thread.sleep( 10 );
} catch ( InterruptedException interruptedException ) {
interruptedException.printStackTrace();
}
}
}
/**
* center the login frame
*/
public void CenterOnScreen() {
Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
int width = getSize().width;
int height = getSize().height;
int x = (screenDimension.width - width)/2;
int y = (screenDimension.height - height)/2;
setBounds(x,y,width,height);
}
public void showConInfo() {
conInfo=new AllInformation();
}
/**
* main
*/
public static void main(String args[])
{
UIManager.put("swing.boldMetal", Boolean.FALSE);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame.setDefaultLookAndFeelDecorated(true);
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground","true");
try
{
UIManager.setLookAndFeel(new MetalLookAndFeel());
}
catch(UnsupportedLookAndFeelException e)
{
System.out.println ("Metal Look & Feel not supported on this platform. /nProgram Terminated"); System.exit(0);
}
do
{
JPBSS=false;
LogIn login=new LogIn();
login.show();
}
while(JPBSS=false);
}
}
其中connect() 方法是连接数据库的。