2011-04-11 62 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. 提升「接受率」。