中的信息
请参阅下面的代码,我试图在成功登录后调用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一会儿
- 定义一个
LoginPanel
,包含收集用户详细信息所需的所有逻辑 - 创建另一个包含应用程序组件和逻辑的面板。
- 使用
JDialog
显示登录面板。它将阻止代码的执行,直到对话框被关闭 - 根据LoginPane的状态,您可以(可能)退出应用程序(登录失败)或继续运行应用程序。
- 将应用面板添加到
JFrame
并使其可见
参见如何创建对话框了解更多细节。
您可能还想使用PreparedStatement
来访问数据库