?? mainwindow.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class mainWindow extends JFrame implements ActionListener
{
private JLabel whereLabel = new JLabel( "營業部" );
private String[] whereStr = {
new String( "營業部A" ), new String( "營業部B" )
};
private JComboBox whereCombo = new JComboBox( whereStr );
private JLabel whoLabel = new JLabel( "身份:" );
private String[] whoStr = { new String( "柜臺人員" ),
new String( "支行行長" ), new String( "營業部主任" )
};
private JComboBox whoCombo = new JComboBox( whoStr );
private JLabel user = new JLabel( "用戶:" );
private JTextField username = new JTextField( 20 );
private JLabel pass = new JLabel( "密碼:" );
private JTextField passwords = new JTextField( 20 );
private JButton enter = new JButton( "進入" );
private JButton exit = new JButton( "離開" );
public mainWindow()
{
super( "管理員登陸" );
Container c = getContentPane();
c.setLayout( new BorderLayout() );
JPanel[] panel = new JPanel[3];
for( int i=0; i<panel.length; i++ )
{
panel[i] = new JPanel();
panel[i].setLayout( new FlowLayout(FlowLayout.CENTER) );
}
c.add( panel[0], BorderLayout.NORTH );
panel[0].add( whereLabel );
panel[0].add( whereCombo );
panel[0].add( whoLabel );
panel[0].add( whoCombo );
c.add( panel[1], BorderLayout.CENTER );
panel[1].add( user );
panel[1].add( username );
panel[1].add( pass );
panel[1].add( passwords );
c.add( panel[2], BorderLayout.SOUTH );
panel[2].add( enter );
panel[2].add( exit );
enter.addActionListener( this );
exit.addActionListener( this );
setSize( 280, 150 );
setResizable( false );
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void actionPerformed( ActionEvent e )
{
if( e.getSource()==enter )
{
String userId=username.getText();
String password=passwords.getText();
if((userId.length()==0)||(password.length()==0))
{
System.out.println("用戶名或密碼不能為空!");
}
/*數據庫判斷*/
else
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//聲明驅動程序
String url="jdbc:odbc:info";//指定數據源名
Connection con=DriverManager.getConnection(url);//連接數據庫
PreparedStatement select_stm=con.prepareStatement("select password from 登陸 where userId=?");
//設置查詢
select_stm.setString(1,userId);
//設置查詢中的一個參數
ResultSet result=select_stm.executeQuery();
//得到查詢結果
String temp_password=null;
//聲明存儲查詢結果中密碼屬性的變量
if(result.next())
temp_password=result.getString(1);
//得到查詢結果中密碼屬性值
result.close();
if(temp_password==null)
//如果數據庫中還沒有此用戶名
{
System.out.println(" no userId");
String s="this userId is not exits!";
JOptionPane.showMessageDialog(null,s);
}
else if(password.regionMatches(0,temp_password,0,password.length()))
{
this.dispose(); //讓主窗口消失
new Login();
}
con.close();
}
catch(Exception ev)
{
}
}
}
else if( e.getSource()==exit )
{
int n = JOptionPane.showConfirmDialog(
this, "是否真的要退出", "退出窗口",
JOptionPane.YES_NO_OPTION );
if( n==JOptionPane.YES_OPTION )
System.exit( 0 );
}
}
public static void main( String[] args )
{
new mainWindow();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -