需要在jframe中显示登录详细信息

时间:2014-12-05 12:47:38

标签: java swing jframe

我在java中制作桌面应用程序,我需要一些帮助,

我使用sql数据库存储用户的所有信息,例如UserName,Password,全名。 我有2个框架,第一个是登录框架,管理员输入他的用户名和密码,如果它与sql数据库的详细信息匹配,那么它将带他到下一个框架,即管理面板,现在我想要的管理员全名应显示在NorthPanel的第二帧(即管理面板)的左上角, 这是我的两个框架的代码,请帮助

如何获取从sql数据库登录并在管理面板中显示的管理员名称?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
    import java.awt.event.*;
     import java.sql.*;
    import java.awt.*;
    import javax.swing.*  

public class Login extends JFrame implements ActionListener, KeyListener, FocusListener,       WindowListener
{
JPanel UserPanel,NorthPanel;

JLabel UserId,UserPassword;
JLabel LblFrgtPass;

JTextField TxtUser;

JPasswordField TxtUserPass;

JButton BtnLogin;

Connection con=null;
Statement stmt=null;
ResultSet rs=null;
boolean flag=false;


public Login()
{
    add(GetUserPanel(), BorderLayout.CENTER);
    add(GetNorthPanel(), BorderLayout.NORTH);
    addWindowListener(this);
}

public boolean GetConnection()
{
    flag=false;
    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con=DriverManager.getConnection("jdbc:odbc:Project");
        stmt=con.createStatement();
        flag=true;
    }catch(Exception ex)
    {
        ex.printStackTrace();
        flag=false;
    }
    return flag;
}

public boolean CloseConnection()
{
    flag=false;
    try
    {
        if(con!=null)
        {
            con.close();
            flag=true;
        }
    }catch(Exception ex)
    {
        flag=false;
    }
    return flag;
}

public ResultSet GetRecords(String sql)
{
    rs=null;
    try
    {
        rs=stmt.executeQuery(sql);

    }catch(Exception ex)
    {
        rs=null;
    }
    return rs;
}


JPanel GetNorthPanel()
{
    NorthPanel = new JPanel();
    NorthPanel.setLayout(new FlowLayout());

    ImageIcon titleIcon = new ImageIcon("titleicon.png");
    JLabel title = new JLabel(titleIcon);

    NorthPanel.add(title);

    NorthPanel.setBackground(Color.white);
    return NorthPanel;
}

JLabel GetLblForgetPassword()
{
    LblFrgtPass = new JLabel("Forgot Password ? ");
    return LblFrgtPass;
}


JPanel GetUserPanel()
{
    UserPanel=new JPanel();
    UserPanel.setLayout(new GridBagLayout());
    UserPanel.setBackground(Color.WHITE);



    GridBagConstraints GbcUserId = new GridBagConstraints();
    GbcUserId.gridx=1;
    GbcUserId.gridy=3;
    GbcUserId.fill=GridBagConstraints.BOTH;

    GbcUserId.insets = new Insets(10, 70, 0, 0);
    UserPanel.add(GetUserId(),GbcUserId);



    GridBagConstraints GbcTxtUser = new GridBagConstraints();
    GbcTxtUser.gridx=2;
    GbcTxtUser.gridy=3;

    GbcTxtUser.insets = new Insets(10, 40, 0, 0);
    UserPanel.add(GetTxtUser(),GbcTxtUser);



    GridBagConstraints GbcUserPassword = new GridBagConstraints();
    GbcUserPassword.gridx=1;
    GbcUserPassword.gridy=4;
    GbcUserPassword.fill=GridBagConstraints.BOTH;

    GbcUserPassword.insets = new Insets(10, 70, 0, 0);
    UserPanel.add(GetUserPassword(),GbcUserPassword);




    GridBagConstraints GbcTxtUserPass = new GridBagConstraints();
    GbcTxtUserPass.gridx=2;
    GbcTxtUserPass.gridy=4;

    GbcTxtUserPass.insets = new Insets(10, 40, 0, 0);
    UserPanel.add(GetTxtUserPass(),GbcTxtUserPass);




    GridBagConstraints GbcBtnLogin = new GridBagConstraints();
    GbcBtnLogin.gridx=2;
    GbcBtnLogin.gridy=5;

    GbcBtnLogin.insets = new Insets(50, 50, 20, 20);
    UserPanel.add(GetBtnLogin(),GbcBtnLogin);

    GridBagConstraints GbcLblFrgtPass = new GridBagConstraints();
    GbcLblFrgtPass.gridx=3;
    GbcLblFrgtPass.gridy=5;

    GbcLblFrgtPass.insets = new Insets(50, 0, 20, 20);
    UserPanel.add(GetLblFrgtPass(),GbcLblFrgtPass);



    return UserPanel;
}

