How do I adjust Button size in Keyboard application?
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 fa开发者_JAVA技巧r 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);
}
}
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 callingpack();
and before callingsetVisible(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();
}
});
}
}
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...)
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?
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.
精彩评论