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

不打扰是莪最后的温柔 提交于 2019-12-24 19:04:14

问题


I’m trying to create a password programme that lets the user create an account and then be able to change username, password and access the account.

This is where I’m at so far:

class 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);
            }
        }
    }   
}

class 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;

    change_username objOfClass4 = new change_username();

    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();                   

                    objOfClass4.setUserName(create_u1.getText());
                }
            }
        );

        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();

                }
            }
        );

    }

}

class 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;   

    private String userName, passWord;


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

    public void setUserName(String givenUserName) {
        this.userName = givenUserName;
    }

    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();

                    System.out.println(getUserName());

                }
            }
        );       
    }

}

I want to access the username that the user entered in the JTextField in class 3, from class 4.

Before doing everything else, I want to print out the username that they entered when they created the account using println(). I've tried multiple ways of doing this, including getters, but none have worked and I'm stuck.

Can someone provide me the exact code that I need that lets me print out the username that they created in the 'create_username' class? I think I can manage to create the rest of the programme once I achieve this.

Thanks


回答1:


Create two private instance variable

public class change_username extends JFrame {

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

    // ADD IT HERE **************************************
    private String userName, passWord;

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

    public void setUserName(String givenUserName){
        this.userName = givenUserName;
    }

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


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

    // YOUR CODE HERE


// CLASS 3
public class create_account extends JFrame{
    private String u1, pw1;
    class4 objClass4 = new class4();
    u1 = objClass4.getUserName();

    // YOUR code here.......

Repeat the same for password, and you should be good. Also, when the Listener is activated use the object of the class to set the userName and the Password. Hope you know, on how to move forward from here.



来源:https://stackoverflow.com/questions/17679750/how-to-use-a-variable-created-in-class1-in-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!