2011-04-11 61 views
0

我不确定这是我放置我的主要空白还是什么?我得到了程序编译没有任何错误,但是当我在TextPad运行应用程序,它只是告诉我“按任意键继续” ....然后什么也不做小键盘Java应用程序编译,但不显示任何东西

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); 
    } 
} 

回答

1

应用程序应要么成为一个小程序(通过扩展JApplet一个应用程序(通过提供一个public static void main(String[])方法作为入口点)。两者都非常罕见。

决定你想要哪一个,它影响你的代码应该如何写入以及它是如何启动的。

+0

“这两者都非常罕见。”我通常会编写一个混合applet /应用程序 - 即使代码只是作为一个applet运行。它可以使开发更快。 – 2011-04-11 08:55:08

+0

@Andrew:我应该说的话可能不同。根据我的经验,如果制作混合应用程序,应该以不同的方式进行布局。 – 2011-04-11 09:02:49

0

更改部分extends JAppletextends javax.swing.JFrame,然后从方法public void telephoneKeypad()删除返回类型(即public telephoneKeypad(),它将成为构造函数),方法init()永远不会无论如何调用,因此你可以将其删除。现在它应该工作。

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 

不要

  1. 混合摇摆与AWT
  2. 忽略废弃警告。
  3. 假设您可以通过复制/粘贴不理解的代码块来创建程序。
  4. 使用不一致的缩进/包围代码。

执行

  1. 构建在EDT的GUI(使用Runnable/SwingUtilities.invokeLater())。
  2. 继续发布SSCCE
  3. 继续使用论坛中的代码格式。
  4. 提升“接受率”。