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

时间:2013-07-16 14:45:35

标签: java class variables bluej

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

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

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

                }
            }
        );       
    }

}

我想访问用户在第3课的JTextField中输入的用户名。

在执行其他所有操作之前,我想打印出他们使用println()创建帐户时输入的用户名。我尝试了多种方法,包括吸气剂,但没有一种方法可行,而且我被困住了。

有人可以提供我需要的确切代码,让我打印出他们在'create_username'类中创建的用户名吗? 我想,一旦实现这一目标,我就可以创建其余的程序。

由于

1 个答案:

答案 0 :(得分:1)

创建两个私有实例变量

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

对密码重复相同,你应该很好。此外,激活监听器时,使用类的对象设置userName和Password。希望你知道如何从这里前进。