?? logframe.java
字號:
/**********************************
* FileName:LogFrame.java
* Function:顯示登錄窗口界面
* Time:2005 1.7
**********************************/
import java.awt .*;
import java.awt.event.*;
import java.sql.*;
class LogFrame extends Frame implements ActionListener ,TextListener
{
Label userName = new Label ("口令");
Label password = new Label ("密碼");
Label lbName =new Label (" ");
Label lbLog = new Label (" ");
TextField tfName = new TextField (15);
TextField tfPW = new TextField(15);
Button btnConfirm = new Button ("確 定");
Button btnCancel = new Button ("取 消");
Button btnAbout = new Button("簡 介");
Dialog MegDlg;
Button btnY;
private int login = 0;
private Connection loginconnection;
private Statement loginstatement;
private ResultSet loginresultSet;
LogFrame()
{
super("登錄窗口");
GridBagLayout gbLayout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints ();
setLayout(gbLayout);
Color MyColor = new Color (0,200,255);
this.setBackground (MyColor);
gbc.gridx=2;gbc.gridy =2;
gbc.gridwidth =1;gbc.gridheight =1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor =GridBagConstraints.CENTER ;
gbc.weightx = 1; gbc.weighty = 0;
gbc.insets = new Insets (2,5,1,5);
gbLayout.setConstraints (userName,gbc);
add(userName);
gbc.gridx=3;gbc.gridy =2;
gbc.gridwidth =1;gbc.gridheight =1;
gbc.fill=GridBagConstraints.NONE ;
gbc.anchor =GridBagConstraints.WEST ;
gbc.weightx = 0; gbc.weighty = 0;
gbc.insets = new Insets (2,5,1,5);
gbLayout.setConstraints (tfName,gbc);
tfName.addTextListener (this);
add(tfName);
gbc.gridx=3;gbc.gridy =3;
gbc.gridwidth =1;gbc.gridheight =1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor =GridBagConstraints.WEST ;
gbc.weightx = 1; gbc.weighty = 0;
gbc.insets = new Insets (2,5,1,5);
gbLayout.setConstraints (lbName,gbc);
add(lbName);
gbc.gridx=2;gbc.gridy =5;
gbc.gridwidth =1;gbc.gridheight =1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor =GridBagConstraints.CENTER ;
gbc.weightx = 1; gbc.weighty = 0;
gbc.insets = new Insets (2,5,1,5);
gbLayout.setConstraints (password,gbc);
add(password);
gbc.gridx=3;gbc.gridy =5;
gbc.gridwidth =1;gbc.gridheight =1;
gbc.fill=GridBagConstraints.NONE ;
gbc.anchor =GridBagConstraints.WEST ;
gbc.weightx =0; gbc.weighty = 0;
gbc.insets = new Insets (2,5,1,5);
gbLayout.setConstraints (tfPW,gbc);
add(tfPW);
tfPW.addTextListener(this);
gbc.gridx=3;gbc.gridy =6;
gbc.gridwidth =1;gbc.gridheight =1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor =GridBagConstraints.WEST ;
gbc.weightx = 1; gbc.weighty = 0;
gbc.insets = new Insets (2,5,1,5);
gbLayout.setConstraints (lbLog,gbc);
add(lbLog);
gbc.gridx=2;gbc.gridy =7;
gbc.gridwidth =1;gbc.gridheight =1;
gbc.fill=GridBagConstraints.NONE ;
gbc.anchor =GridBagConstraints.EAST;
gbc.weightx = 1; gbc.weighty = 0;
gbc.insets = new Insets (2,5,1,5);
gbLayout.setConstraints (btnConfirm,gbc);
btnConfirm.addActionListener(this);
add(btnConfirm);
gbc.gridx=3;gbc.gridy =7;
gbc.gridwidth =1;gbc.gridheight =1;
gbc.fill=GridBagConstraints.NONE ;
gbc.anchor =GridBagConstraints.WEST ;
gbc.weightx =1; gbc.weighty = 0;
gbc.insets = new Insets (2,5,1,5);
gbLayout.setConstraints (btnCancel,gbc);
btnCancel.addActionListener (this);
add(btnCancel);
gbc.gridx=3;gbc.gridy =7;
gbc.gridwidth =1;gbc.gridheight =1;
gbc.fill=GridBagConstraints.NONE ;
gbc.anchor =GridBagConstraints.CENTER;
gbc.weightx = 1; gbc.weighty = 0;
gbc.insets = new Insets (2,5,1,5);
gbLayout.setConstraints (btnAbout,gbc);
btnAbout.addActionListener (this);
add(btnAbout);
btnY= new Button("返回");
btnY.addActionListener (this);
setSize(340,350);
setResizable(false);
setLocation (350,250);
setVisible (true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand () == "確 定")
{
logindispose();
if(login == 1)
{
Frame f = new MainFrame();
f.show();
dispose();
}
}
else if(e.getActionCommand () =="取 消")
dispose();
else if(e.getActionCommand () == "簡 介")
{
MegDlg =new Dialog (this,"簡介",true);
GridBagConstraints constraints = new GridBagConstraints ();
Panel txtPanel =new Panel ();
TextArea txtAbout =new TextArea ();
Font txtFont = new Font("TimesRoman",Font.BOLD ,14);
txtAbout.setFont (txtFont);
txtAbout.setEditable (false);
txtAbout.setText(" 圖書管理系統1.0版\n\n"+
"設計者:孔帥、施義森\n"+
" 倪磊、張春壘\n"+
"聯系方式:0731-2618605\n"+
"地址:長沙理工大學");
txtPanel.setLayout(new BorderLayout ());
txtPanel.add(txtAbout,BorderLayout.CENTER );
Panel butPanel = new Panel ();
butPanel.setLayout (new GridBagLayout ());
constraints.weightx =20;
constraints.weighty =20;
constraints.gridx =0;
constraints.gridy =0;
constraints.gridwidth =2;
constraints.gridheight =3;
butPanel.add(btnY,constraints);
MegDlg.add(txtPanel,BorderLayout.CENTER );
MegDlg.add(butPanel,BorderLayout.SOUTH );
MegDlg.setSize (200,200);
MegDlg.setLocation (690,300);
MegDlg.setVisible (true);
}
else if(e.getActionCommand () =="返回")
MegDlg.dispose();
}
public void textValueChanged(TextEvent e)
{
if(e.getSource () ==tfPW)
tfPW.setEchoChar ('*');
if(e.getSource () == tfName)
lbName.setText (tfName.getText ());
}
private void logindispose()
{
String url = "jdbc:odbc:book_db";
String username = "sa";
String password = "2259853";
//加載驅動程序以連接數據庫
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
loginconnection = DriverManager.getConnection( url,username,password );
}
//捕獲加載驅動程序異常
catch ( ClassNotFoundException cnfex )
{
System.err.println("裝載 JDBC/ODBC 驅動程序失敗。" );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
//捕獲連接數據庫異常
catch ( SQLException sqlex )
{
System.err.println( "無法連接數據庫" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
try
{
String loginquery;
String loginusename = tfName.getText();
String loginpassword = tfPW.getText();
loginquery = "select * from entry where (passname='"+loginusename +
"' and password = '"+loginpassword+"')";
loginstatement = loginconnection.createStatement();
loginresultSet = loginstatement.executeQuery( loginquery );
boolean Records = loginresultSet.next();
if ( ! Records )
{
lbLog.setText ("登錄失敗") ;
return;
}
else
{
login = 1 ;
}
loginconnection.close();
}
catch(SQLException sqlex)
{
sqlex.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -