在Action Listener中使用另一个类的变量输出'null'

时间:2013-07-16 20:34:49

标签: java swing variables null actionlistener

这是在解决了我之前的问题之后出现的另一个问题:

How to use a variable created in class1, in another class?

上述问题的答案允许我通过调用类4中的类4中的方法来打印在类3中创建的变量。

但是,当我尝试将该变量作为动作侦听器的一部分进行打印时,它会打印出“null”而不是JTexfield create_u1中的用户输入(在第3类中)。

更新了sajjadG:请亲自尝试

第1课

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class class1 extends JFrame {
    public static void main(String[] args) {

        mainPage MP = new mainPage();
        MP.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MP.setLocationRelativeTo(null);
        MP.setSize(300,200);
        MP.setVisible(true);
    }
}

第2课

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class mainPage extends JFrame {
    create_account crAcc = new create_account();
    change_username chU = new change_username();
    change_password chPW = new change_password();
    sign_in signIn = new sign_in();

    private JButton create_account, change_username, change_password, signIn_button;

    public mainPage(){
        super("Password Programme"); 

        setPreferredSize (new Dimension (400, 100));
        setLayout (null);

        create_account = new JButton("Create an Account");
        add(create_account);

        change_username = new JButton("Change Username");
        add(change_username);

        change_password = new JButton("Change Password");
        add(change_password);

        signIn_button = new JButton("Sign in and Access Files");
        add(signIn_button);

        create_account.setBounds (10, 20, 150, 20);
        change_username.setBounds (10, 50, 150, 20);
        change_password.setBounds (10, 80, 150, 20);
        signIn_button.setBounds (10, 110, 200, 20);

        HandlerClass handler = new HandlerClass();
        create_account.addActionListener(handler);
        change_username.addActionListener(handler);
        change_password.addActionListener(handler);
        signIn_button.addActionListener(handler);
    }

    private class HandlerClass implements ActionListener{
        public void actionPerformed(ActionEvent event){
            if(event.getSource()==create_account) {
                crAcc.setLocationRelativeTo(null); 
                crAcc.setSize(300,200); 
                crAcc.setVisible(true);
            }
            if(event.getSource()==change_username) {
                chU.setLocationRelativeTo(null); 
                chU.setSize(300,200); 
                chU.setVisible(true);
            }
            if(event.getSource()==change_password) {
                chPW.setLocationRelativeTo(null); 
                chPW.setSize(300,200); 
                chPW.setVisible(true);
            }
            if(event.getSource()==signIn_button) {
                signIn.setLocationRelativeTo(null);
                signIn.setSize(300,200); 
                signIn.setVisible(true);
            }
        }
    }   
}

第3课

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class create_account extends JFrame{
    private String u1, pw1;
    private JLabel cU1, cpw1, statusBar;

    public JTextField create_u1; 
    public JPasswordField create_pw1;
    private JButton change;

    public String userName3, passWord3;    

    public void checkUserName(String u, String pw) {

        System.out.println(u);          ///// this prints it out correctly
        System.out.println(pw); 

        if (create_u1.getText()==u){
            System.out.println("correct");
        }else {                       /// it tests incorrect even though i inputted same thing
            System.out.println("incorrect");
            System.out.println(create_u1.getText());
            System.out.println(userName3);       /// prints out null            }
    }

    public create_account() {
        super("Create Account"); 
        setPreferredSize (new Dimension (400, 85));
        setLayout (null);

        statusBar = new JLabel("Create a username");
        add(statusBar, BorderLayout.SOUTH);        
        statusBar.setBounds(20, 110, 250, 30); 

        cU1 = new JLabel("Username");
        cpw1 = new JLabel("Password");
        create_u1 = new JTextField(10);
        create_pw1 = new JPasswordField(10);

        cU1.setBounds(10, 10, 150, 30); 
        create_u1.setBounds(100, 10, 100, 30); 
        cpw1.setBounds(10, 50, 150, 30); 
        create_pw1.setBounds(100, 50, 100, 30); 

        add(create_u1);
        add(cU1);

        create_u1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event){
                    JOptionPane.showMessageDialog(null, "Username saved. Now create a password");

                    statusBar.setText("Create a password");

                    add(cpw1);
                    add(create_pw1);

                    cpw1.repaint();
                    create_pw1.repaint();

                    create_pw1.requestFocus();                   

                }
            }
        );

        create_pw1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event){
                    JOptionPane.showMessageDialog(null, "Password saved");

                    statusBar.setText("Account created. Return to main programme");                    

                    statusBar.requestFocus();
                }
            }
        );
    }

}

