0

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

如何在另一个类中使用在 class1 中创建的变量?

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

但是,当我尝试将该变量打印为动作侦听器的一部分时,它会打印出“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();
                }
            }
        );         
    }

}
4

2 回答 2

1

那是因为你没有userName先设置属性。你应该usernName先设置然后尝试getUserName()

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 命名约定,并为您的类、属性和方法使用有意义的名称。

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

阅读此链接以了解有关 Java 命名约定的更多规则。

您需要了解更多有关 OOP 的信息。所以我建议阅读《Java 中的思考》一书。TIJ 第三版 是免费的而且很好。

于 2013-07-16T20:54:25.633 回答
-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());

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

然后你的代码在前面......

于 2013-07-16T20:40:22.057 回答