Search code examples
javaswingappletjapplet

How to get the NumPad layout


I need the layout for numpad on the keyboard. I got most of it, the only problem I am having is that the "enter" button is supposed to be till the bottom. But when I use the Panel p4 and I set it to SOUTH the "enter" button does not reach the bottom. Here is the code.

This is what it look like

enter image description here

This is what I wanted

enter image description here

public class NumPad extends Applet
{   
  public void init() {
  setLayout(new BorderLayout());

  Panel p1 = new Panel();
  p1.setLayout(new GridLayout(1, 0));
  p1.add(new Button("Num"));
  p1.add(new Button("/"));
  p1.add(new Button("*"));
  p1.add(new Button("-"));
  add(p1, BorderLayout.NORTH);

  Panel p2 = new Panel();
  p2.setLayout(new GridLayout(3, 0));
  p2.add(new Button("7"));
  p2.add(new Button("8"));
  p2.add(new Button("9"));
  p2.add(new Button("4"));
  p2.add(new Button("5"));
  p2.add(new Button("6"));
  p2.add(new Button("1"));
  p2.add(new Button("2"));
  p2.add(new Button("3"));
  add(p2, BorderLayout.CENTER);      

  Panel p4 = new Panel();
  p4.setLayout(new GridLayout(1, 0, 30, 40));
  p4.add(new Button("0"));
  p4.add(new Button("."));
  add(p4, BorderLayout.SOUTH);      

  Panel p3 = new Panel();
  p3.setLayout(new GridLayout(2, 0));
  p3.add(new Button("+"));
  p3.add(new Button("Enter"));
  add(p3, BorderLayout.EAST);      
 }
}

Solution

  • No need to re-arrange everything, you can simlpy nest p2 and p4 into another JPanel and add that new panel in the center. As long as you don't add anything in the SOUTH of the "main" panel, you are good to go.

    By the way, don't use awt components, go directly for the Swing ones. This will save you lot of time and avoid troubling behaviour (consider extending JApplet instead of Applet)

    See this small example:

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class NumPad extends JPanel {
        public NumPad() {
            setLayout(new BorderLayout());
    
            JPanel top = new JPanel(new GridLayout(1, 0));
            top.add(new JButton("Num"));
            top.add(new JButton("/"));
            top.add(new JButton("*"));
            top.add(new JButton("-"));
            add(top, BorderLayout.NORTH);
            JPanel p2p4 = new JPanel(new BorderLayout());
            JPanel p2 = new JPanel(new GridLayout(3, 0));
            p2.add(new JButton("7"));
            p2.add(new JButton("8"));
            p2.add(new JButton("9"));
            p2.add(new JButton("4"));
            p2.add(new JButton("5"));
            p2.add(new JButton("6"));
            p2.add(new JButton("1"));
            p2.add(new JButton("2"));
            p2.add(new JButton("3"));
            p2p4.add(p2, BorderLayout.CENTER);
    
            JPanel p4 = new JPanel();
            p4.setLayout(new GridLayout(1, 0, 30, 40));
            p4.add(new JButton("0"));
            p4.add(new JButton("."));
            p2p4.add(p4, BorderLayout.SOUTH);
            add(p2p4);
            JPanel p3 = new JPanel();
            p3.setLayout(new GridLayout(2, 0));
            p3.add(new JButton("+"));
            p3.add(new JButton("Enter"));
            add(p3, BorderLayout.EAST);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame(NumPad.class.getSimpleName());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    NumPad numPad = new NumPad();
                    frame.add(numPad);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    }