验证用户身份后调用另一个 JFrame

Calling another JFrame after veryfing user identity

如有任何帮助,我们将不胜感激。

我创建了 2 个 JFrame,其中一个是登录 window,用于验证用户凭据,我已将其与我网站的 mySQL 链接(这很有效,因为我非常Java 的新手),另一个是我的主要 window,它包含用户将在其中工作的主用户界面。

我正在尝试找到一种方法,当验证用户凭据时,登录 window 关闭并打开主 window。我越深入地研究它,我越觉得我把整个事情都搞错了,因为两个 JFrames 都是在 main 方法中构建的(我虽然这是你做的,每个教程都从一个 main 方法开始)。

到我在 JButton 或 textField_Password 上创建的 actionPerform,但这在 all.Any 想法如何从 actionPerform 打开另一个 class 时不起作用?

我想在验证用户凭据后打开主 window。关于如何进行的任何想法。这对我来说是全新的,我们将不胜感激任何帮助。 提前感谢任何帮助我解决这个问题的人,因为我有点卡住了!

我试图添加以下内容:

Main window = new Main();
window.frame.setVisible();

到我的 Jbutton actionPerform

这是主要代码,我有点迷茫,如有任何帮助,将不胜感激

import javax.swing.JFrame;
import java.awt.SystemColor;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.Font;

import java.awt.GridBagLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

//import com.mysql.jdbc.Connection;
//import com.mysql.jdbc.PreparedStatement;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JSeparator;
import javax.swing.JPasswordField;
import javax.swing.JCheckBox;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.EventQueue;

import java.sql.SQLException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Login {

private JFrame frame;
private JTextField txtUsername;
private JPasswordField txtPassword;
final String signUp = ("https://www.google.com");
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                Login window = new Login();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Login() {
    initialize();

}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(Color.BLACK);
    frame.setBounds(100, 100, 547, 382);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    frame.setLocationRelativeTo(null);
    // frame.setUndecorated(true);

    txtUsername = new JTextField();
    txtUsername.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            txtUsername.setText(null);
        }
    });

    JLabel labelResponse = new JLabel("");
    labelResponse.setHorizontalAlignment(SwingConstants.CENTER);
    labelResponse.setFont(new Font("Dialog", Font.BOLD, 12));
    labelResponse.setForeground(Color.ORANGE);
    labelResponse.setBounds(122, 280, 277, 30);
    frame.getContentPane().add(labelResponse);
    txtUsername.setBorder(null);
    txtUsername.setHorizontalAlignment(SwingConstants.CENTER);
    txtUsername.setFont(new Font("Meiryo", Font.BOLD, 18));
    txtUsername.setForeground(new Color(0, 128, 0));
    txtUsername.setOpaque(false);
    txtUsername.setText("Username");
    txtUsername.setBackground(SystemColor.inactiveCaption);
    txtUsername.setBounds(82, 62, 353, 53);
    frame.getContentPane().add(txtUsername);
    txtUsername.setColumns(10);

    /*
     * JLabel labelClose = new JLabel("X");
     * 
    labelClose.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
     * labelClose.addMouseListener(new MouseAdapter() {
     * 
     * @Override public void mouseClicked(MouseEvent e) { 
  System.exit(0); } });
     * labelClose.setFont(new Font("Tahoma", Font.BOLD, 20));
     * labelClose.setForeground(new Color(102, 205, 170)); 
 labelClose.setBounds(493,
     * 11, 22, 22); frame.getContentPane().add(labelClose);
     * 
     * JLabel labelMin = new JLabel("-");
     * 
 labelMin.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
     * labelMin.addMouseListener(new MouseAdapter() {
     * 
     * @Override public void mouseClicked(MouseEvent arg0) {
     * frame.setState(Frame.ICONIFIED); } }); 
 labelMin.setForeground(new Color(102,
     * 205, 170)); labelMin.setFont(new Font("Tahoma", Font.BOLD, 
20));
     * labelMin.setBounds(470, 11, 22, 22); 
frame.getContentPane().add(labelMin);
     */

    JLabel lblGt = new JLabel("Log In");
    lblGt.setForeground(new Color(0, 128, 0));
    lblGt.setFont(new Font("Meiryo", Font.BOLD, 20));
    lblGt.setBounds(10, 5, 88, 38);
    frame.getContentPane().add(lblGt);

    JSeparator separator = new JSeparator();
    separator.setBounds(82, 113, 353, 2);
    frame.getContentPane().add(separator);

    JSeparator separator_1 = new JSeparator();
    separator_1.setBounds(82, 190, 353, 2);
    frame.getContentPane().add(separator_1);

    JCheckBox chckbxShowPass = new JCheckBox("Show Pass");


