📄 logon.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Logon extends JFrame implements ActionListener {
public Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
private int fl=1;
private JPanel pLog = new JPanel();
private JLabel lbUser, lbPass;
private JTextField txtUser;
private JPasswordField txtPass;
private JButton btnOk, btnCancel;
private JRadioButton rad,remp;
private Connection con;
public String user;
public Logon () {
super ("Bug Tracking System.");
setSize (500, 400);
addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent we) {
setVisible (false);
dispose();
System.exit (0);
}
});
setLocation (d.width / 2 - getWidth() / 2, d.height / 2 - getHeight() / 2);
pLog.setLayout (null);
//Setting the Form's Radio Button's
rad=new JRadioButton("Admin");
rad.setBounds(80,140,100,25);
rad.addActionListener(this);
remp=new JRadioButton("Employee");
remp.setBounds(80,200,100,25);
remp.addActionListener(this);
rad.setSelected(true);
ButtonGroup bg=new ButtonGroup();
bg.add(rad);
bg.add(remp);
//Setting the Form's Labels.
lbUser = new JLabel ("Username:");
lbUser.setForeground (Color.black);
lbUser.setBounds (20, 15, 75, 25);
lbPass = new JLabel ("Password:");
lbPass.setForeground (Color.BLACK);
lbPass.setBounds (20, 50, 75, 25);
//Setting the Form's TextField & PasswordField.
txtUser = new JTextField ();
txtUser.setBounds (100, 15, 150, 25);
txtPass = new JPasswordField ();
txtPass.setBounds (100, 50, 150, 25);
//Setting the Form's Buttons.
btnOk = new JButton ("OK");
btnOk.setBounds (20, 90, 100, 25);
btnOk.addActionListener (this);
btnCancel = new JButton ("Cancel");
btnCancel.setBounds (150, 90, 100, 25);
btnCancel.addActionListener (this);
//Adding All the Controls in Panel.
pLog.add(rad);
pLog.add(remp);
pLog.add (lbUser);
pLog.add (lbPass);
pLog.add (txtUser);
pLog.add (txtPass);
pLog.add (btnOk);
pLog.add (btnCancel);
//Adding Panel to the Form.
getContentPane().add (pLog);
//Opening the Database.
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String loc = "jdbc:odbc:bugproj";
con = DriverManager.getConnection (loc);
}
catch (ClassNotFoundException cnf) {
JOptionPane.showMessageDialog (null, "Driver not Loaded...");
System.exit (0);
}
catch (SQLException sqlex) {
JOptionPane.showMessageDialog (null, "Unable to Connect to Database...");
System.exit (0);
}
setVisible (true);//form
}
public void actionPerformed (ActionEvent ae) {
Object obj = ae.getSource();
if (obj == btnOk) { //If OK Button Pressed.
String password = new String (txtPass.getPassword());
if (txtUser.getText().equals ("")) {
JOptionPane.showMessageDialog (this, "Provide Username to Logon.");
txtUser.requestFocus();
}
else if (password.equals ("")) {
txtPass.requestFocus();
JOptionPane.showMessageDialog (null,"Provide Password to Logon.");
}
else {
String pass; //To Hold the Password.
boolean verify = false; //To Confirm Logon.
if(fl==1)
{
if(txtUser.getText().equals("Admin")&&password.equals("admin"))
{
verify=true;
//NextPage page=new NextPage();
//JLabel label = new JLabel("Welcome: Admin");
//page.getContentPane().add("South",label);
//page.setVisible(true);
new BugSystem(1,con);
setVisible(false);
dispose(); }
}
else
{
String tablename=null;
if(fl==2)tablename="employees";
try {
//SELECT Query to Retrieved the Record.
String query = "SELECT * FROM " + tablename ;
//+ " WHERE id = " + Integer.parseInt(txtUser.getText())
Statement st = con.createStatement (); //Creating Statement Object.
ResultSet rs = st.executeQuery (query); //Executing the Query.
rs.next(); //Moving Towards the Record.
user = rs.getString ("username"); //Storing UserName.
pass = rs.getString ("pass"); //Storing Password.
if (txtUser.getText().equals (user) && password.equals (pass))
{
verify = true;
//Integer.parseInt(txtUser.getText())
new BugSystem2 (fl,1,con); //Show Main Form.
setVisible (false); //Hide the Form.
dispose(); //Free the System Resources.
}
else {
verify = false;
JOptionPane.showMessageDialog (this, "Incorrect Information Provided.");
txtUser.setText ("");
txtPass.setText ("");
txtUser.requestFocus ();
}
}
catch (Exception sqlex) {
if (verify == false) {
JOptionPane.showMessageDialog (this, "Incorrect excep Information Provided.");
txtUser.setText ("");
txtPass.setText ("");
txtUser.requestFocus ();
}
}
}
}
}
else if (obj == btnCancel)
{
setVisible (false);
dispose();
System.exit (0);
}
else if(obj==rad)
{ fl=1;}
else if(obj==remp)
{ fl=2;}
}
public static void main(String args[])
{ Logon start=new Logon(); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -