如何在java swing中成功登录后调用Jmenu屏幕



请参阅下面的代码,我试图在成功登录后调用Jmenu类

登录:

public class Login {
    Connection con;
    Statement st;
    ResultSet rs;
    JFrame f = new JFrame ("User Login");
    JLabel l = new JLabel ("UserName:");
    JLabel l1 = new JLabel ("Password:");
    JTextField t = new JTextField (10);
    JTextField t1 = new JTextField (10);
    JButton b = new JButton ("Login");

    public Login ()
    {
        connect ();
        frame ();
    }
    public void connect ()
    {
        try
        {
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);
        String db = "jdbc:odbc:Joy_DB";
        con = DriverManager.getConnection(db);
        st = con.createStatement ();
        }
        catch (Exception ex)
        {

        }
    }
    public void frame ()
    {
        f.setSize (600,400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible (true);
        JPanel p = new JPanel ();
        p.add (l);
        p.add (t);
        p.add (l1);
        p.add (l);
        p.add (t1);
        p.add (b);
        f.add (p);
        b.addActionListener(new ActionListener(){
            public void actionPerformed (ActionEvent e)
            {
                try
                {
                String user = t.getText (). trim ();
                String pass = t1.getText (). trim ();
                String sql = "select User,Password from Table2 where User = '"+user+"' and Password = '"+pass+"'";
                rs = st.executeQuery(sql);
                int count = 0;
                while (rs.next())
                {
                    count = count +1;
                }
                if (count == 1 )
                {
                    JOptionPane.showMessageDialog(null,"User Found");
                    //JMenuDemo M = new JMenuDemo ();
                }
                else if (count > 1)
                {
                    JOptionPane.showMessageDialog(null, "Duplicate User !");
                }
                else
                {
                    JOptionPane.showMessageDialog (null,"User does not exist");
                }
                }
                catch (Exception ex)
                {
                }
            }
    });

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        new Login ();
        //JMenuDemo M = new JMenuDemo ();
        // TODO code application logic here
    }
}

如何在成功登录后使用上面的代码调用J菜单框

请帮助我将发送另一个类也就是Jmenu一会儿

  1. 定义一个LoginPanel,包含收集用户详细信息所需的所有逻辑
  2. 创建另一个包含应用程序组件和逻辑的面板。
  3. 使用JDialog显示登录面板。它将阻止代码的执行,直到对话框被关闭
  4. 根据LoginPane的状态,您可以(可能)退出应用程序(登录失败)或继续运行应用程序。
  5. 将应用面板添加到JFrame并使其可见

参见如何创建对话框了解更多细节。

您可能还想使用PreparedStatement来访问数据库

中的信息

相关内容

  • 没有找到相关文章