簡體   English   中英

鍵盤Java應用程序已編譯,但未顯示任何內容

[英]Keypad Java application Compiles, but doesn't show anything

我不確定這是我放置主空隙的地方還是什么? 我可以編譯該程序而沒有任何錯誤,但是當我在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);
    }
 }

您的應用程序應該 applet(通過擴展JApplet應用程序(通過提供public static void main(String[])方法作為入口點)。 兩者都很少見。

確定所需的代碼,它會影響代碼的編寫方式和啟動方式。

更改extends JApplet extends 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;
    }
}

編譯/運行

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. 提升“接受率”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM