簡體   English   中英

關閉 JFrame 並從另一個類打開另一個 JFrame

[英]Close JFrame and open another JFrame from another class

我有這個特殊問題,使我無法進入項目的下一階段。 我想從另一個類的主類中關閉一個 JFrame,然后在單擊按鈕時打開另一個 jFrame。 我已經能夠在單擊按鈕時打開第二個 JFrame,但無法關閉第一個 JFrame。 我正在使用Netbeans

這是我的代碼。

登錄頁面(主類)

package auth;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LoginPage extends javax.swing.JFrame {
    public static LoginPage lp = new LoginPage(); 
    
    public LoginPage() {
        initComponents();
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here: Cancel button
        System.exit(0);
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here: Login button
        String uname = jTextField1.getText();
        String pword = jPasswordField1.getText();
        
        try {
            LoginController.collectUserData(uname, pword);
        } catch (SQLException ex) {
            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginPage().setVisible(true);
            }
        });
    }


和另一堂課

登錄控制器

package auth;

import dbconnect.dbconnect;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;

public class LoginController extends javax.swing.JFrame { 
    
    public static void collectUserData(String uname, String pword) throws SQLException {
        Login user = new Login();
        user.setUsername(uname);
        user.setPass(pword);
        checkDatabaseAndLogin(user);
    }

    public static void checkDatabaseAndLogin(Login test) throws SQLException {
        JFrame rootPane;
        if (test.getUsername().equals("")||test.getPass().equals("")) {
            rootPane = new JFrame();
            JOptionPane.showMessageDialog(rootPane, "Some fields are empty", "Error", 1);
        } else {
            try {
                //LoginPage lp = new LoginPage();
                Connection con = dbconnect.connect();
                PreparedStatement pst = con.prepareStatement("select * from test where username=? and pass=?");
                pst.setString(1, test.getUsername());
                pst.setString(2, test.getPass());
                ResultSet rs = pst.executeQuery();
                                
                if (rs.next()) {
                    String un = rs.getString("username");
                    //System.out.println(un);
                    PatronPage pt = new PatronPage(un);
                    pt.setVisible(true);  //Code to open the new window
                    LoginPage.lp.dispose(); //Code to close the old window
                } else {
                    rootPane = new JFrame();
                    JOptionPane.showMessageDialog(rootPane, "Username or Password do not match record", "Login error", 1);
                } 
            } catch (Exception ex) {
                System.out.println(""+ex);
            }
        }
    }
    
}

當然,我刪除了 Netbeans 中的其他系統生成代碼,我只是提供了我認為與解決方案相關的代碼塊。 請幫忙,我卡住了

您在LoginController類中使用以下行“關閉”的LoginPage實例

LoginPage.lp.dispose();

不是您最初顯示的實例

new LoginPage().setVisible(true);

恐怕您創建 Swing UI 的整個方法都是錯誤的。 也許先完成Swing 教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM