如何在另一个类中使用在class1中创建的变量? [英] How to use a variable created in class1, in another class?

查看:123
本文介绍了如何在另一个类中使用在class1中创建的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个密码程序,该程序允许用户创建一个帐户,然后可以更改用户名,密码并访问该帐户.

这是我到目前为止的位置:

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

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

                }
            }
        );

    }

}

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

                }
            }
        );       
    }

}

我想访问用户从类4在类3的JTextField中输入的用户名.

在进行其他操作之前,我想打印出他们在使用println()创建帐户时输入的用户名.我已经尝试过多种方法来做到这一点,包括使用吸气剂,但都没有奏效,我被困住了.

有人可以向我提供所需的确切代码,以便我打印出他们在"create_username"类中创建的用户名吗? 我认为一旦实现这一点,我就可以设法创建程序的其余部分.

谢谢

解决方案

创建两个私有实例变量

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.......

重复相同的密码,就可以了.另外,当侦听器被激活时,请使用该类的对象来设置用户名和密码.希望您知道如何从这里前进.

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

解决方案

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.

这篇关于如何在另一个类中使用在class1中创建的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