变量不在类之间传递

时间:2013-01-12 20:51:37

标签: java class methods user-input

我已经废弃了旧的问题,因为它不清楚并且有一堆错误,我发现有点“我认为”解决方案,虽然没有完全正常工作。 修订:::::::: LognL类

public final class LogInL extends JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
String Username;
String ID;
public LogInL() {
    initComponents();
    ButtonGroup();         
}

private void backBActionPerformed(ActionEvent e) {
    LoginMain login = new LoginMain();
    login.setVisible(true);
    this.dispose();
}

private void LoginBActionPerformed(ActionEvent e) {

    if(prosecutorCB.isSelected())
    {
        try {
            conn = SQLConnect.ConnectDb();
            String sql = "SELECT prosecutors.username, criminalrecords.ID FROM prosecutors, criminalrecords "
                       + "WHERE username = ? and ID = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1, usernameF.getText());
            pst.setString(2, criminalIdF.getText());

            Username = usernameF.getText();
            System.out.println("Username is " + Username);

            ID = criminalIdF.getText();
            System.out.println("Criminal ID is " + ID);
            rs = pst.executeQuery();
            if(rs.next())
            {
                ProsecutorMain Frame = new ProsecutorMain(); // call Policemain class //display it
                Frame.pack();
                Frame.setVisible(true); // make it visible 
                System.out.println("Welcome to prosecutors");
                this.dispose();
            }
            else
            {
                JOptionPane.showMessageDialog(null,"<html>wrong username or criminal ID<br>"
                + "Criminal may not longer be in database");
            }
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(LogInL.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//end if proseutor


}//end logination

private void ButtonGroup()
{
    ButtonGroup bg = new ButtonGroup();
    bg.add(prosecutorCB);
    bg.add(criminaldefenceCB);
}

修订:::::::: 检察官主要类

public class ProsecutorMain extends JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
LogInL id = new LogInL();
String UserName;
public ProsecutorMain() throws SQLException 
{
    initComponents();
    UserName = id.Username;
    username.setText(UserName);

    firstname.setText("blablabla");
    incidentlocation.setText("kupa");
    System.out.println(UserName);


} 
private void logOutActionPerformed(ActionEvent e) {

        int response = JOptionPane.showConfirmDialog(null, "<html> Are you sure you want to log out?", 
                "Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (response == JOptionPane.NO_OPTION) 
        {

        }
        if (response == JOptionPane.YES_OPTION)
        {
            this.dispose();
            LogInL login = new LogInL();
            login.setVisible(true);
            JOptionPane.showMessageDialog(null,"You have been sucessfully logged out");
        }       
    }

问题中的变量是用户名

1 个答案:

答案 0 :(得分:0)

UserName中的变量nullProsecutorMain,因为您正在创建LogInL的新实例,该实例没有原始数据库中的值(并显示) )JFrame的实例。

要修复,您必须传入 设置Username的实例:

public ProsecutorMain(LogInL id) {
   this.id = id;
   ...
}   

并在LogInL中创建:

new ProsecutorMain(this);

当您从查询中获得结果时,您可以使用必要的ProsecutorMain和{{1}创建自定义JFrame,而不是创建新的LawRecord username }字段。然后,您可以将其传递给id

您可以使用模式ProsecutorMain来接受搜索条件,而不是在此使用2 JFrames

链接:How to Make Dialogs