Java从另一个类访问JTextField

时间:2018-02-27 18:14:51

标签: java swing oop jframe jtextfield

我想访问JTextField,但我不能,因为它在另一个类中。我的GUI和我的ActionListener类是分开的,但我想从另一个类访问JTextField,我有问题这样做。很抱歉,代码非常长,当我运行它时,它给了我nullpointer异常。我应该如何从BusinessLogic类访问JTextField?这是我的代码: BankApp课程:

import java.awt.CardLayout;
import static java.awt.Component.CENTER_ALIGNMENT;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class BankApp {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            BankGUI object1;
            object1 = new BankGUI();
            object1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            object1.setSize(600, 400);
            object1.setLocationRelativeTo(null);
            object1.setVisible(true);
        }
    });
}
}

BankGUI:

import java.awt.*;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;

public class BankGUI extends JFrame {

    private static CardLayoutBankNavigationController controller;
    public static JTextField NameField;

    public BankGUI() {
        super("Banking App");
        CardLayout layout = new CardLayout();
        setLayout(layout);
        controller = new CardLayoutBankNavigationController(getContentPane(), layout);

        add("Main Menu", createMainMenu());
        add("Register", RegisterView("Register"));
        add("Login", LoginView("Login"));
        add("About", About("About"));
        add("Exit", otherView("Exit"));

        controller.setView("Main Menu");
    }

    public static JPanel About(String named) {
        JPanel About= new JPanel(new GridBagLayout());
        JTextArea AboutTextArea = new JTextArea(8,35);
        JButton MainMenuButton = new JButton("Back");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,10,10,10);
        gbc.anchor = GridBagConstraints.CENTER;
        AboutTextArea.setWrapStyleWord(true);
        AboutTextArea.setEditable(false);
        AboutTextArea.setLineWrap(true);
        AboutTextArea.setFont(new Font("Serif", Font.PLAIN, 22));
        AboutTextArea.setText("This is a banking app made by Velizar Mitrev. "
                + "\nIts created only for learning purposes and its my first GUI application. "
                + "Its application that simulates online banking applications where you create account "
                + "and in the account you have your money. "
                + "\nHave Fun with it!");
        gbc.gridx = 0;
        gbc.gridy = 0;
        About.add(AboutTextArea, gbc);

        MainMenuButton.setFocusable(false);
        MainMenuButton.setFont(new Font("Serif", Font.PLAIN, 24));

        gbc.gridx = 0;
        gbc.gridy = 1;
        About.add(MainMenuButton, gbc);


        ////////////////
        ButtonListener actionListener = new ButtonListener(controller, null);
        MainMenuButton.addActionListener(actionListener);

