帮助密码锁程序 [英] Help with Combination Lock program

查看:50
本文介绍了帮助密码锁程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在为大学做作业,我必须创建一个密码锁程序,在该程序中,单击三个按钮序列,然后关闭框架.我选择了3-9-3的顺序.我可以让它对第一个数字3做出反应,但是在此之后,如果对数字9进行逻辑运算,它将不会继续执行我的其他操作.有人可以向我提出任何有关如何编写此代码以便使其正常工作的建议吗?

Hi, I am doing an assignment for college where I have to create a combination lock program where I click a three button sequence and it closes the frame. I chose the sequence of 3-9-3. I can get it to react to the first number of 3 but after that it will not continue on to my else if logic to the number 9. Can anyone offer me any suggestions on how I can write this in order to get this to work?

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;

public class Assign3 extends JFrame implements ActionListener
{

  int count=0;
 
  // create buttons for panels
  JButton b0 = new JButton("0");
  JButton b1 = new JButton("1");
  JButton b2 = new JButton("2");
  JButton b3 = new JButton("3");
  JButton b4 = new JButton("4");
  JButton b5 = new JButton("5");
  JButton b6 = new JButton("6");
  JButton b7 = new JButton("7");
  JButton b8 = new JButton("8");
  JButton b9 = new JButton("9");

  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel panel3 = new JPanel();
  JPanel panel4 = new JPanel();


  public Assign3()
    {
      setTitle( "Combination Lock" );

	  // add label and text pairs to the small panels
      panel1.add( b0 );
      panel2.add( b1 ); panel2.add( b2 ); panel2.add( b3 );
      panel3.add( b4 ); panel3.add( b5 ); panel3.add( b6 );
      panel4.add( b7 ); panel4.add( b8 ); panel4.add( b9 );


      b0.setActionCommand( "0" );
	  b1.setActionCommand( "1" );
	  b2.setActionCommand( "2" );
	  b3.setActionCommand( "3" );
	  b4.setActionCommand( "4" );
	  b5.setActionCommand( "5" );
	  b6.setActionCommand( "6" );
	  b7.setActionCommand( "7" );
	  b8.setActionCommand( "8" );
	  b9.setActionCommand( "9" );


      setLayout( new FlowLayout() );
      add(panel1);
      panel1.setLayout( new BoxLayout( panel1, BoxLayout.X_AXIS ) );
      add(panel2);
      panel2.setLayout( new BoxLayout( panel2, BoxLayout.Y_AXIS ) );
      add(panel3);
      panel3.setLayout( new BoxLayout( panel3, BoxLayout.Y_AXIS ) );
      add(panel4);
      panel4.setLayout( new BoxLayout( panel4, BoxLayout.Y_AXIS ) );

      //register listener
      b0.addActionListener( this );
      b1.addActionListener( this );
      b2.addActionListener( this );
      b3.addActionListener( this );
      b4.addActionListener( this );
      b5.addActionListener( this );
      b6.addActionListener( this );
      b7.addActionListener( this );
      b8.addActionListener( this );
      b9.addActionListener( this );
     }

 public void actionPerformed( ActionEvent evt)
  {

    // check which command has been sent
    if ( count == 0 & evt.getActionCommand().equals( "3" ))
	 {
	  count =+ 1;
	  getContentPane().setBackground( Color.lightGray);
          repaint();
	 }
    else
	  if ( count == 1 & evt.getActionCommand().equals( "9" ))
	  {
	    count=+1;
	    getContentPane().setBackground(  Color.lightGray );
            repaint();
	  }
      else

        if ( evt.getActionCommand().equals( "3" ) & count == 2)
	  {
           this.dispose();
	  }
        else
           getContentPane().setBackground( Color.red );
           count = 0;
           repaint();
  }


    public static void main ( String[] args )
    {
      Assign3 frame  = new Assign3() ;
      frame.setSize( 275, 130 );
      frame.setVisible( true );
      frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
      frame.setBackground(  Color.lightGray );
    }
}

推荐答案

我必须注意,您所做的根本不是编程,而是相反的.编程全都涉及泛化和抽象.

一个真正的"软件开发人员将永远不会像10那样编写类似JButton b0 = new JButton("0");的声明.听说过数组吗?从来没有像b1.setActionCommand("1")b0.addActionListener( this );这样的东西.听说过数组吗?

如果您不认为这是错误的,我不知道该如何提供帮助.我可能会理解,这是否是您人生中第一次编写程序……

也许可以帮忙吗?
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [框架的使用和意义 [ http://norvig.com/21-days.html [
I must note that what you are doing is not programming at all, but is something opposite. Programming is all about generalization and abstraction.

A "real" software developer will never ever write a declaration like JButton b0 = new JButton("0"); 10 times. Ever heard of arrays? And never anything like b1.setActionCommand("1") or b0.addActionListener( this );. Ever heard of arrays?

If you don''t feel it''s wrong, I don''t know how to help. I would probably understand if this is a first attempt to write a program in your life…

Maybe, this can help?
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

Ever heard of this principle?

You really should either go in for programming or quite. If you choose the first, you probably need to start if from scratch and do it well, otherwise you just waste you time. Mayne you rather need simple exercises using console applications and an elementary book on language and programming to read from the beginning to the very end.

Please see:
using and meaningful of Framework[^],
http://norvig.com/21-days.html[^].

—SA


我真的是编程的初学者,这是大学里的编程课程,所以请弄清楚我的水平.感谢您提供的链接,我会将它们添加为书签,以备将来使用.这些方法是我在本课程的当前阅读中使用的工具,所以这就是我所使用的工具.但是我确实找到了解决方案,就是我在actionperfromper中保留了最后一个else语句的花括号,它看起来应该像这样,并且将计数更改为count ++:

I am really a beginner a programming and this is a programming course in college so figure out where my level is at. Thanks for the links, I will bookmark these for future use. These methods were the tools I was given to use with my current reading in my course so this is what I used. but I did find the solution which is that I left the braces off the last else statement in my actionperfromed and it should look like this as well as changing the count to count++:

  if ( evt.getActionCommand().equals( "3" ) & count == 2)
    {
     this.dispose();
    }
      else
        {
         getContentPane().setBackground( Color.red );
         count = 0;
         repaint();
        }
}


这种逻辑似乎还可以.您是否设置了断点以查看发生了什么?逐步执行代码是弄清楚为什么您的预期逻辑无法在代码中工作的第一方法.
This logic seems OK. Have you set a breakpoint to see what is happening ? Stepping through your code is the number one way to work out why your intended logic is not working in code.


这篇关于帮助密码锁程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