?
Solved

How to get the keypad numbers to display

Posted on 2009-02-09
2
Medium Priority
?
152 Views
Last Modified: 2013-11-23
I have the following code, but I am unable to get the numbers to display when they are clicked.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
 
 
public class celluar extends JFrame implements ActionListener{
    // creates borders for display
 
    public static void main(String[] args) {
	celluar c = new celluar();
    }
    public celluar() {
 
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	JPanel panel = new JPanel();
	panel.setLayout(new GridLayout(4, 3));
 
	panel.setBackground(Color.GRAY);
 
	// creates and add buttons for keypad
 
	JButton b1 = new JButton("1");
	panel.add(b1);
	b1.addActionListener(this);
	b1.setActionCommand("1");
	b1.setBackground(Color.BLUE);
 
	JButton b2 = new JButton("2");
	panel.add(b2);
	b2.addActionListener(this);
	b2.setActionCommand("2");
	b2.setBackground(Color.BLUE);
 
	JButton b3 = new JButton("3");
	panel.add(b3);
	b3.addActionListener(this);
	b3.setActionCommand("3");
	b3.setBackground(Color.BLUE);
 
	JButton b4 = new JButton("4");
	panel.add(b4);
	b4.addActionListener(this);
	b4.setActionCommand("4");
	b4.setBackground(Color.BLUE);
 
	JButton b5 = new JButton("5");
	panel.add(b5);
	b5.addActionListener(this);
	b5.setActionCommand("5");
	b5.setBackground(Color.BLUE);
 
	JButton b6 = new JButton("6");
	panel.add(b6);
	b6.addActionListener(this);
	b6.setActionCommand("6");
	b6.setBackground(Color.BLUE);
 
	JButton b7 = new JButton("7");
	panel.add(b7);
	b7.addActionListener(this);
	b7.setActionCommand("7");
	b7.setBackground(Color.BLUE);
 
	JButton b8 = new JButton("8");
	panel.add(b8);
	b8.addActionListener(this);
	b8.setActionCommand("8");
	b8.setBackground(Color.BLUE);
 
	JButton b9 = new JButton("9");
	panel.add(b9);
	b9.addActionListener(this);
	b9.setActionCommand("9");
	b9.setBackground(Color.BLUE);
 
	JButton b10 = new JButton("*");
	panel.add(b10);
	b10.addActionListener(this);
	b10.setActionCommand("*");
	b10.setBackground(Color.BLUE);
 
	JButton b11 = new JButton("0");
	panel.add(b11);
	b11.addActionListener(this);
	b11.setActionCommand("0");
	b11.setBackground(Color.BLUE);
 
 
	JButton b12 = new JButton("#");
	panel.add(b12);
	b12.addActionListener(this);
	b12.setActionCommand("#");
	b12.setBackground(Color.BLUE);
 
 
 
 
 
	getContentPane().setLayout(new BorderLayout());
	getContentPane().add(panel,BorderLayout.CENTER);
 
	JTextField number = new JTextField();
	number.setColumns(15);
	getContentPane().add(number,BorderLayout.NORTH);
 
	pack();
	setVisible(true);
 
    }
 
    public void actionPerformed(ActionEvent e) {}
}

Open in new window

0
Comment
Question by:bdiddy05
2 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 23594434
>>public void actionPerformed(ActionEvent e) {}

As i mentioned in your previous question, your ActionListener is empty
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 23594439
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
 
 
public class celluar extends JFrame implements ActionListener{
    // creates borders for display
 
    JTextField number = new JTextField();

    public static void main(String[] args) {
        celluar c = new celluar();
    }
    public celluar() {
 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 3));
 
        panel.setBackground(Color.GRAY);
 
        // creates and add buttons for keypad
 
        JButton b1 = new JButton("1");
        panel.add(b1);
        b1.addActionListener(this);
        b1.setActionCommand("1");
        b1.setBackground(Color.BLUE);
 
        JButton b2 = new JButton("2");
        panel.add(b2);
        b2.addActionListener(this);
        b2.setActionCommand("2");
        b2.setBackground(Color.BLUE);
 
        JButton b3 = new JButton("3");
        panel.add(b3);
        b3.addActionListener(this);
        b3.setActionCommand("3");
        b3.setBackground(Color.BLUE);
 
        JButton b4 = new JButton("4");
        panel.add(b4);
        b4.addActionListener(this);
        b4.setActionCommand("4");
        b4.setBackground(Color.BLUE);
 
        JButton b5 = new JButton("5");
        panel.add(b5);
        b5.addActionListener(this);
        b5.setActionCommand("5");
        b5.setBackground(Color.BLUE);
 
        JButton b6 = new JButton("6");
        panel.add(b6);
        b6.addActionListener(this);
        b6.setActionCommand("6");
        b6.setBackground(Color.BLUE);
 
        JButton b7 = new JButton("7");
        panel.add(b7);
        b7.addActionListener(this);
        b7.setActionCommand("7");
        b7.setBackground(Color.BLUE);
 
        JButton b8 = new JButton("8");
        panel.add(b8);
        b8.addActionListener(this);
        b8.setActionCommand("8");
        b8.setBackground(Color.BLUE);
 
        JButton b9 = new JButton("9");
        panel.add(b9);
        b9.addActionListener(this);
        b9.setActionCommand("9");
        b9.setBackground(Color.BLUE);
 
        JButton b10 = new JButton("*");
        panel.add(b10);
        b10.addActionListener(this);
        b10.setActionCommand("*");
        b10.setBackground(Color.BLUE);
 
        JButton b11 = new JButton("0");
        panel.add(b11);
        b11.addActionListener(this);
        b11.setActionCommand("0");
        b11.setBackground(Color.BLUE);
 
 
        JButton b12 = new JButton("#");
        panel.add(b12);
        b12.addActionListener(this);
        b12.setActionCommand("#");
        b12.setBackground(Color.BLUE);
 
 
 
 
 
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel,BorderLayout.CENTER);
 
        number.setColumns(15);
        getContentPane().add(number,BorderLayout.NORTH);
 
        pack();
        setVisible(true);
 
    }
 
    public void actionPerformed(ActionEvent e) {
          number.setText(number.getText()+e.getActionCommand());
    }
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question