        return About;
    }

    ///////////////////////////////////////////////////////
    public static JPanel RegisterView(String named) {
        JPanel Register = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,15,10,15);
        gbc.anchor = GridBagConstraints.WEST;
        JLabel Name;
        JLabel Surname;
        JTextField SurnameField;
        JLabel Password;
        JPasswordField PasswordField;
        JLabel ConfirmPassword;
        JPasswordField ConfirmPasswordField;
        JLabel Email;
        JTextField EmailField;
        JButton RegisterButton;
        JButton MainMenuButton;

        Name= new JLabel("Name:");
        Name.setFont(new Font("Serif", Font.PLAIN, 24));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.weighty = 1;

        Register.add((Name), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        NameField = new JTextField(15);
        NameField.setFont(new Font("Serif",Font.PLAIN,20));

        Register.add(NameField, gbc);


        Surname = new JLabel("Surname:");
        Surname.setFont(new Font("Serif", Font.PLAIN, 24));
        gbc.gridx = 0;
        gbc.gridy = 1;

        Register.add((Surname), gbc);

        SurnameField = new JTextField(15);
        SurnameField.setFont(new Font("Serif",Font.PLAIN,20));

        gbc.gridx = 1;
        gbc.gridy = 1;

        Register.add(SurnameField, gbc);

        Password = new JLabel("Password:");
        Password.setFont(new Font("Serif", Font.PLAIN, 24));
        gbc.gridx = 0;
        gbc.gridy = 2;

        Register.add((Password), gbc);

        PasswordField = new JPasswordField(15);
        PasswordField.setFont(new Font("Serif",Font.PLAIN,20));
        gbc.gridx = 1;
        gbc.gridy = 2;


        Register.add(PasswordField, gbc);

        ConfirmPassword = new JLabel("Confirm password:");
        ConfirmPassword.setFont(new Font("Serif", Font.PLAIN, 22));
        gbc.gridx = 0;
        gbc.gridy = 3;

        Register.add((ConfirmPassword), gbc);

        ConfirmPasswordField = new JPasswordField(15);
        ConfirmPasswordField.setFont(new Font("Serif",Font.PLAIN,20));
        gbc.gridx = 1;
        gbc.gridy = 3;

        Register.add(ConfirmPasswordField, gbc);

        Email = new JLabel("Email:");
        Email.setFont(new Font("Serif", Font.PLAIN, 24));
        gbc.gridx = 0;
        gbc.gridy = 4;

        Register.add((Email), gbc);

        EmailField = new JTextField(15);
        EmailField.setFont(new Font("Serif",Font.PLAIN,20));
        gbc.gridx = 1;
        gbc.gridy = 4;


        Register.add(EmailField, gbc);

        MainMenuButton = new JButton("Back");
        MainMenuButton.setFont(new Font("Serif", Font.PLAIN, 24));
        MainMenuButton.setPreferredSize(new Dimension(120, 40));
        MainMenuButton.setFocusable(false);
        gbc.gridx = 0;
        gbc.gridy = 5;

        Register.add((MainMenuButton), gbc);

        RegisterButton = new JButton("Register");
        RegisterButton.setFont(new Font("Serif", Font.PLAIN, 24));
        RegisterButton.setPreferredSize(new Dimension(150, 40));
        RegisterButton.setFocusable(false);
        gbc.gridx = 1;
        gbc.gridy = 5;


        Register.add(RegisterButton, gbc);
        ////////////////
        ButtonListener actionListener = new ButtonListener(controller, null);
        MainMenuButton.addActionListener(actionListener);
        RegisterButton.addActionListener(actionListener);

        return Register;
    }

    public static JPanel LoginView(String named) {
        JPanel Login= new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,15,20,15);
        gbc.anchor = GridBagConstraints.NORTH;
        JLabel Name;
        JLabel Password;
        JTextField NameField;
        JPasswordField PasswordField;
        JButton LoginButton;
        JButton MainMenuButton;



        Name = new JLabel("Name:");
        Name.setFont(new Font("Serif", Font.PLAIN, 24));
        gbc.gridx = 0;
        gbc.gridy = 0;
        Login.add(Name, gbc);

        NameField = new JTextField(15);
        NameField.setFont(new Font("Serif",Font.PLAIN,20));
        gbc.gridx = 1;
        gbc.gridy = 0;
        Login.add(NameField, gbc);


        Password = new JLabel("Password:");
        Password.setFont(new Font("Serif", Font.PLAIN, 24));
        gbc.gridx = 0;
        gbc.gridy = 1;
        Login.add(Password, gbc);

        PasswordField = new JPasswordField(15);
        PasswordField.setFont(new Font("Serif",Font.PLAIN,20));
        gbc.gridx = 1;
        gbc.gridy = 1;
        Login.add(PasswordField, gbc);

        MainMenuButton = new JButton("Back");
        MainMenuButton.setFont(new Font("Serif", Font.PLAIN, 24));
        MainMenuButton.setPreferredSize(new Dimension(120, 40));
        MainMenuButton.setFocusable(false);
        gbc.gridx = 0;
        gbc.gridy = 2;
        Login.add(MainMenuButton, gbc);

        LoginButton = new JButton("Login");
        LoginButton.setFont(new Font("Serif", Font.PLAIN, 24));
        LoginButton.setPreferredSize(new Dimension(120, 40));
        LoginButton.setFocusable(false);
        gbc.gridx = 1;
        gbc.gridy = 2;
        Login.add(LoginButton, gbc);

        ButtonListener actionListener = new ButtonListener(controller, null);
        MainMenuButton.addActionListener(actionListener);
        LoginButton.addActionListener(actionListener);

        return Login;
    }
   ////////////////////////////////////////////////////////
    public static JPanel otherView(String named) {
        JPanel view = new JPanel();
        view.setLayout(new BoxLayout(view, BoxLayout.PAGE_AXIS));

        JButton mainMenu = new JButton("Main Menu");
        view.add(mainMenu);

        ButtonListener actionListener = new ButtonListener(controller, null);
        mainMenu.addActionListener(actionListener);

        return view;
    }


    public static JPanel createMainMenu() {
        JPanel menu = new JPanel();
        menu.setLayout(new BoxLayout(menu, BoxLayout.PAGE_AXIS));

        menu.add(Box.createRigidArea(new Dimension(0, 10)));
        JLabel IntroText = new JLabel("Banking Application");
        IntroText.setMaximumSize(new Dimension(280, 60));
        IntroText.setFont(new Font("Serif", Font.PLAIN, 34));
        IntroText.setAlignmentX(CENTER_ALIGNMENT);
        menu.add(IntroText);

        menu.add(Box.createRigidArea(new Dimension(0, 20)));

        JButton CreateAccount = new JButton("Register");
        CreateAccount.setMaximumSize(new Dimension(200, 50));
        CreateAccount.setFont(new Font("Serif", Font.PLAIN, 24));
        CreateAccount.setAlignmentX(CENTER_ALIGNMENT);
        CreateAccount.setFocusable(false);
        CreateAccount.setActionCommand("CreateAccount");
        menu.add(CreateAccount);

        menu.add(Box.createRigidArea(new Dimension(0, 20)));

        JButton LoginAccount = new JButton("Login");
        LoginAccount.setMaximumSize(new Dimension(200, 50));
        LoginAccount.setFont(new Font("Serif", Font.PLAIN, 24));
        LoginAccount.setAlignmentX(CENTER_ALIGNMENT);
        LoginAccount.setFocusable(false);
        LoginAccount.setActionCommand("LoginAccount");
        menu.add(LoginAccount);

        menu.add(Box.createRigidArea(new Dimension(0, 20)));

        JButton AboutButton = new JButton("About");
        AboutButton.setMaximumSize(new Dimension(200, 50));
        AboutButton.setFont(new Font("Serif", Font.PLAIN, 24));
        AboutButton.setAlignmentX(CENTER_ALIGNMENT);
        AboutButton.setFocusable(false);
        menu.add(AboutButton);

        menu.add(Box.createRigidArea(new Dimension(0, 20)));

        JButton ExitButton = new JButton("Exit");
        ExitButton.setMaximumSize(new Dimension(200, 50));
        ExitButton.setFont(new Font("Serif", Font.PLAIN, 24));
        ExitButton.setAlignmentX(CENTER_ALIGNMENT);
        ExitButton.setFocusable(false);
        menu.add(ExitButton);

        ButtonListener actionListener = new ButtonListener(controller, null);
        CreateAccount.addActionListener(actionListener);
        LoginAccount.addActionListener(actionListener);
        AboutButton.addActionListener(actionListener);
        ExitButton.addActionListener(actionListener);

        return menu;
    }

    public JTextField getTextField() {
        return NameField;
    }

    }

