键盘Java应用程序可编译,但不';我什么也没表现出来
我不确定是不是我把我的主要空间放在哪里了?我让程序编译没有任何错误,但当我在TextPad中运行应用程序时,它只告诉我“按任意键继续”。。。。然后什么也不做键盘Java应用程序可编译,但不';我什么也没表现出来,java,Java,我不确定是不是我把我的主要空间放在哪里了?我让程序编译没有任何错误,但当我在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.eve
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);
}
}
您的应用程序应该是一个小程序(通过扩展
JApplet
)或者是一个应用程序(通过提供一个publicstaticvoidmain(String[])
方法作为入口点)。两样都有是很少见的
决定您想要哪一个,它会影响代码的编写方式和启动方式。将部分
extensed JApplet
更改为extensed javax.swing.JFrame
,然后从方法public void telephoneKeypad()
(即public telephoneKeypad()
,它将成为构造函数),方法init()
无论如何都不会被调用,所以您可以删除它。现在它应该可以工作了。试试这个源代码。仔细看评论
//<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;
}
}
不要
Runnable
/SwingUtilities.invokeLater()
)“两者都有是非常罕见的。”我通常会编写一个混合的小程序/应用程序——即使该代码只打算作为小程序运行。它可以使开发更快。@Andrew:我可能应该用不同的措辞。根据我的经验,如果一个混合应用程序被制作出来,它的布局应该有所不同。
prompt> javac TelephoneKeypad.java
prompt> appletviewer TelephoneKeypad.java
prompt> java TelephoneKeypad