有没有办法在文件中实现多个类而无需重新键入整个代码?

时间:2013-11-20 17:47:03

标签: java class import

我最近写了一个程序,它是一个基本的用户/密码JFrame密码登录。像往常一样,代码相当长。我想知道的是,是否有办法实现此代码/文件,而无需将整个类复制/粘贴到另一个文件中。 即能够通过某种方式在另一个程序的介绍中引用该类,而不必重新输入它。

这是我的代码:

    import javax.swing.JOptionPane.*;
    import java.lang.Math.*;
    import java.lang.System.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.JFrame;

    public class UserLog extends JFrame  

    {

    public static void main(String[]Args) throws InterruptedException 
        {
        boolean isValid=false;
        while(!isValid)
            {
        // Components related to "login" field    
        JLabel label_loginname = new JLabel("Enter your login name:");    
        JTextField loginname = new JTextField(15);    
        // loginname.setText("EnterLoginNameHere"); 
        // Pre-set some text    
        // Components related to "password" field    
        JLabel label_password = new JLabel("Enter your password:");    
        JPasswordField password = new JPasswordField();    
        // password.setEchoChar('@'); 
        // Sets @ as masking character    
        // password.setEchoChar('\000'); 
        // Turns off masking    
        JCheckBox rememberCB = new JCheckBox("Remember me");

        Object[] array = {label_loginname,
        loginname,                       
        label_password,                       
        password,                       
        rememberCB};
        Object[] options = {"Login", "Cancel"};
        int res = JOptionPane.showOptionDialog(null,
                                                array,
                                                "Login",
                                                JOptionPane.YES_NO_OPTION,
                                                JOptionPane.QUESTION_MESSAGE,
                                                null,     //do not use a custom Icon
                                                options,  //the titles of buttons
                                                options[0]); //default button title

        // User hit Login    
        if (res == 0) 
            { 
                System.out.println( "Login" ); 
            }    
        // User hit CANCEL    
        if (res == 1) 
            { 
                System.out.println( "Canceled" ); 
            }    
        // User closed the window without hitting any button    
        if (res == JOptionPane.CLOSED_OPTION) 
            { 
                System.out.println( "CLOSED_OPTION" ); 
            }


        // Output data in "login" field, if any    
        String newloginname = loginname.getText();    
        String newpassword = new String(password.getPassword());    
        if (newloginname.equalsIgnoreCase("Cody_Coulter") && newpassword.equals("cheche1"))
            {
                System.out.println("Login Successful!");
                boolean selectedCB = rememberCB.isSelected();    
                System.out.println( "selectedCB: " + selectedCB );
                Thread.sleep(3000);
                Object[] array1= {"It's about time to choose"};
                Object[] options1= {"Leave", "Keep Going"};
                int res1 = JOptionPane.showOptionDialog(null,
                                                array1,
                                                "There",
                                                JOptionPane.YES_NO_OPTION,
                                                JOptionPane.QUESTION_MESSAGE,
                                                null,     //do not use a custom Icon
                                                options1,  //the titles of buttons
                                                options1[0]); //default button title
                if(res1==1)
                    {
                        Object[] options2 = {"Answers for Algebra", 
                                             "Answers for APUSH",
                                             "Answers for Computer Science"};
                        Object[] array2={"Pick Your Poison:"};
                        int res2= JOptionPane.showOptionDialog(null,
                                                    array2,
                                                    "This",
                                                    JOptionPane.YES_NO_OPTION,
                                                    JOptionPane.QUESTION_MESSAGE,
                                                    null,     //do not use a custom Icon
                                                    options2,  //the titles of buttons
                                                    options2[0]); //default button title
                        if (res2 == 0) 
                        { 
                            JOptionPane.showMessageDialog(null, "Nigguh you a cheatuh" ); 
                        }    
                        else
                        if (res2 == 1) 
                        { 
                            JOptionPane.showMessageDialog(null, "Nigguh, who's dumb enough to need to cheat in APUSH" ); 
                        }
                        else
                        if (res2 == 2) 
                        { 
                            JOptionPane.showMessageDialog(null, "Nigguh, you dumb" ); 
                        }     

                        String name1 = JOptionPane.showInputDialog(null,
                                                            "What is your name?");
                        int length = 0;
                        length = newpassword.length();
                        String Pass = "*";
                        newpassword =newpassword.replaceAll(".","*");
                        System.out.println("Username: "+newloginname+"\nPassword: "+
                                            newpassword+"\nName: "+name1);
                    }

            }
        else {
                JOptionPane.showMessageDialog(null,"Wrong Username or Password!");
                isValid=false;
             }

            }
        // Output data in "password" field, if any    
        // Output state of "remember me" check box    

        }

    }

你可以按照

的方式做点什么吗?
public class newProgram {
     public static void main(String[]Args){
         final private class UserLog {
                                     }
     //The rest of the new program

1 个答案:

答案 0 :(得分:4)

你的程序只不过是一个庞大的主要方法。你最好完全废弃 并重新创建它,但从一开始就使用面向对象的原则,包括创建具有状态和行为的类。

这是创建灵活且可重复使用的代码的最佳方式。我建议你得到一本不错的Java书并通过它。