>我想创建一个登录菜单。登录后,应该会出现一个新的 JFrame。所有 JFrames 都是使用 Netbeans 的 GUI Builder (Design( 创建的。由于某种原因,从前一个 JFrame 到第二个 JFrame 的链接不起作用。事实上,在我按"Entra"在以前的JFrame上后,它只处理而不是出现第二个形式。这是"第一个"表单,其中包含登录菜单
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 应该链接到这个 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"时,它只会处理但不链接到第二个框架。
对于初学者,我建议您导入类,而不是像
public class PrincipaleCF extends javax.swing.JFrame {
但
import javax.swing.JFrame;
public class PrincipaleCF extends JFrame {
要解决框架链接问题,您只需要调用一个函数,该函数将打开第二个窗口,因为它关闭了第一个窗口,
如下所示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);
}
}
来到 StackOverflow。所以让我直截了当。您希望在此 try-catch 块成功时显示PrincipaleCF
:
try{
Database.setDefaultConnection(Database.connetti());
premutoLogin = true;
dispose();
} catch(SQLException exc){
PrincipaleCF.mostraErroriSwing(this, exc);
}
因此,要做到这一点,您必须在释放登录 Jframe 之前创建一个 PrincipaleCF JFrame 的 instace。所以你的 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);
}
您正在调用静态方法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
}
}