JLabel GetUserId()
{
    UserId = new JLabel("User Id       :      ");
    UserId.setFont(new Font("Bookman Old Style", Font.PLAIN, 14));
    return UserId;
}

JTextField GetTxtUser()
{
    TxtUser = new JTextField(10);
    TxtUser.addKeyListener(this);
    TxtUser.addFocusListener(this);
    return TxtUser;
}

JLabel GetUserPassword()
{
    UserPassword = new JLabel("Password   :      ");
    UserPassword.setFont(new Font("Bookman Old Style", Font.PLAIN, 14));
    return UserPassword;
}

JPasswordField GetTxtUserPass()
{
    TxtUserPass = new JPasswordField(10);
    TxtUserPass.addKeyListener(this);
    TxtUserPass.addFocusListener(this);
    return TxtUserPass;
}

JLabel GetLblFrgtPass()
{
    LblFrgtPass = new JLabel("Forgot Passord ?");
    return LblFrgtPass;
}

JButton GetBtnLogin()
{
    BtnLogin = new JButton("  LogIn  ");
    //Project1 p = new Project1();
    BtnLogin.addActionListener(this);
    BtnLogin.setFont(new Font("Bookman Old Style", Font.PLAIN, 14));

    BtnLogin.registerKeyboardAction(BtnLogin.getActionForKeyStroke(
            KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
            KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
            JComponent.WHEN_FOCUSED);

    BtnLogin.registerKeyboardAction(BtnLogin.getActionForKeyStroke(
            KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
            KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
            JComponent.WHEN_FOCUSED);

    return BtnLogin;
}

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==BtnLogin)
    {
        String User_Id = TxtUser.getText().trim();
        String User_Pass = TxtUserPass.getText().trim();

        String sql = "Select * from users where UserId = '"+User_Id+"' and Password= '"+User_Pass+"'";

        if(GetConnection()==true)
        {
                        try
                        {
                            rs =GetRecords(sql);
                int count = 0;
            String usertype="";

            while(rs.next())
            {
                                count = count + 1;
                                usertype=rs.getString(3);
            }

            if(count ==1)
            {
                                if(usertype.equalsIgnoreCase("student"))
                                {
                JOptionPane.showMessageDialog(null, "Student Frame");
                                }
                                else if(usertype.equalsIgnoreCase("teacher"))
                                {
                JOptionPane.showMessageDialog(null, "Teacher Frame");
                                }
                               else if(usertype.equalsIgnoreCase("admin"))
                                {
                                                            Admin frame=new Admin();
                                                            frame.setSize(600, 400);
                                                            frame.setLocationRelativeTo(null);
                                                            frame.setVisible(true);

                                }
                                dispose();
            }
            else
            {
                                JOptionPane.showMessageDialog(null, "User Not Found!");
            }

            }
            catch(Exception ex)
            {
                //ex.printStackTrace();
                System.err.println("ERROR2");
            }
        }
        else
        {
            System.out.println("Not Connected");
        }
    }


}

public void keyTyped(KeyEvent ex)
{
    //ASCII :American Standard Code for Information Interchange

}

public void keyPressed(KeyEvent ex)
{
    System.out.println(ex.getKeyCode());
    if(ex.getKeyCode()==10 && ex.getSource()==TxtUser)
    {
        TxtUserPass.requestFocus();
    }
    else if(ex.getKeyCode()==10 && ex.getSource()==TxtUserPass)
    {
        BtnLogin.requestFocus();
    }
    else if(ex.getKeyCode()==10 && ex.getSource()==BtnLogin)
    {
        JOptionPane.showMessageDialog(null, "User Not Found!");
    }
}
public void keyReleased(KeyEvent ex)
{
    if(ex.getKeyCode()==10 && ex.getSource()==TxtUserPass)
    {
    //  JOptionPane.showMessageDialog(null, "User Not Found!");
    }   
}

public void focusGained(FocusEvent ex)
{
    if(ex.getSource()==TxtUser)
    {
        TxtUser.setBackground(Color.white);
    }
    else if(ex.getSource()==TxtUserPass)
    {
        TxtUserPass.setBackground(Color.white);
    }
}

