[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5678
  • Last Modified:

Numeric Keypad (Basic Java Programming)

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
romeotheriault
Asked:
romeotheriault
  • 6
2 Solutions
 
GrandSchtroumpfCommented:
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
 
GrandSchtroumpfCommented:
0
 
GrandSchtroumpfCommented:
oooops,
BorderLayout bLayout new BorderLayout();
should have been
BorderLayout bLayout = new BorderLayout();
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ThummalaRaghuveerCommented:
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
 
GrandSchtroumpfCommented:
ThummalaRaghuveer,
we're not supposed to do other people's homework, we should just guide them.
0
 
GrandSchtroumpfCommented:
<:°)
0
 
romeotheriaultAuthor Commented:
GrandSchtroumpf, how did you get that little O that makes his mouth.
0
 
GrandSchtroumpfCommented:
> 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now