📄 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 + -