我不确定这是我放置主要空白的地方还是什么?我得到程序编译没有任何错误,但当我在TextPad中运行应用程序时它只是告诉我'按任意键继续'....然后什么都不做
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 JApplet
{
public void init()
{
this.setLayout(new GridLayout(4,3));
this.setSize(new Dimension(175, 231));
new telephoneKeypad().setVisible(true);
}
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 :(得分:1)
您的应用 应该是applet(通过扩展JApplet
)或应用程序(通过提供public static void main(String[])
方法作为入口点)。两者都很少见。
决定你想要哪一个,它会影响你的代码应该如何编写以及它是如何开始的。
答案 1 :(得分:0)
将部分extends JApplet
更改为extends javax.swing.JFrame
,然后从方法public void telephoneKeypad ()
中删除返回的类型(即public telephoneKeypad ()
,它将成为构造函数),方法{{1永远不会被调用,所以你可以删除它。现在应该可以了。
答案 2 :(得分:0)
试试这个来源。仔细看看评论。
//<applet code='TelephoneKeypad' width='400' height='400'></applet>
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.*;
// class names should be EachWordUpperCase
public class TelephoneKeypad extends JApplet {
public void init() {
// an applet's size is set by the HTML
//this.setSize(new Dimension(175, 231));
Runnable r = new Runnable() {
public void run() {
TelephoneKeypadPanel kpad = new TelephoneKeypadPanel();
setContentPane(kpad.getKeyPad());
validate();
}
};
SwingUtilities.invokeLater(r);
}
public static void main(String args[]) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame("Telephone KeyPad");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TelephoneKeypadPanel kpad = new TelephoneKeypadPanel();
// use layouts.
// kpad.setBounds(500, 500, 500, 500);
f.setContentPane(kpad.getKeyPad());
f.pack();
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
class TelephoneKeypadPanel {
private JPanel pnlKeyPad;
TelephoneKeypadPanel () {
pnlKeyPad = new JPanel(new BorderLayout(5,5));
JButton btnZero = new JButton("0");
JButton btnOne = new JButton("1");
JButton btnTwo = new JButton("2");
JButton btnThree = new JButton("3");
JButton btnFour = new JButton("4");
JButton btnFive = new JButton("5");
JButton btnSix = new JButton("6");
JButton btnSeven = new JButton("7");
JButton btnEight = new JButton("8");
JButton btnNine = new JButton("9");
JButton btnStar = new JButton("*");
JButton btnHash = new JButton("#");
JButton btnDial = new JButton("Dial");
JTextField tfNumber = new JTextField(15);
JPanel pnlNumberEntry = new JPanel();
JPanel keys = new JPanel(new GridLayout(4,4,10,10));
pnlKeyPad.add(pnlNumberEntry, BorderLayout.NORTH);
// what is with all the 'null' layout constraints?!?
pnlNumberEntry.add(tfNumber);
pnlKeyPad.add(keys, BorderLayout.CENTER);
pnlKeyPad.add(btnDial, BorderLayout.SOUTH);
keys.add(btnOne);
keys.add(btnTwo);
keys.add(btnThree);
keys.add(btnFour);
keys.add(btnFive);
keys.add(btnSix);
keys.add(btnSeven);
keys.add(btnEight);
keys.add(btnNine);
keys.add(btnStar);
keys.add(btnZero);
keys.add(btnHash);
}
public JPanel getKeyPad() {
return pnlKeyPad;
}
}
编译/运行
prompt> javac TelephoneKeypad.java
prompt> appletviewer TelephoneKeypad.java
prompt> java TelephoneKeypad
<强>唐&#39;吨强>
<强>不要强>
Runnable
/ SwingUtilities.invokeLater()
)。