如果密码正确,请检查程序

时间:2016-05-22 02:50:06

标签: java swing jpanel jtextfield

我正在为学校编写程序,我希望它检查输入的密码是否正确,但即使密码不正确,程序仍会继续运行。以下是我的整个计划!

    public class ma_RocketProgram
{

  static Console c;


//***********************************Main Program******************************

  public static void main (String[] args)
  {

    c = new Console (35, 85);

    c.setColor (Color.black);
    c.fillRect (0, 0, 700, 550);

    Image picture1 = loadImage ("USA.png");      
    c.drawImage (picture1, 200, 100, null);

    IntroMessage();

    c.getChar();

    Password();

    c.setColor (Color.blue);

    c.drawRect (175, 475, 300, 10);

    for (int a = 176; a <= 465; a++)
    {

      c.setColor (Color.green);

      c.fillRect(a, 476, 10, 9);

      try
      {

        Thread.sleep(25);

      }

      catch (InterruptedException ex) 
      { 

        Thread.currentThread ().interrupt (); 

      }

    }

  }

//********************************Image Loader Method**************************

  public static Image loadImage (String name)
  {
    Image img = null;

    try
    {

      img = ImageIO.read (new File (name));

    }

    catch (IOException e)
    {

    }

    return img;

  }

//******************************Intro Message*************************************

  public static void IntroMessage()

  {

    Font f = new Font ("Courier New", Font.PLAIN, 30);

    c.setColor (Color.green);
    c.setFont (f);

    c.drawString ("W", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString (" e", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("  l", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("   c", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("    o", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("     m", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("      e", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("        t", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("         o", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("           t", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("            h", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("             e", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("               R", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                o", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                 c", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                  k", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                   e", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                    t", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                      L", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                       a", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                        u", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                         n", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                          c", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                           h", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                            e", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("                             r", 60, 25);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("P", 250, 60);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString (" r", 250, 60);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("  o", 250, 60);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("   g", 250, 60);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("    r", 250, 60);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("     a", 250, 60);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.drawString ("      m", 250, 60);

    try
    {

      Thread.sleep(50);

    }

    catch (InterruptedException ex) 
    { 

      Thread.currentThread ().interrupt (); 

    }

    c.setColor(Color.white);

    c.drawRect(215, 360, 215, 55);

    Font f2 = new Font ("Courier New", Font.PLAIN, 15);

    c.setColor (Color.green);
    c.setFont (f2);

    c.drawString ("Press any key to", 245, 375);
    c.drawString ("display password field", 225, 400);

  }

//************************************Password*************************

  public static void Password()
  {

    JPanel panel = new JPanel ();

    JLabel label = new JLabel ("Enter the password");

    JPasswordField pass = new JPasswordField(10);

    panel.add (label);
    panel.add (pass);

    String[] options = new String[]{"Enter", "Cancel"};

    int option = JOptionPane.showOptionDialog (null, panel, "Rocket Launcher", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);

    if (option == 0)
    {

      char[] password = pass.getPassword();

      char[] passw = new char[]{'1'};

      boolean b = Arrays.equals(password, passw);

      if (b = false)
      {

        System.exit(0);

      }

    }

  }


}

1 个答案:

答案 0 :(得分:2)

快速测试显示......

char[] password = new char[]{'u', 'n', 'k', 'w', 'o', 'n'};
char[] passw = new char[]{'u', 'n', 'k', 'w', 'o', 'n'};

if (Arrays.equals(password, passw)) {
    System.out.println("Are equal");
} else {
    System.out.println("Are not equal");
}

打印Are equal

char[] password = new char[]{'u', 'n', 'k', 'w', 'o', 'n'};
char[] passw = new char[]{'U', 'n', 'k', 'w', 'o', 'n'};

if (Arrays.equals(password, passw)) {
    System.out.println("Are equal");
} else {
    System.out.println("Are not equal");
}

打印Are not equal

从概念上讲,这证明Arrays.equals有效

快速浏览一下JOptionPane JavaDocs for showOptionDialog精彩集锦

  

返回:
  一个整数,表示用户选择的选项,如果用户关闭了对话框,则为CLOSED_OPTION

虽然有点模糊,但这意味着,在您的情况下,如果用户选择 Enter 或用户的0选择取消,它将返回1

因此,基于此,您的代码仅在用户选择取消

时才有效

这是System.out.println(...)打印出变量实际值

非常有用的地方

所以你应该看起来更像......

JPanel panel = new JPanel();
JLabel label = new JLabel("Enter the password");
JPasswordField pass = new JPasswordField(10);

panel.add(label);
panel.add(pass);

String[] options = new String[]{"Enter", "Cancel"};

int option = JOptionPane.showOptionDialog(null, panel, "Rocket Launcher", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);

if (option == 0) {
    char[] password = pass.getPassword();
    char[] passw = new char[]{'u', 'n', 'k', 'w', 'o', 'n'};

    if (Arrays.equals(password, passw)) {
        System.out.print("Right");
    } else {
        System.exit(0);
    }
}