这是我的ActionListener类:

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

public class ButtonListener implements ActionListener {

private BankNavigationController controller;
BankGUI gui;
BusinessLogic BL;

public ButtonListener(BankNavigationController controller, BankGUI gui) {
    this.gui = gui;
    this.controller = controller;
}

public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand() == "CreateAccount"){
        controller.setView("Register");
    }
    if(e.getActionCommand() == "LoginAccount"){
        controller.setView("Login");
    }
    if(e.getActionCommand() == "Back"){
        controller.setView("Main Menu");
    }
    if(e.getActionCommand() == "About"){
        controller.setView("About");
    }
    if(e.getActionCommand() == "Exit"){
        System.exit(0);
    }
    if(e.getActionCommand() == "Exit"){
        System.exit(0);
    }
    if(e.getActionCommand() == "Register")
    {
        BL.registerAccount(gui);
    }
}


}

这是我的CardLayout:

import java.awt.*;

public class CardLayoutBankNavigationController implements 
BankNavigationController {

    private Container parent;
    private CardLayout layout;

    public CardLayoutBankNavigationController(Container parent, CardLayout layout) {
        this.parent = parent;
        this.layout = layout;
    }

    @Override
    public void setView(String command) {
        layout.show(parent, command);
    }

}

这是BankNavigationInterface

public interface BankNavigationController {

    public void setView(String command);
}

这是BusinessLogic类:

import java.util.HashMap;
import java.util.Map;

public class BusinessLogic {

Map<String, UserAccount> UserAccounts = new HashMap<String, UserAccount>();


public BusinessLogic(BankGUI gui){

}

public void registerAccount(BankGUI gui){
    System.out.println(gui.getTextField());
}
}

我的问题是如何从BusinessLogic访问BankGUI中的JTextField?

0 个答案:

没有答案