Jump to content

Chip and Pin Keypad GUI

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Deco

Deco

    Newbie

  • Members
  • PipPip
  • 28 posts
I'm trying to make a program which creates a GUI keypad. It has digits 0-9, Enter and Clear Buttons.
I'm currently stuck on how to create an action Listener that will store each digit the user clicks and output to the user a hash symbol (the output screen is located in another class)

It seems like such a simple concept yet my code will not compile. Any advise on where I'm going wrong or what I have missed out is much appreciated.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;


class Keypad

{

	

	int entry = 0;

	char [] pin = new char[6];

	

	public void displayKeypad()

	{

		JFrame gridFrame = new JFrame("Keypad");

		gridFrame.setVisible(true);

		gridFrame.setSize(150,200);

		JPanel gridPanel = new JPanel();

		gridFrame.add(gridPanel);

		GridLayout grid = new GridLayout(4,3);

		gridPanel.setLayout(grid);

		

		JButton button1 = new JButton("1");

		button1.setActionCommand("1");

		button1.addActionListener(this);

		

		JButton button2 = new JButton("2");

		button2.setActionCommand("2");

		

		JButton button3 = new JButton("3");

		button3.setActionCommand("3");

		

		JButton button4 = new JButton("4");

		button4.setActionCommand("4");

		

		JButton button5 = new JButton("5");

		button5.setActionCommand("5");

		

		JButton button6 = new JButton("6");

		button6.setActionCommand("6");

		

		JButton button7 = new JButton("7");

		button7.setActionCommand("7");

		

		JButton button8 = new JButton("8");

		button8.setActionCommand("8");

		

		JButton button9 = new JButton("9");

		button9.setActionCommand("9");

		

		JButton button0 = new JButton("0");

		button0.setActionCommand("0");

		

		JButton buttonE = new JButton("Enter");

		

		JButton buttonC = new JButton("Clear");

		

		gridPanel.add(button1);

		gridPanel.add(button2);

		gridPanel.add(button3);

		gridPanel.add(button4);

		gridPanel.add(button5);

		gridPanel.add(button6);

		gridPanel.add(button7);

		gridPanel.add(button8);

		gridPanel.add(button9);

		gridPanel.add(buttonC);

		gridPanel.add(button0);

		gridPanel.add(buttonE);

	}

	

	public void ActionPerformed(ActionEvent e)

	{

		pin[entry] = e.getActionCommand();

		entry++;

	}

			

}


#2
ksemeks

ksemeks

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
e.getActionCommand(); returns a String, while pin is a char.

getActionCommand

public String getActionCommand()
Returns the command string associated with this action. This string allows a "modal" component to specify one of several commands, depending on its state. For example, a single button might toggle between "show details" and "hide details". The source object and the event would be the same in each case, but the command string would identify the intended action.
Returns:
the string identifying the command for this event
Look up the javadoc.
// d-_-b+

#3
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
I would take a slightly different approach to this. I made your code compile and gave you a good example of an action listener.
public class Keypad {
    int entry = 0;
    char[] pin = new char[6];
    
    public void displayKeypad()
    {
        JFrame gridFrame = new JFrame("Keypad");
        gridFrame.setSize(150,200);
        JPanel gridPanel = new JPanel();
        gridFrame.add(gridPanel);
        GridLayout grid = new GridLayout(4,3);
        gridPanel.setLayout(grid);
        
        JButton button1 = new JButton("1");
        button1.setActionCommand("1");
        button1.addActionListener(new MyActionListener(this));
        
        JButton button2 = new JButton("2");
        button2.setActionCommand("2");
        button2.addActionListener(new MyActionListener(this));
        
        JButton button3 = new JButton("3");
        button3.setActionCommand("3");
        button3.addActionListener(new MyActionListener(this));
        
        JButton button4 = new JButton("4");
        button4.setActionCommand("4");
        button4.addActionListener(new MyActionListener(this));
        
        JButton button5 = new JButton("5");
        button5.setActionCommand("5");
        button5.addActionListener(new MyActionListener(this));
        
        JButton button6 = new JButton("6");
        button6.setActionCommand("6");
        button6.addActionListener(new MyActionListener(this));
        
        JButton button7 = new JButton("7");
        button7.setActionCommand("7");
        button7.addActionListener(new MyActionListener(this));
        
        JButton button8 = new JButton("8");
        button8.setActionCommand("8");
        button8.addActionListener(new MyActionListener(this));
        
        JButton button9 = new JButton("9");
        button9.setActionCommand("9");
        button9.addActionListener(new MyActionListener(this));
        
        JButton button0 = new JButton("0");
        button0.setActionCommand("0");
        button0.addActionListener(new MyActionListener(this));
        
        JButton buttonE = new JButton("Enter");
        
        JButton buttonC = new JButton("Clear");
        
        gridPanel.add(button1);
        gridPanel.add(button2);
        gridPanel.add(button3);
        gridPanel.add(button4);
        gridPanel.add(button5);
        gridPanel.add(button6);
        gridPanel.add(button7);
        gridPanel.add(button8);
        gridPanel.add(button9);
        gridPanel.add(buttonC);
        gridPanel.add(button0);
        gridPanel.add(buttonE);
        gridFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gridFrame.setVisible(true);
    }
    
    class MyActionListener implements ActionListener {
        
        KeyPad t;
        
        public MyActionListener(KeyPad t){
            this.t = t;
        }

        public void actionPerformed(ActionEvent ae) {
            t.pin[t.entry] = ae.getActionCommand().charAt(0);
            t.entry++;
            //System.out.println("Entry = "+t.entry);
        }

    }

    public static void main(String args[]){
        KeyPad t = new KeyPad();
        t.displayKeypad();
    }
}

twas brillig

#4
Deco

Deco

    Newbie

  • Members
  • PipPip
  • 28 posts

tate said:

I would take a slightly different approach to this. I made your code compile and gave you a good example of an action listener.

      

1)  button1.addActionListener(new MyActionListener(this));

        

       

    

    class MyActionListener implements ActionListener

 {

        

     2)   KeyPad t;

        

        public MyActionListener(KeyPad t){

            this.t = t;

        }

}

This works great. I don't understand how the above code works:

1) 'this' first appears here. I've no idea what it does or is as we haven't made any variables named 'this' up until now. What does 'this' used in this context do?

2)What does this.t do?

Thanks for the code. The questions are merely so I can understand this code and reuse it myself.

#5
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
"this" is basically just the current class object itself. You can use "this" to access the class objects variables and methods and is often used when making getter and setter methods for example:
public class Example{
    int variable;
    char variable2;
    public Example(int variable, char variable2){
        this.variable=variable;
        this.variable2=variable2;
    }

    //getter methods
    public int getVariable(){
        return this.variable;
    }
    public char getVariable2(){
        return this.variable2;
    }

    //setter methods
    public void setVariable(int variable){
        this.variable=variable;
    }
    public void setVariable2(char variable2){
        this.variable2=variable2;
    }

}

For 1 "this" is used to give the "MyActionListener" constructor a "KeyPad" object so "this" is just like giving an object of "KeyPad" where it is needed.
For 2 "this.t" is MyActionListener's variable "KeyPad t;" and we set it equal to "t" so we can access the variables from the KeyPad class in our action.
twas brillig

#6
Deco

Deco

    Newbie

  • Members
  • PipPip
  • 28 posts
'this' will come in handy.
Thanks