我正在为我的班级编写一个java应用程序。这是一个电话键盘。我快完成了。我只需要显示数字,我就不知道如何更改数字按钮的大小。到目前为止,我尝试过的所有内容在编译时都会导致错误。
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);
}
}
答案 0 :(得分:5)
我有几条建议:
GridLayout gridLayout1 = new GridLayout(0, 3);
setLocationRelativeTo(null);
之后和致电pack();
之前致电setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
。如,
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();
}
});
}
}
答案 1 :(得分:4)
您在没有任何参数的情况下实例化了GridLayout
。如果将其更改为
GridLayout gridLayout1 = new GridLayout(4,3);
你会得到更好的观点。
作为旁注,如果您使用一次布局对象,则只需执行
即可pnlKeyPad.setLayout(new GridLayout(4,3));
而不是
GridLayout gridLayout1 = new GridLayout(4,3);
pnlKeyPad.setLayout(gridLayout1);
(与您的所有布局相关...)
答案 2 :(得分:4)
另请参阅构造函数GridLayout
还尝试不在轻量级组件(java.awt.Button
)上添加重量级组件(javax.swing.JFrame
)。
因为Awt使用本机GUI小部件, 你的操作系统知道它们和处理 把它们放在一起, 而Swing你的小部件 只是一个无意义的像素 窗口;摆动手柄决定如何 你的小部件布局和堆叠。混合 这两个是非常不受支持的,可以 导致荒谬的结果,如 隐藏一切的原生按钮 否则在他们的对话框中 居住,因为其他一切都是 用Swing创建。
- What is the difference between swing and AWT?
答案 3 :(得分:1)
如何在键盘应用程序中调整按钮大小?
请参阅我对Removing the three dots “…” from a JButton?的回答,使用运行时参数重新调整字体大小。