所以,在这里的一些人的帮助下,我能够得到这个代码来编译和运行。但现在,另一个问题出现了。运行应用程序时,面板为空白。我是个新手,我知道我做的事情很愚蠢。我整晚坐在这里试图让它完全发挥作用,但无法弄清楚我还需要做些什么才能让它发挥作用。
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JOptionPane;
import javax.swing.JApplet;
import java.awt.event.*;
public class telephoneKeypad extends javax.swing.JFrame
{
public void 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();
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 :(得分:3)
您声明构造函数错误 - public void telephoneKeypad()
而不是public telephoneKeypad()
您仍然需要处理面板,但这实际上会运行构造函数而不是默认构造函数。
此外,通常类名以大写字母开头。