Java设置专注于不同的JFrame

时间:2015-12-03 03:27:58

标签: java swing nullpointerexception

现在我知道这是一个糟糕的方法,但我正在制作一个程序,我需要用户登录。我想如果我创建了两个不同的JFrame(一个登录,一个登记我的程序)我可以改变他们各自的可见性状态来决定我想要显示哪个帧。

我创建了登录屏幕,当您点击登录时,它会检查凭据,它应该隐藏登录屏幕并显示主程序的屏幕。

我尝试处理登录屏幕,并分别更改屏幕的可见性状态,并且我一直收到空指针异常。该程序确实继续运行,但我不认为继续这个错误是好的。

我不确定如何摆脱登录界面。 这是我的课程和迄今为止的三个班级的基本草图:

import java.awt.EventQueue;

public class Class1 {



    public static void main(String args[]){
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Windows window = new Windows(); // localizes the window class
                    window.dMMWindow();
                    window.displayloginWindow(); //calls for the window method inside of the window class

                } catch (Exception e) { //catches any exceptions
                    e.printStackTrace();
                }
            }
        });
    }

}

和第二类

 import java.awt.Button;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;



import org.eclipse.wb.swing.FocusTraversalOnArray;

public class Windows {

    private JFrame loginWindow;
    private JFrame ErrorWindow;
    private JFrame moneyManagerWindow;
    private JTextField inputUsername;
    private JTextField inputPassword;


    /**
     * Displays login window
     */
    public void displayloginWindow(){
        createLoginWindow();
        loginWindow.setVisible(true);
    }

    public void disposeWindow(String frameName){ //disposes specified windows
        switch (frameName){ //switch statement to close specified windows
        case "loginWindow":
            loginWindow.setVisible(false);
        break;

        case "ErrorWindow":
            ErrorWindow.setVisible(false);
        break;

        case "moneyManagerWindow":
            moneyManagerWindow.setVisible(false);
        break;

        default:

        break;

        }

    }

    public void displayErrorWindow(){
        createErrorWindow();
        ErrorWindow.setVisible(true);
    }

    public void dMMWindow(){
        createMoneyManagerWindow();
        moneyManagerWindow.setVisible(true);

    }

    private void createMoneyManagerWindow() {
        moneyManagerWindow = new JFrame();
        moneyManagerWindow.setResizable(false);
        moneyManagerWindow.setTitle("Money Manager");
        moneyManagerWindow.setBounds(100, 100, 621, 490);
        moneyManagerWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        moneyManagerWindow.setLocationRelativeTo(null);

        JTextPane welcomePane = new JTextPane();
        welcomePane.setEditable(false);
        welcomePane.setBackground(SystemColor.menu);
        welcomePane.setText("Welcome: ");
        welcomePane.setBounds(12, 13, 230, 22);
        moneyManagerWindow.getContentPane().add(welcomePane);

    }

    private void createErrorWindow() {
        ErrorWindow = new JFrame();
        ErrorWindow.setTitle("Error");
        ErrorWindow.setResizable(false);
        ErrorWindow.setBounds(100, 100, 353, 193);
        ErrorWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        ErrorWindow.setLocationRelativeTo(null);
        ErrorWindow.setAlwaysOnTop(true);

        JTextPane txtpnIncorrectUsernameOr = new JTextPane();
        txtpnIncorrectUsernameOr.setBackground(SystemColor.menu);
        txtpnIncorrectUsernameOr.setEditable(false);
        txtpnIncorrectUsernameOr.setBounds(80, 30, 207, 22);
        txtpnIncorrectUsernameOr.setText("Incorrect username or password");

        JButton contButton = new JButton("Continue");
        contButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                ErrorWindow.dispose();
            }
        });
        contButton.setBounds(127, 79, 97, 45);
        ErrorWindow.getContentPane().setLayout(null);
        ErrorWindow.getContentPane().add(txtpnIncorrectUsernameOr);
        ErrorWindow.getContentPane().add(contButton);
    }


    /**
     * Initialize the contents of the frame.
     */
    private void createLoginWindow() {
        loginWindow = new JFrame();
        loginWindow.setResizable(false);
        loginWindow.setTitle("Money Planner");
        loginWindow.setBounds(100, 100, 450, 234);
        loginWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        loginWindow.setLocationRelativeTo(null);        
        loginWindow.getContentPane().setLayout(null);
        loginWindow.isAlwaysOnTop();

        Button cAccButton = new Button("Create Account");
        cAccButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //when button is clicked
            }
        });
        cAccButton.setBounds(100, 151, 104, 24);
        loginWindow.getContentPane().add(cAccButton);

        Button loginButton = new Button("Login");
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String username = inputUsername.getText(); // sets username equal to whatever was in the username text field
                String password = inputPassword.getText(); // sets password equal to whatever was in the password text field
                Users users = new Users(); //localizes the users class
                try{
                users.analyze(username, password); // sends the local variables "username" and "password" over to the analyze method in the Uers class
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        loginButton.setBounds(232, 151, 104, 24);
        loginWindow.getContentPane().add(loginButton);

        inputUsername = new JTextField();
        inputUsername.setBounds(180, 61, 116, 22);
        loginWindow.getContentPane().add(inputUsername);
        inputUsername.setColumns(10);

        inputPassword = new JTextField();
        inputPassword.setBounds(180, 96, 116, 22);
        loginWindow.getContentPane().add(inputPassword);
        inputPassword.setColumns(10);
        loginWindow.getContentPane().setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{inputUsername, inputPassword, cAccButton, loginButton}));
        loginWindow.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{inputUsername, inputPassword, loginButton, cAccButton}));
    }
}

最后一堂课

    public class Users {

    private String username = "q"; //created random username for debugging
    private String password = "1"; //created random password for debugging


    public void analyze(String username, String password){ //accepts username and password
        Windows window = new Windows();
        //System.out.println("username :" + username + " password: " + password); //debugging
        if (this.username.equals(username)){ //tests to see if the classes username equals the inputed username
            if (this.password.equals(password)){ //tests to see if the classespassword equals the inputed password  
                window.dMMWindow();
                window.disposeWindow("loginWindow");
                    //what to do once logged in
                    //function once botch username  and password match
            } else { 
                window.displayErrorWindow(); // output for incorrect information
            }
        } else if (!this.username.equals(username)){ //not sure if I need this but i'll keep it
            window.displayErrorWindow();
        }
    }


}

0 个答案:

没有答案