程序编译并运行但没有输出

时间:2013-08-02 13:20:16

标签: java swing jframe file-handling

目前,我已为登录身份验证创建了2个JFrame,一个JFrame,另一个是主菜单。问题是当我编译并运行程序时没有输出,屏幕只是空白。 NetBeans 编译器显示其正在运行但未显示任何内容。

以下是我所做的:这是登录框架代码。

public class Login_screen extends javax.swing.JFrame {

    /**
     * Creates new form Login_screen
     */
    public Login_screen() {
        initComponents();
        this.getContentPane().setBackground(new Color(0,176,80)); //changed the background colour of the frame
    }

    JFrame frame=new JFrame();

    Main_Menu_screen openscreen=new Main_Menu_screen();
    createLogin_class createAccount=new createLogin_class();

    private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try{
            libraryLogin_class loginObject=new libraryLogin_class();
            createAccount.login();
            String username=txtusername.getText();
            String password=new String(txtpassword.getPassword());

            FileInputStream file=new FileInputStream("login.txt");
            ObjectInputStream readFile=new ObjectInputStream(file);

            loginObject=(libraryLogin_class)readFile.readObject();

            readFile.close();
            if(loginObject.getUsername().equals(username) && 
                    loginObject.getPassword().equals(password)){

                JOptionPane.showMessageDialog(frame,"Login Successful");
                this.setVisible(false);
                openscreen.setVisible(true);
            }
            else
            {
                JOptionPane.showMessageDialog(frame,"Username/Password Incorrect!");
            }
        }catch(Exception e){}
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login_screen().setVisible(true);
            }
        });
    }
}

这是与登录框相关联的2个单独的类。

public class createLogin_class {

    /**
     * Below method creates a login with the given 
     * username and password and passes  them to createLogin method
     */

    public void login(){

        String username="shehan";
        String password="123";

        createLogin_class user=new createLogin_class();
        user.createLogin(username, password);
    }

    /**
     * This method calls the constructor of libraryLogin class 
     * and pass the parameters to create an object, 
     * after the object is created it is written in to a file.
     * @param username
     * @param password 
     */
    public void createLogin(String username,String password){

        libraryLogin_class logins=new libraryLogin_class(username,password);

        try{
            FileOutputStream write=new FileOutputStream("login.txt");
            ObjectOutputStream writeTofile=new ObjectOutputStream(write);

            writeTofile.writeObject(logins);
            writeTofile.flush();
            writeTofile.close();
        }catch(Exception e){}
    }
}

另一个班级:

public class libraryLogin_class implements Serializable {

    private String username;
    private String password;

    public libraryLogin_class(){}

    public libraryLogin_class(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

1 个答案:

答案 0 :(得分:0)

我设法自己找出问题所在。问题是由于递归引起的。一个构造函数正在调用自己,所以我改变了它,程序开始工作。