我从一个 JFrame 到另一个 Jframe 的链接不起作用

My linkage from a JFrame to another Jframe does not work

我想创建一个登录菜单。登录后,应该会出现一个新的 JFrame。 所有 JFrames 都是使用 Netbeans 的 GUI Builder(设计)创建的。 由于某种原因,从前一个 JFrame 到第二个的 linkage 不起作用。的确,我在前一个JFrame上按"Entra"后,它只处理并没有出现第二个窗体。 这是 "First" 表单,其中包含登录菜单

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


public class Login extends javax.swing.JFrame {

    boolean premutoLogin;

    //some stuff automatically generated...                                               

    private void entraButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        eseguiLogin();
    }                                           


    private void eseguiLogin(){
        Database.schema = Database.user = usernameTextField.getText();
        Database.password = new String(passwordField.getPassword());
        try{
            Database.setDefaultConnection(Database.connetti());
            premutoLogin = true;
            dispose();
        } catch(SQLException exc){
            PrincipaleCF.mostraErroriSwing(this, exc);
        }
    }


}

前一个JFrame应该link到这个JFrame:

import java.awt.Color;
import java.sql.*;
import javax.swing.JOptionPane;

public class PrincipaleCF extends javax.swing.JFrame {
    /**
     * Creates new form PrincipaleCF
     */
    public PrincipaleCF() {
        initComponents();
        Login login = new Login(this, true);
        login.setVisible(true);
        if(!login.premutoLogin)
            dispose();
        else
            mostraDefault();            
    }

    private void mostraDefault(){
        setVisible(true);
        this.getRootPane().setDefaultButton(checkButton);
    }

    //edit by the gui builder          

    private void esciButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try{
            Database.getDefaultConnection().close();
        } catch(SQLException exc){
            mostraErroriSwing(this, exc);
        }
        dispose();
    }                                          

    private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        eseguiControllo();
    }                                           


    public static void mostraErroriSwing(java.awt.Component thrower, SQLException e){
        //my definition...
    }

    //psv main generated automatically

        /* Create and display the form */
       //...


}

出于某种原因,它编译 运行 没有问题。但是当我在前一个表单上单击 "Entra" 时,它只会处理但不会 link 到第二帧。

您正在调用静态方法 eseguiLogin() 但您没有创建新实例。所以,试试这个:


private void eseguiLogin(){
        Database.schema = Database.user = usernameTextField.getText();
        Database.password = new String(passwordField.getPassword());
        try{
            Database.setDefaultConnection(Database.connetti());
            premutoLogin = true;
            dispose();
        } catch(SQLException exc){

            PrincipaleCF cf =new PrincipaleCF (); //Creates new instance
            cf.mostraErroriSwing(this, exc); //Calls static method
        }
    }

对于初学者,我建议您导入 类 而不是像

这样的东西

public class PrincipaleCF extends javax.swing.JFrame {

但是

import javax.swing.JFrame;

public class PrincipaleCF extends JFrame {

要解决您的框架链接问题,您只需调用一个函数来打开您的第二个 window,因为它会关闭第一个,就像

private void eseguiLogin(){
    Database.schema = Database.user = usernameTextField.getText();
    Database.password = new String(passwordField.getPassword());
    try{
        Database.setDefaultConnection(Database.connetti());
        premutoLogin = true;
        dispose();

        //this
        PrincipaleCF secondFrame = new PrincipaleCF();
    } catch(SQLException exc){
        PrincipaleCF.mostraErroriSwing(this, exc);
    }
}

欢迎来到 Whosebug。所以让我弄清楚这一点。您想在 try-catch 块成功时显示 PrincipaleCF

try{
        Database.setDefaultConnection(Database.connetti());
        premutoLogin = true;
        dispose();
    } catch(SQLException exc){
        PrincipaleCF.mostraErroriSwing(this, exc);
    } 

因此,为此,您必须在处理登录 Jframe 之前创建 PrincipaleCF JFrame 的实例。所以你的 try 块看起来像:

try{
        Database.setDefaultConnection(Database.connetti());
        premutoLogin = true;
        PrincipaleCF pcf = new PrincipaleCF();
        // If that not succeed try calling pcf.mostraDefault();
        dispose();
    } catch(SQLException exc){
        PrincipaleCF.mostraErroriSwing(this, exc);
    }