public void focusLost(FocusEvent ex)
{
    if(ex.getSource()==TxtUser)
    {
        TxtUser.setBackground(Color.white);
    }
    else if(ex.getSource()==TxtUserPass)
    {
        TxtUserPass.setBackground(Color.white);
    }
}

public void windowActivated(WindowEvent ex)
{
}
public void windowDeactivated(WindowEvent ex)
{
}
public void windowIconified(WindowEvent ex)
{
}
public void windowDeiconified(WindowEvent ex)
{
}
public void windowClosing(WindowEvent ex)
{
        //JOptionPane.showMessageDialog(null,"GoodBye","Exit LogIn",JOptionPane.PLAIN_MESSAGE);
}
public void windowClosed(WindowEvent ex)
{

}
public void windowOpened(WindowEvent ex)
{
}


public static void main(String[] args)
{

                    Login Frame = new Login();
                    Frame.setResizable(false);
                    Frame.setSize(600,400);
                    Frame.setLocationRelativeTo(null);
                    Frame.setVisible(true);
            Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

以下代码是第二帧

 import java.awt.*;
 import javax.swing.*;
 import java.sql.*;
 public class Admin extends Login {

JFrame frame = new JFrame("Admin");
JPanel panel;
JPanel nPanel;
JLabel logOut;
JLabel UserName;

JButton btn1;
JButton btn2;
JButton btn3;
JButton btn4;
JButton btn5;
JButton btn6;

private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
boolean flag = false;

//****************MAIN CONSTRUCTOR******************************************
public Admin(){

    add(GetNPanel(), BorderLayout.NORTH);
    add(GetCPanel(), BorderLayout.CENTER);



}

//****************SQL CONNECTION********************************************

public boolean GetConnection(){
    flag = false;

    try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc:Project");
        //System.out.println("Connected");
        stmt = con.createStatement();
        flag=true;
    }
    catch(Exception ex){
        ex.printStackTrace();
        flag = false;
    }

    return flag;
}

//****************CENTER PANEL**********************************************
JPanel GetCPanel(){

    panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    //gbc.insets = new Insets(1, 1, 1, 1);  //cell padding
            gbc.fill = GridBagConstraints.BOTH; //fill cell area
            gbc.weightx = 1;  // fill horizontal cell area
            gbc.weighty = 1;  //fill vertical cell area

            //btn1
            btn1 = new JButton(new ImageIcon("teacher.png"));
            btn1.setToolTipText("Add / Edit / Remove Teachers");
            gbc.gridx = 0;
    gbc.gridy = 0;
    panel.add(btn1, gbc);

            //btn2
            btn2 = new JButton(new ImageIcon("student.png"));
    gbc.gridx = 1;
    gbc.gridy = 0;
    panel.add(btn2, gbc);

            //btn3
            btn3 = new JButton(new ImageIcon("notice.png"));
    gbc.gridx = 2;
    gbc.gridy = 0;
    panel.add(btn3, gbc);

            //btn4
            btn4 = new JButton(new ImageIcon("complaints.png"));
            gbc.gridx = 0;
    gbc.gridy = 1;
    panel.add(btn4, gbc);

            //btn5
            btn5 = new JButton(new ImageIcon("messages.png"));
            gbc.gridx = 1;
    gbc.gridy = 1;
    panel.add(btn5, gbc);

            //btn6
            btn6 = new JButton(new ImageIcon("password.png"));
            gbc.gridx = 2;
    gbc.gridy = 1;
    panel.add(btn6, gbc);

            return panel;
}

JPanel GetNPanel()
{
    nPanel = new JPanel();  
    return nPanel;
}



//****************NORTH PANEL***********************************************

public static void main(String[] args)
{
    new Admin();


    Admin frame = new Admin();
    //FRAME
    frame.setSize(600, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);

}

}

2 个答案:

答案 0 :(得分:1)

我会有一个setName方法,它接受一个字符串变量,然后设置标签的文本。

Public void setName(String name){
Label.setText(name)
}

然后我会在登录成功后调用该方法。

答案 1 :(得分:1)

您正在从AdminPanel帧延长Login帧。您可以做的是在Login框架中创建一个实例变量,并在登录成功时将用户全名的值存储到其中。

由于AdminPanel继承了Login框架,因此它会自动获取用户全名的值。然后,您可以在左侧角落的AdminPanel中获取任何JLabel,并在那里设置文本实例变量的值。