所以我正在使用eclipse和mysql创建一个登录界面。该接口可以很好地用作外壳,因此无需连接到数据库,现在我终于将其连接到数据库,但是我遇到了在成功登录后将其链接到另一个应用程序的问题。
因此,对于它的外壳,我只为代码提供了一个用户名/密码组合,以继续进行界面的下一部分,但是如果我希望在成功登录后使链接正常工作,我应该将其放置在哪里代码?
我的代码:
package project_files;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import project_files.registration_test;
import project_files.root_login;
import project_files.gui_interface;
import project_files.video_interface;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
public class login_sys {
private JFrame frame;
private JTextField txtUsername;
private JPasswordField txtPassword;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
login_sys window = new login_sys();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public login_sys() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(200, 200, 523, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel LabelLogin = new JLabel("Login Page");
LabelLogin.setBounds(209, 12, 100, 15);
frame.getContentPane().add(LabelLogin);
JLabel LabelUsername = new JLabel("Username");
LabelUsername.setBounds(61, 68, 92, 17);
frame.getContentPane().add(LabelUsername);
JLabel LabelPassword = new JLabel("Password");
LabelPassword.setBounds(61, 133, 66, 15);
frame.getContentPane().add(LabelPassword);
txtUsername = new JTextField();
txtUsername.setBounds(244, 68, 124, 19);
frame.getContentPane().add(txtUsername);
txtUsername.setColumns(10);
txtPassword = new JPasswordField();
txtPassword.setBounds(244, 131, 124, 19);
frame.getContentPane().add(txtPassword);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatabase", "root", "pass1234");
Statement stmt= con.createStatement();
String sql = "Select * from user where Email = '" +txtUsername.getText()+"' and Password ='" +txtPassword.getText().toString()+"'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
JOptionPane.showMessageDialog(null, "Login Successfully...");
else
JOptionPane.showMessageDialog(null, "Incorrect username and password...");
con.close();
} catch(Exception e) {System.out.print (e);}
String password = txtPassword.getText();
String username = txtUsername.getText();
if(password.contains("pass1234") && username.contains("root")){
txtPassword.setText(null);
txtUsername.setText(null);
root_login info = new root_login();
root_login.main(null);
}
else
{
if(password.contains("pass1234") && username.contains("[email protected]")){
txtPassword.setText(null);
txtUsername.setText(null);
JOptionPane.showMessageDialog(null, "Login Successful", "Login Warning", JOptionPane.WARNING_MESSAGE);
video_interface info = new video_interface();
video_interface.main(null);
}
else
{
JOptionPane.showMessageDialog(null, "Invalid Login Details", "Login Error", JOptionPane.ERROR_MESSAGE);
txtPassword.setText(null);
txtUsername.setText(null);
}
}
}});
btnLogin.setBounds(23, 203, 130, 25);
frame.getContentPane().add(btnLogin);
JButton btnReset = new JButton("Reset");
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtUsername.setText(null);
txtPassword.setText(null);
}
});
btnReset.setBounds(212, 203, 114, 25);
frame.getContentPane().add(btnReset);
JButton btnNewExit = new JButton("Exit");
btnNewExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFrame frmLogin_sys = new JFrame("Exit");
if (JOptionPane.showConfirmDialog(frmLogin_sys, "Confirm if you want to exit", "Login System",
JOptionPane.YES_NO_OPTION)== JOptionPane.YES_NO_OPTION) {
System.exit(0);
}
}
});
btnNewExit.setBounds(373, 203, 114, 25);
frame.getContentPane().add(btnNewExit);
JSeparator separator = new JSeparator();
separator.setBounds(12, 175, 499, 2);
frame.getContentPane().add(separator);
JSeparator separator_1 = new JSeparator();
separator_1.setBounds(12, 36, 499, 2);
frame.getContentPane().add(separator_1);
JButton btnCreateAccount = new JButton("Create Account");
btnCreateAccount.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
registration_test info = new registration_test();
registration_test.main(null);
}
});
btnCreateAccount.setBounds(135, 233, 265, 25);
frame.getContentPane().add(btnCreateAccount);
}
}
我正在使用(目前仍在我的代码中)
if(password.contains("pass1234") && username.contains("[email protected]")){
txtPassword.setText(null);
txtUsername.setText(null);
JOptionPane.showMessageDialog(null, "Login Successful", "Login Warning", JOptionPane.WARNING_MESSAGE);
video_interface info = new video_interface();
video_interface.main(null);
将成功的登录链接到其他应用程序界面。我可以吗使用
video_interface info = new video_interface();
video_interface.main(null);
我一直在做什么?添加此代码的正确位置在哪里?第二个应用程序位置在我的代码的开头导入。
[如果您更改video_interface
类的构造函数以初始化并显示GUI组件(就像您当前在login_sys.initialize
方法中所做的那样,则可以使用]很好)
video_interface info = new video_interface()
在任何Java程序中都应该只有一个
static void main(String[] args)
方法。
作为附带说明,Java通常使用camelCasing(例如JFrame
,JTextField
),如果采用该约定,则代码将更易于阅读。