2014-12-05 88 views
-2

林做在Java桌面应用程序,我需要一些帮助,需要JFrame中显示的登录信息

即时通讯使用SQL数据库来存储我的用户,e.g用户名,密码,全名的所有信息。 我有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); 

} 

} 
+0

答案就是将名称设置为JLabel并将其添加到northpanel。不知何故,我认为这不是你要找的答案。你能重新解释一下这个问题吗? – 2014-12-05 12:53:04

回答

1

您正在扩展您的AdminPanel框架Login框架。你可以做的是在你的Login框架中创建一个实例变量,并且如果登录成功,则将用户全名存储到它。

由于AdminPanel继承Login框架,所以它会自动获取用户的全名的值。然后,您可以在左边角落的AdminPanel中使用任何JLabel,并在其中设置实例变量的值。

1

我想有一个的setName方法,该方法采用一个字符串变量,然后设置标签的文本。

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

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

相关问题