?? 學生管理系統.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class 學生管理系統 extends JDialog implements ActionListener
{
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
JTextField txtUserName=new JTextField(15);
JPasswordField txtPassword=new JPasswordField(15);
JButton ok=new JButton("確定");
JButton cancel=new JButton("取消");
public 學生管理系統(){
setModal(true);
setBackground(Color.LIGHT_GRAY);
Container contentPane=this.getContentPane();
contentPane.setLayout(new GridLayout(5,1));
p2.add(new JLabel("用戶名:"));p2.add(txtUserName);
p3.add(new JLabel(" 密碼:"));p3.add(txtPassword);
p4.add(ok);p4.add(cancel);
ok.addActionListener(this);
cancel.addActionListener(this);
txtUserName.addActionListener(this);
txtPassword.addActionListener(this);
contentPane.add(p1);
contentPane.add(p2);
contentPane.add(p3);
contentPane.add(p4);
contentPane.add(p5);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(500,375);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
Dimension my=this.getSize();
setLocation((screen.width-my.width)/2,(screen.height-my.height)/2);
setTitle("登錄窗口");
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok||e.getSource()==txtPassword){
String name=txtUserName.getText().trim();
String pass=String.valueOf(txtPassword.getPassword());
Connection con=null;
Statement sm=null;
String command=null;
try{
//DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:student");
}catch(Exception ex){
System.out.println(ex.getMessage());
}
try{
sm=con.createStatement();
command="select username,arth from yonghu where username='"+name+"' and pws='"+pass+"'";
ResultSet re=sm.executeQuery(command);
if(re.next())
{
int x=Integer.parseInt(re.getString("arth"));
if(x==2)
new MainWindow3(name);
else
if(x==1)
new MainWindow1(name);
else
new MainWindow2(name);
dispose();
}
else{
JOptionPane.showMessageDialog(null,"用戶名或密碼錯誤!");
txtUserName.requestFocus();
txtUserName.setSelectionStart(0);
txtUserName.setSelectionEnd(txtUserName.getText().length());
}
}catch(Exception em){
System.out.println(em.getMessage());
}
}
else if(e.getSource()==cancel){
dispose();
System.exit(0);
}
else if(e.getSource()==txtUserName)
txtPassword.requestFocus();
}
public static void main(String[] args){
JDialog.setDefaultLookAndFeelDecorated(true);
Font font=new Font("JFrame",Font.PLAIN,14);
Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
while(keys.hasMoreElements()){
Object key=keys.nextElement();
if(UIManager.get(key) instanceof Font)
UIManager.put(key,font);
}
new 學生管理系統();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -