1

I'm writing a java application for my class. It's a telephone keypad. I'm almost finished with it. I just have to get the numbers to show I just don't have a clue of how to change the size of the number buttons. Everything I've tried so far has resulted in ERRORS when compiling.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JOptionPane;
import javax.swing.JApplet;
import javax.swing.JFrame;
import java.awt.event.*;

public class TelephoneKeypad extends javax.swing.JFrame
{


    public TelephoneKeypad()
    {
        Panel pnlKeyPad = new Panel();
          GridLayout gridLayout1 = new GridLayout();
          Button btnZero = new Button();
          Button btnOne = new Button();
          Button btnTwo = new Button();
          Button btnThree = new Button();
        Button btnFour = new Button();
          Button btnFive = new Button();
          Button btnSix = new Button();
          Button btnSeven = new Button();
          Button btnEight = new Button();
          Button btnNine = new Button();
          Button btnStar = new Button();
          Button btnHash = new Button();

        TextField tfNumber = new TextField(15);
          Button btnDial = new Button();
          BorderLayout borderLayout1 = new BorderLayout();
          Panel pnlNumberEntry = new Panel();
          FlowLayout flowLayout1 = new FlowLayout();





            btnOne.setLabel("1");
            btnTwo.setLabel("2");
            btnThree.setLabel("3");
            btnFour.setLabel("4");
            btnFive.setLabel("5");
            btnSix.setLabel("6");
            btnSeven.setLabel("7");
            btnEight.setLabel("8");
            btnNine.setLabel("9");
            btnStar.setLabel("*");
            btnZero.setLabel("0");
            btnHash.setLabel("#");
            btnDial.setLabel("Dial");

            pnlNumberEntry.setLayout(flowLayout1);
            pnlKeyPad.setLayout(gridLayout1);
            this.setLayout(borderLayout1);
            this.add(pnlNumberEntry, BorderLayout.NORTH);
            pnlNumberEntry.add(tfNumber, null);
            pnlNumberEntry.add(btnDial, null);
            this.add(pnlKeyPad, BorderLayout.CENTER);
            pnlKeyPad.add(btnOne, null);
            pnlKeyPad.add(btnTwo, null);
            pnlKeyPad.add(btnThree, null);
            pnlKeyPad.add(btnFour, null);
            pnlKeyPad.add(btnFive, null);
            pnlKeyPad.add(btnSix, null);
            pnlKeyPad.add(btnSeven, null);
            pnlKeyPad.add(btnEight, null);
            pnlKeyPad.add(btnNine, null);
            pnlKeyPad.add(btnStar, null);
            pnlKeyPad.add(btnZero, null);
            pnlKeyPad.add(btnHash, null);
        }

            public static void main(String args[])
                {
                TelephoneKeypad kpad = new TelephoneKeypad();
                kpad.setBounds(500, 500, 500, 500);
                kpad.setVisible(true);
    }
 }
skaffman
  • 398,947
  • 96
  • 818
  • 769
Josh Tha CreativeOne
  • 279
  • 2
  • 11
  • 22
  • 1
    See also [L&F size variants](http://stackoverflow.com/questions/2899935/java-swing-low-profile-button-height/2900157#2900157). – trashgod Apr 30 '11 at 21:38
  • 1
    See aslo [How to Use Actions](http://download.oracle.com/javase/tutorial/uiswing/misc/action.html). – trashgod Apr 30 '11 at 23:15
  • 2
    Your question is tagged with a Swing tag and you are using a JFrame, yet all your other components are AWT component. Don't mix AWT components in a Swing application. Make sure the components you use start with "J". – camickr May 01 '11 at 02:52

4 Answers4

5

I have a couple of recommendations:

  • set the GridLayout constants when calling its constructor. e.g., GridLayout gridLayout1 = new GridLayout(0, 3);
  • Don't set the bounds of your GUI but let the GUI's layout managers do this for you based on its preferred size.
  • Don't forget to call pack() on the JFrame after adding all components
  • I usually call setLocationRelativeTo(null); after calling pack(); and before calling setVisible(true);
  • Don't forget to call setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); in your class's (JFrame) constructor.
  • One way to set the button's size is to set the Font of the button to be larger.

e.g.,

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

public class TPad extends JPanel {
   public static final String[][] BTN_TEXTS = {
      {"1", "2", "3"},
      {"4", "5", "6"},
      {"7", "8", "9"},
      {"*", "0", "#"}
   };
   private static final int GAP = 5;
   private static final int FONT_POINTS = 36;
   private static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, FONT_POINTS);

   public TPad() {
      int rows = BTN_TEXTS.length;
      int cols = BTN_TEXTS[0].length;
      setLayout(new GridLayout(rows, cols, GAP, GAP));
      setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
      for (int row = 0; row < BTN_TEXTS.length; row++) {
         for (int col = 0; col < BTN_TEXTS[row].length; col++) {
            JButton btn = new JButton(BTN_TEXTS[row][col]);
            btn.setFont(BTN_FONT);
            add(btn);
         }
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("TPad");
      frame.getContentPane().add(new TPad());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @Boro: thanks. Methinks you're going to run out of votes today just like I'm about to do (I Have only 4 votes left). – Hovercraft Full Of Eels Apr 30 '11 at 20:48
  • 1
    yea probably I will. Someone has to judge all these answers, right? :) Plus there is always another day with new votes :) All best to you. – Boro Apr 30 '11 at 21:04
4

You instantiated GridLayout without any parameters. If you change it to

GridLayout gridLayout1 = new GridLayout(4,3);

You'll get much better view.

As a side note, if you use a layout object once, you can simply do

pnlKeyPad.setLayout(new GridLayout(4,3));

instead of

GridLayout gridLayout1 = new GridLayout(4,3);
pnlKeyPad.setLayout(gridLayout1);

(relevant for all of your layouts...)

MByD
  • 135,866
  • 28
  • 264
  • 277
4

Refer How to Use GridLayout

Also refer the constructors for GridLayout


Also try not to add a heavyweight component (java.awt.Button) on a lightweight component(javax.swing.JFrame).

Because Awt uses native GUI widgets, your OS knows about them and handles putting them in front of each other, etc. Whereas with Swing your widgets are just meaningless pixels within a window; Swing handles deciding how your widgets lay out and stack. Mixing the two is highly unsupported and can lead to ridiculous results, such as native buttons that obscure everything else in the dialog box in which they reside because everything else was created with Swing.
- What is the difference between swing and AWT?

Community
  • 1
  • 1
Alpine
  • 3,838
  • 1
  • 25
  • 18
  • 1
    +1 well spot mixing of heavy and light weight components. It is recommended to never mix these two types of objects. If you must here is a great [tutorial](http://java.sun.com/products/jfc/tsc/articles/mixing/) explaining what to expect. – Boro Apr 30 '11 at 20:42
1

How do I adjust Button size in Keyboard application?

See my answer to Removing the three dots “…” from a JButton? re adjusting the font size using run-time parameters.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433