[RESOLVED] Using layouts to place objects?
Could really use some help with a homework problem. I need to display a numeric keypad, above the keypad show a label that displays numbers picked, to the right of keypad is another button to clear display. It says to user a border layout to manage overall presentation and grid layout to manage keypad buttons. Put a border around keypad buttons and a border around the display.
I'm not asking anyone to do it because I do want to understand it but I can't get any further at this point, so I could use some help.
I had gotten the grid with the keypad but now I'm not sure how to nest it within a border?
The errors say I'm trying to add the container's parent to itself. I'm trying to put a border around it, I don't understand how this works?
Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
at java.awt.Container.addImpl(Container.java:1017)
at java.awt.Container.add(Container.java:352)
at KeyPadPanel.<init>(KeyPadPanel.java:42)
at KeyPadDemo.main(KeyPadDemo.java:12)
Code:
import java.awt.*;
import javax.swing.*;
public class KeyPadPanel extends JPanel
{
public KeyPadPanel()
{
JPanel panel = new JPanel();
setLayout (new GridLayout (4,3));
panel.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
setBackground (Color.green);
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 b10 = new JButton ("#");
JButton b11 = new JButton ("0");
JButton b12 = new JButton ("*");
add (b1);
add (b2);
add (b3);
add (b4);
add (b5);
add (b6);
add (b7);
add (b8);
add (b9);
add (b10);
add (b11);
add (b12);
panel.add(panel);
}
}
the driver class
Code:
import javax.swing.*;
public class KeyPadDemo extends JPanel
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("KeyPad Demo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
KeyPadPanel panel = new KeyPadPanel();
frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}
}