Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Numeric Keypad (Basic Java Programming)

Posted on 2004-10-23
9
Medium Priority
?
5,511 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
Comment
Question by:romeotheriault
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
9 Comments
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 12391016
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
ID: 12391021
0
 
LVL 30

Assisted Solution

by:GrandSchtroumpf
GrandSchtroumpf earned 1000 total points
ID: 12391027
oooops,
BorderLayout bLayout new BorderLayout();
should have been
BorderLayout bLayout = new BorderLayout();
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Accepted Solution

by:
ThummalaRaghuveer earned 1000 total points
ID: 12391784
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
ID: 12392634
ThummalaRaghuveer,
we're not supposed to do other people's homework, we should just guide them.
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 12395270
<:°)
0
 

Author Comment

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

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

610 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