chckbxShowPass.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    chckbxShowPass.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            if (chckbxShowPass.isSelected()) {
                txtPassword.setEchoChar((char) 0);
            } else {
                txtPassword.setEchoChar('*');
            }
        }
    });

    chckbxShowPass.setBorder(null);
    chckbxShowPass.setOpaque(false);
    chckbxShowPass.setForeground(new Color(0, 128, 0));
    chckbxShowPass.setFont(new Font("Meiryo", Font.BOLD, 14));
    chckbxShowPass.setBackground(new Color(0, 128, 0));
    chckbxShowPass.setBounds(409, 281, 106, 23);
    frame.getContentPane().add(chckbxShowPass);

    JLabel labelRegister = new JLabel("Register");

labelRegister.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    labelRegister.setForeground(new Color(0, 128, 0));
    labelRegister.setFont(new Font("Meiryo", Font.BOLD, 14));
    labelRegister.setBounds(10, 285, 76, 16);
    frame.getContentPane().add(labelRegister);

    JButton buttonLogin = new JButton("Login");
    buttonLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {

 Class.forName("com.mysql.jdbc.Driver");
                Connection con = (Connection) 
DriverManager.getConnection(

"jdbc:mysql://localhost:3306/test", "root", "password"); // not the actual 
                String query = "select  EmailAddress, 
Password from users where EmailAddress LIKE '%"
                        + 
txtUsername.getText() + "%'";
                PreparedStatement statement = 
con.prepareStatement(query);
                ResultSet result = 
statement.executeQuery();
                if (result.next()) {
                    String dbasePassword = 
result.getString("Password").toString().trim();
                    String enteredPassword = new 
String(txtPassword.getText().trim());
                    if 
   (dbasePassword.equals(enteredPassword))

 labelResponse.setText("User recognized");

                    else

labelResponse.setText("Verify credentials");
                } else {
                    labelResponse.setText("You 
must be registered to use this software.");
                }
                statement.close();
                con.close();
            } catch (SQLException se) {
                se.printStackTrace();
            } catch (Exception evt) {
                evt.printStackTrace();
                labelResponse.setText("Exception 
occurred while searching in the users table");
            }
        }
    });

    txtPassword = new JPasswordField();
    txtPassword.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent evt) {
            if (evt.getKeyCode() == KeyEvent.VK_ENTER)
                try {


    Class.forName("com.mysql.jdbc.Driver");
                    Connection con = (Connection) 
      DriverManager.getConnection(

     "jdbc:mysql://localhost:3306/test", "root",
                            "password");
                    String query = "select  
      EmailAddress, Password from users where EmailAddress LIKE '%"
                            + 
      txtUsername.getText() + "%'";
                    PreparedStatement statement = 
      con.prepareStatement(query);
                    ResultSet result = 
     statement.executeQuery();
                    if (result.next()) {
                        String dbasePassword = 
      result.getString("Password").toString().trim();
                        String enteredPassword 
      = new String(txtPassword.getText().trim());
                        if 
     (dbasePassword.equals(enteredPassword))


    labelResponse.setText("User recognized");

                        else

     labelResponse.setText("Verify credentials");
                    } else {

    labelResponse.setText("You must be registered to use this software.");
                    }
                    statement.close();
                    con.close();
                } catch (SQLException se) {
                    se.printStackTrace();
                } catch (Exception evte) {
                    evte.printStackTrace();

    labelResponse.setText("Exception occurred while searching in the users 
    table");
                }
        }
    });

    txtPassword.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            txtPassword.setText(null);

        }
    });
    txtPassword.setText("Password");
    txtPassword.setBorder(null);
    txtPassword.setHorizontalAlignment(SwingConstants.CENTER);
    txtPassword.setForeground(new Color(0, 128, 0));
    txtPassword.setFont(new Font("Meiryo", Font.BOLD, 18));
    txtPassword.setOpaque(false);
    txtPassword.setBackground(SystemColor.inactiveCaption);
    txtPassword.setBounds(82, 139, 353, 53);
    frame.getContentPane().add(txtPassword);

    buttonLogin.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    buttonLogin.setBorderPainted(false);
    buttonLogin.setBackground(new Color(0, 128, 0));
    buttonLogin.setFont(new Font("Tahoma", Font.PLAIN, 18));
    buttonLogin.setBounds(82, 216, 353, 53);
    frame.getContentPane().add(buttonLogin);

    labelRegister.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            try {
                Desktop.getDesktop().browse(new 
  URI("insertURL"));
            } catch (Exception ex) {
                // System.out.println(ex);
            }
        }
    });
}
}

我找到了解决方案,我会 post 在这里。希望它能帮助其他新编码员。

我的主要问题是我试图 link 两个应用程序 windows。我没有选择 New/Other/Jframe,而是使用了 New/Other/Application Window,这使得调用第二个 window.

变得困难