第4课

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class change_username extends JFrame {

    private JLabel uT1, pwT, uCh, statusBar;
    private JTextField username_input, username_change;
    private JPasswordField password_input;   

    create_account objOfClass3 = new create_account();

    public void checkUserName() {
        objOfClass3.checkUserName(username_input.getText(), password_input.getText());
    }

    public change_username() {
        super("Change Username"); 
        setPreferredSize (new Dimension (400, 85));
        setLayout (null);

        statusBar = new JLabel("Enter your username");
        add(statusBar, BorderLayout.SOUTH);
        statusBar.setBounds(20, 130, 250, 30); 

        uT1 = new JLabel("Username");
        username_input = new JTextField(10);

        pwT = new JLabel("Password");
        password_input = new JPasswordField(10); 

        uCh = new JLabel("New Username");
        username_change = new JTextField(10);

        uT1.setBounds(10, 10, 150, 30); 
        username_input.setBounds(100, 10, 100, 30); 
        pwT.setBounds(10, 50, 150, 30); 
        password_input.setBounds(100, 50, 100, 30);        
        uCh.setBounds(10, 90, 150, 30); 
        username_change.setBounds(100, 90, 100, 30); 

        add(uT1);
        add(username_input);

        username_input.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event){
                    statusBar.setText("Enter your password");

                    add(pwT);
                    add(password_input);

                    pwT.repaint();
                    password_input.repaint();

                    password_input.requestFocus();

                }
            }
        );        

        password_input.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event){
                    statusBar.setText("Enter your new username");

                    add(uCh);
                    add(username_change);

                    uCh.repaint();
                    username_change.repaint();

                    username_change.requestFocus();

                    checkUserName();
                }
            }
        );

        username_change.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event){
                    statusBar.setText("Username Changed. Return to main programme");

                    username_change.requestFocus();
                }
            }
        );         
    }

}

2 个答案:

答案 0 :(得分:1)

这是因为您没有先设置userName属性。您应先设置usernName,然后尝试getUserName()

在打印userName

之前,请尝试在actionPerformed方法中添加以下行
setUserName(username_input.getText());

这是经过纠正和测试的代码:

package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class ChangeUsername extends JFrame {

    private JLabel uT1, pwT, uCh, statusBar;
    private JTextField usernameInput, usernameChange;
    private JPasswordField passwordInput;
    public String userName, passWord;

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String givenUserName) {
        this.userName = givenUserName;
        System.out.println(getUserName());   /////// this correctly prints the variable
    }

    public ChangeUsername() {
        super("Change Username");
        setPreferredSize(new Dimension(400, 85));
        setLayout(null);

        statusBar = new JLabel("Enter your username");
        add(statusBar, BorderLayout.SOUTH);
        statusBar.setBounds(20, 130, 250, 30);

        uT1 = new JLabel("Username");
        usernameInput = new JTextField(10);

        pwT = new JLabel("Password");
        passwordInput = new JPasswordField(10);

        uCh = new JLabel("New Username");
        usernameChange = new JTextField(10);

        uT1.setBounds(10, 10, 150, 30);
        usernameInput.setBounds(100, 10, 100, 30);
        pwT.setBounds(10, 50, 150, 30);
        passwordInput.setBounds(100, 50, 100, 30);
        uCh.setBounds(10, 90, 150, 30);
        usernameChange.setBounds(100, 90, 100, 30);

        add(uT1);
        add(usernameInput);

        usernameInput.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                statusBar.setText("Enter your password");
                add(pwT);
                add(passwordInput);

                pwT.repaint();
                passwordInput.repaint();
                passwordInput.requestFocus();

                setUserName(usernameInput.getText());// setting the username
//                statusBar.setText(statusBar.getText() + " " + getUserName());
                System.out.println(getUserName()); ////// this line does not
            }
        });

    }

    public static void main(String argv[]) {
        new ChangeUsername().setVisible(true);
    }
}

另外我应该提一下,你应该开始使用Java命名约定,并为你的类,属性和方法使用有意义的名称。

  • 类以Upercase字母开头
  • 方法和属性以小写字母开头
  • 所有标识符都是camelCase,所以不要在它们之间使用_。写getUserName而不是get_user_name

阅读此link以获取有关Java命名约定的更多规则。

您需要了解有关OOP的更多信息。所以我建议阅读“Thinking in Java”一书。 TIJ third edition是免费且好的。

答案 1 :(得分:-1)

此处,您未设置文本字段的值。 做点什么

username_input.setText(userName);

将以上行放在setUserName(){}

public change_username() {
        super("Change Username"); 
        setPreferredSize (new Dimension (400, 85));
        setLayout (null);

        statusBar = new JLabel("Enter your username");
        add(statusBar, BorderLayout.SOUTH);
        statusBar.setBounds(20, 130, 250, 30); 

        uT1 = new JLabel("Username");
        username_input = new JTextField(10);

        pwT = new JLabel("Password");
        password_input = new JPasswordField(10); 

        uCh = new JLabel("New Username");
        username_change = new JTextField(10);

        uT1.setBounds(10, 10, 150, 30); 
        username_input.setBounds(100, 10, 100, 30); 
        pwT.setBounds(10, 50, 150, 30); 
        password_input.setBounds(100, 50, 100, 30);        
        uCh.setBounds(10, 90, 150, 30); 
        username_change.setBounds(100, 90, 100, 30); 

        add(uT1);
        add(username_input);

        // SET THE TEXT HERE Before the Listener **************************

        username_input.setText(getUserName());

        // *****************************************************************

然后你的代码提前.....