Solved

Numeric Keypad (Basic Java Programming)

Posted on 2004-10-23
4,939 Views
Last Modified: 2011-10-03
Hi all, here is the problem.  I need to design and implement a java program that displays a numeric keypad that might appear on a phone. Above the keypad buttons I need to show a label that displays the numbers as they are picked. To the right of the keypad buttons, I need to include another button to clear the display. I'm thinking that the "Border Layout" may be the best Layout Manager for this task. With a "Grid Layout" within one of the Border Layout fields. The book (I'm taking a java class, if you haven't figured that out yet.) doesn't explain how to call an call a panel into an existing panel.  Here is what I have so far.

//********************************************************************
//  BorderDemo.java      
//
//  Demonstrates the use of various types of borders.
//********************************************************************

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

public class PhoneReal extends JPanel
{
   //-----------------------------------------------------------------
   //  Creates several bordered panels and displays them.
   //-----------------------------------------------------------------
   
      
      public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Phone");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            
     
      JPanel panel = new JPanel();
      panel.setLayout (new GridLayout (4, 3));
      panel.setBorder (BorderFactory.createLineBorder (Color.gray, 3));
      panel.setBackground (Color.white);

      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");
            JButton b0 = new JButton ("0");

      panel.add (b1);
      panel.add (b2);
      panel.add (b3);
      panel.add (b4);
      panel.add (b5);
      panel.add (b6);
      panel.add (b7);
      panel.add (b8);
      panel.add (b9);
      panel.add (b0);
     
            frame.getContentPane().add (panel);
      frame.pack();
      frame.setVisible(true);
            
   }
}

So I'm really lost.  Some examples on how to do the above describe problem would be great. Thank you.
0
Question by:romeotheriault
    8 Comments
     
    LVL 30

    Expert Comment

    by:GrandSchtroumpf
    Example adding a panel to the north of another panel:

    JPanel myPanel = ...  // this is the panel you want to add to the other panel

    BorderLayout bLayout new BorderLayout();
    JPanel bPanel = new JPanel(bLayout);
    bPanel.add(myPanel, BorderLayout.NORTH);
    0
     
    LVL 30

    Expert Comment

    by:GrandSchtroumpf
    0
     
    LVL 30

    Assisted Solution

    by:GrandSchtroumpf
    oooops,
    BorderLayout bLayout new BorderLayout();
    should have been
    BorderLayout bLayout = new BorderLayout();
    0
     
    LVL 2

    Accepted Solution

    by:
    Here is the code that helps you with Layout but Implement action listeners for all buttons to get the functionality....

    // ********************************************************************
    //BorderDemo.java
    //
    //Demonstrates the use of various types of borders.
    //********************************************************************

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;

    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class PhoneReal extends JPanel {
        //-----------------------------------------------------------------
        //  Creates several bordered panels and displays them.
        //-----------------------------------------------------------------

        public static void main(String[] args) {
            JFrame frame = new JFrame("Phone");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(4, 3));
            //panel.setBorder(BorderFactory.createLineBorder(Color.gray, 3));
            panel.setBackground(Color.white);

            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");
            JButton b0 = new JButton("0");

            panel.add(b1);
            panel.add(b2);
            panel.add(b3);
            panel.add(b4);
            panel.add(b5);
            panel.add(b6);
            panel.add(b7);
            panel.add(b8);
            panel.add(b9);
            panel.add(b0);

            frame.getContentPane().setLayout(new BorderLayout());
            frame.getContentPane().add(panel,BorderLayout.CENTER);
            JButton clearBt = new JButton("Clear");
            JTextField number = new JTextField();
            number.setColumns(15);
            frame.getContentPane().add(number,BorderLayout.NORTH);
            frame.getContentPane().add(clearBt,BorderLayout.WEST);
            System.out.println(frame.getContentPane().getLayout());
            System.out.println(frame.getContentPane().getComponentCount());
            frame.pack();
            frame.setVisible(true);

        }
    }
    0
     
    LVL 30

    Expert Comment

    by:GrandSchtroumpf
    ThummalaRaghuveer,
    we're not supposed to do other people's homework, we should just guide them.
    0
     
    LVL 30

    Expert Comment

    by:GrandSchtroumpf
    <:°)
    0
     

    Author Comment

    by:romeotheriault
    GrandSchtroumpf, how did you get that little O that makes his mouth.
    0
     
    LVL 30

    Expert Comment

    by:GrandSchtroumpf
    > that little O that makes his mouth.
    it's a "degree" sign and it's supposed to be his round nose, lol.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now